如何在 Laravel 5.4 中的 firstOrCreate 中使用 where

Mik*_*ams 2 php model laravel laravel-5.3 laravel-eloquent

我如何使用这样的代码:

 $db = ModelName::firstOrCreate(['bot_id'=>10, 'bot_text'=>$botTxt->id])
->where('updated_at','<=', Carbon::today());
Run Code Online (Sandbox Code Playgroud)

这不能正常工作。

Ale*_*nin 5

正确的语法是:

ModelName::where('updated_at','<=', Carbon::today())
    ->firstOrCreate(['bot_id' => 10, 'bot_text' => $botTxt->id]);
Run Code Online (Sandbox Code Playgroud)

此外,由于firstOrCreate()使用了批量分配功能,请确保您已$fillableModelName类中定义了该属性:

protected $fillable = ['bot_id', 'bot_text'];
Run Code Online (Sandbox Code Playgroud)