【问题标题】:How can I insert multiple children along with parent record using Laravel Eloquent如何使用 Laravel Eloquent 插入多个子项和父记录
【发布时间】:2016-02-19 13:10:09
【问题描述】:

我正在尝试使用 Laravel 创建一个简单的购物应用程序,我正在使用 LaravelShoppingCart 插件在用户会话中存储商品。当用户点击我的store 方法时,应该存储orderorder_line 记录。

我目前收到以下代码的此错误:

参数 1 传递给 Illuminate\Database\Eloquent\Model::__construct() 必须是数组类型,给定对象,调用 /home/vagrant/site/app/Http/Controllers/BasketController.php上线 130和定义

表架构:

// create orders table
Schema::create('orders', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users');
    $table->text('order_notes')->nullable();
    $table->decimal('subtotal', 5, 2)->default(0);
    $table->timestamps();
});

// create order lines table
Schema::create('order_lines', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('order_id')->unsigned();
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
    $table->integer('menu_item_id')->unsigned();
        $table->foreign('menu_item_id')->references('id')->on('menu_item');
    $table->decimal('sale_price', 5, 2);
    $table->integer('quantity');
    $table->timestamps();
});

订单模型(关系):

/**
 * Define relationship to OrderLines
 * @return [type] [description]
 */
public function order_line()
{
    return $this->hasMany('App\OrderLine');
}

OrderLine 模型(关系):

public function order()
{
    return $this->belongsTo('App\Order');
}

存储功能:

public function store(CreateOrderGuestRequest $request)
{

    $order = new Order;

    $order->order_notes = $request->order_notes;
    $order->save();

    $order_lines = new Collection();

    foreach (Cart::content() as $row) {
        $order_lines->push([
            'menu_item_id' => $row->id,
            'quanity'      => $row->qty,
            'sale_price'   => $row->price
        ]);
    }

    $order->order_line()->saveMany(new OrderLine($order_lines));

    // redirect somewhere after


}

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    你已经很接近了。您只需要进行一些调整即可使其正常工作。您需要将模型集合或模型数组传递给 saveMany 方法,因此当您循环时,不是“推”一个数组,而是“推”一个新的 OrderLine 模型,如下所示:

    foreach (Cart::content() as $row) {
        $order_lines->push(
            new OrderLine([
                'menu_item_id' => $row->id,
                'quanity'      => $row->qty,
                'sale_price'   => $row->price
            ])
        );
    }
    

    那么,当你调用saveMany时,只需传递$order_lines集合;

    $order->order_line()->saveMany($order_lines);
    

    当然,这是假设 Cart::content() 有效。

    在相关说明中:saveMany 不会通过单个查询插入所有条目。它将循环并一一添加。要使用单个查询进行批量插入,您需要使用查询生成器。

    编辑:如何使用查询生成器使用insert 方法的示例:

    $order_lines = [];
    
    foreach (Cart::content() as $row) {
        $order_lines[] = [
            'menu_item_id' => $row->id,
            'quanity'      => $row->qty,
            'sale_price'   => $row->price,
            'order_id'     => $order->id
        ];
    }
    
    DB::table('order_lines')->insert($order_lines);
    

    您只需构建一个数据数组以插入到表中并插入它。

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 2019-06-12
      • 2015-03-02
      • 1970-01-01
      • 2016-05-04
      相关资源
      最近更新 更多