Find or Create with Eloquent

后端 未结 6 1850
我在风中等你
我在风中等你 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:34

    You can use firstOrCreate (It's working with Laravel 4.2)

    $bucketUser = BucketUser::firstOrCreate([
        'bucket_id' => '1',
        'user_id'   => '2',
    ]);
    

    returns found instance or new instance.

提交回复
热议问题