我如何使用这样的代码:
$db = ModelName::firstOrCreate(['bot_id'=>10, 'bot_text'=>$botTxt->id])
->where('updated_at','<=', Carbon::today());
这不能正常工作。
答案 0 :(得分:3)
正确的语法是:
ModelName::where('updated_at','<=', Carbon::today())
->firstOrCreate(['bot_id' => 10, 'bot_text' => $botTxt->id]);
此外,由于firstOrCreate()
使用了质量分配功能,因此请确保您已在$fillable
类中定义了ModelName
属性:
protected $fillable = ['bot_id', 'bot_text'];
答案 1 :(得分:0)
您也可以像这样在 firstOrCreate 方法的第一个参数中传递条件:
$db = ModelName::firstOrCreate([
['updated_at','<=', Carbon::today()],
'bot_id'=>10,
'bot_text'=>$botTxt->id
]);