Home > Enterprise >  Laravel firstOrNew based on created_at same date
Laravel firstOrNew based on created_at same date

Time:09-27

I am using lumen. I want to use firstOrNew based on created_at. For example in my table there exists data for created_at = 2021-09-23 14:42:13 . For that cases it will update if request date value become that day. Here I tried with

$tableObj = User::firstOrNew([
        'created_at' => Carbon::today() // date('Y-m-d')
    ]);
$tableObj ->mobile= '5457874545';
$tableObj ->save();

Here it always inserted new row. Thanks in advance

CodePudding user response:

Can You Please Format the date try

 $tableObj = User::firstOrNew([
        'created_at' => Carbon::today()->format('Y-m-d') // date('Y-m-d')
    ]);
$tableObj ->mobile= '5457874545';
$tableObj ->save();

or use UpdateOrCreate method of laravel

  $tableObj = User::updateOrCreate(
               ['created_at' => Carbon::today()->format('Y-m-d')], 
               ['mobile'=>'5457874545']
             );
  • Related