Laravel save one to many relationship

后端 未结 3 1234
陌清茗
陌清茗 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:33

    Sure you can do this:

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

    (status() is the belongsTo relation. You might need to adjust that name)

提交回复
热议问题