【问题标题】:How to save back-end data in two tables in october cms十月cms如何将后端数据保存在两个表中
【发布时间】:2020-09-23 07:27:36
【问题描述】:

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

找不到列:1054 '字段列表'中的未知列'pickup_time'(SQL:插入offline_mall_brandsnameidpickup_timedelivery_timeslugwebsite , description, updated_at, created_at) 值 (ASDE, 1, ["0","1"], ["2","3"], zip-jet, , , 2020-06-04 10:19:17, 2020-06-04 10:19:17))”在 /home/vagrant/code/cml/vendor/laravel/framework/src/Illuminate/Database/Connection.php 的第 664 行

品牌

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  

【问题讨论】:

    标签: octobercms octobercms-plugins octobercms-backend october-form-controller


    【解决方案1】:

    哦,我明白发生了什么。 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'
    
        }
    }
    

    【讨论】:

    • 对不起我的错误,它是类型..表名相同而不是大写等..
    • @Danish 在再次查看问题后更新了我的答案。
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多