Laravel save one to many relationship

后端 未结 3 1236
陌清茗
陌清茗 2021-02-19 14:14

I have the following relationships set up in Laravel:

OrderStatus Model
  - hasMany(\'Order\')

 Order Model
  - \'belongsTo(\'OrderStatus\');

3条回答
  •  面向向阳花
    2021-02-19 14:36

    The correct way, to save a relationship for a new related model is as follows:

    $status = OrderStatus::where(['name'=>'sample_status'])->firstOrFail();
    $order = new Order;
    $status->order()->save($order);
    

    Documentation link : http://laravel.com/docs/4.2/eloquent#inserting-related-models

提交回复
热议问题