Home > Blockchain >  Return Model of Newly created Multiple records
Return Model of Newly created Multiple records

Time:09-21

I need to get the model of newly created records

$data = [['name' => 'john'], ['name' => 'Doe']];
$result = Model::insert($data);

But when I checked the $result variable it was Boolean.

How can I get the newly created records?

CodePudding user response:

using fresh() solved my problem

$data = [['name' => 'john'], ['name' => 'Doe']];
$result = Model::create($data);
$data = $result->fresh();

CodePudding user response:

Instead of using insert you can use create, for example,

$data = [['name' => 'john'], ['name' => 'Doe']];
$result = Model::insert($data);//instead of this!
$result = Model::create($data);// use this,

then the $result keyword will return last created data. if you are using, $result = $model->save(); method, you have to return $model variable. should not return $result, because it will give you the Boolean value only.

  • Related