Find or Create with Eloquent

后端 未结 6 1851
我在风中等你
我在风中等你 2020-12-06 04:22

I have recently started working with Laravel and Eloquent, and was wondering about the lack of a find or create option for models. You could always write, for example:

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 04:33

    Laravel 4 models have a built-in findOrNew method that does what you need:

    $user = User::findOrNew($id);
    

提交回复
热议问题