首页 > 解决方案 > 十月cms如何将后端数据保存在两个表中

问题描述

我有两个表BrandsBrandTimings我想用取货和送货时间来保存品牌,我有一个表格,我要提交所有数据我收到错误,我理解错误,但我不知道如何分配取货和送货时间到一个变量并删除它们,然后使用 afterSave(),用新创建的品牌 ID 将它们保存在 BrandTimings 中,我尝试过 beforeSave(),但我不明白该怎么做,因为 10 月 cms 文档没有示例,

未找到列:1054 '字段列表'中的未知列 'pickup_time'(SQL:插入offline_mall_brandsname、、、、、、、、、、、)值( ASDE id、 1 pickup_time、[ "0"、"1"]、[" delivery_time2 slug" ,"3"], zip-jet, , , 2020-06-04 10:19:17, 2020-06-04 10:19:17))" 在 /home/vagrant/code/cml/vendor 的第 664 行/laravel/framework/src/Illuminate/Database/Connection.phpwebsitedescriptionupdated_atcreated_at

品牌

id     name      location
1      Inspire   Lacer

品牌时间

id     brand_id      pickup_time     delivery_time
1      1             08:00           15:00 
1      1             09:00           16:00
1      1             10:00           17:00
1      1             11:00           18:00  

标签: octobercmsoctobercms-pluginsoctobercms-backendoctober-form-controller

解决方案


哦,我明白发生了什么。Brands没有delivery_time,但BrandTimings有。一种可行的方法是在您的 php.ini 文件中。现在你没有提到这些模型是否相互关联,所以我不会那样写。而且你没有展示你现在是如何尝试保存它的,所以我要弥补它。这是一个例子:

public function onSaveBrandTimings() {

    // Get inputs and you would validate them as well
    $name = Input::get('name');
    $location = Input::get('location');
    $pickup_time = Input::get('pickup_time');
    $delivery_time = Input::get('deliver_time');

    if ($validates == true) {

        // Create Brand
        $brand = New Brands;// Create brand instance but won't have id
        $brand->name = $name;
        $brand->location = $location;
        $brand->save(); // Saving the brand will fill in the ID in the instance

        // Create BrandTimings
        $brandTimings = new BrandTimings;
        $brandTimings->brand_id = $brand->id;
        $brandTimings->pickup_time = $pickup_time;
        $brandTimings->delivery_time = $delivery_time;
        $brandTimings->save();

        // Return a statment or a partial update or a redirect
        return 'Success'

    } else {

        return 'Failed'

    }
}

推荐阅读