八宝网cms如何保存两张表的后台数据

问题描述 投票:0回答:1

我有两个表格 BrandsBrandTimings我想保存品牌的取货和交货时间,我有一个表单,我提交的所有数据,我得到一个错误,我明白错误,但我不知道如何分配取货和交货时间的变量,并删除它们,然后使用afterSave(),保存在BrandTimings与新创建的品牌的ID,我已经尝试beforeSave(),我不能理解如何做,因为十月cms文档没有例子。

未找到列:1054 '字段列表'中的未知列'pickup_time'(SQL:insert into offline_mall_brands (name, id, pickup_time, delivery_time, slug, website, 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))" 在homevagrantcodecmlvendorlaravelframeworksrcIlluminateDatabaseConnection.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个回答
1
投票

哦,我知道发生了什么。Brands 没有送货时间,但 BrandTimings 的。有一种方法可以在你的php中实现。现在你没有提到这些模型之间是否有关系,所以我就不这么写了。而且你也没有说明你现在是如何尝试保存的,所以我打算编造一下。这里举个例子。

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'

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.