duancong2160 2015-11-17 23:42
浏览 103

如何使用Laravel Eloquent插入多个子项以及父记录

I am trying to create a simple shopping application using Laravel, I am using LaravelShoppingCart plugin to store items in the user session. When the user hits my store method the order and order_line records should be stored.

I am currently getting this error with the code below:

Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given, called in /home/vagrant/site/app/Http/Controllers/BasketController.php on line 130 and defined

Table schema:

// create orders table
Schema::create('orders', function (Blueprint $table) {
    $table->decimal('subtotal', 5, 2)->default(0);

// create order lines table
Schema::create('order_lines', function (Blueprint $table) {
    $table->decimal('sale_price', 5, 2);

Order Model (relationship):

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

OrderLine Model (relationship):

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

Store Function:

public function store(CreateOrderGuestRequest $request)

    $order = new Order;

    $order->order_notes = $request->order_notes;

    $order_lines = new Collection();

    foreach (Cart::content() as $row) {
            'menu_item_id' => $row->id,
            'quanity'      => $row->qty,
            'sale_price'   => $row->price

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

    // redirect somewhere after

  • 写回答

1条回答 默认 最新

  • doucai1901 2015-11-17 23:51

    You were pretty close. You just need to make a few adjustments to get this to work. You need to pass a collection of models or an array of models to the saveMany method so when you loop through, instead of "pushing" an array, "push" a new OrderLine model like this:

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

    Then, when you call saveMany, just pass the $order_lines collection;


    This, of course, is assuming Cart::content() is valid.

    On a related note: saveMany will not insert all the entries with a single query. It'll loop through and add them one-by-one. To do a bulk insert with just a single query, you need to use the query builder.

    Edit: An example of how to use the insert method using the query builder:

    $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

    You just build an array of data to insert into the table and insert it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方