phplaravelmodellaravel-5.3eloquent

How can use a where in firstOrCreate in Laravel 5.4


How can I use a code like this:

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

This is not working correctly.


Solution

  • The correct syntax is:

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

    Also, since firstOrCreate() uses the mass assignment feature, make sure you've defined the $fillable property in the ModelName class:

    protected $fillable = ['bot_id', 'bot_text'];