I have a project in laravel with a database < a href="/ questions / tagged / mysql" class="post-tag" title = 'show questions with the tag "mysql"'> mysql and another with mysql to make a transfer of data from one system to another, the fields from one to another are similar but not the same I do a script like this (I put the code abbreviated):
public function readCars():JsonResponse
{
$count = 0;
$data = array();
$sql = 'SELECT * from Cars';
$cars = DB::connection('mysql_old')->select($sql);
foreach ($cars as $car) {
unset($data);
$data['id'] = $car->car_id;
$data['enabled'] = $car->active;
$exists = Car::where('id',$data['id'])->first();
if ($exists === null) {
Car::create($data);
$count++;
}
}
return $this->jsonOkResponse($count);
}
The code what it does is look for the record in mysql_old
, see if it exists with the function ( DB::where...
) and if it does not exist it inserts it.
My question is whether this option is not more optimal with firstOrCreate
.
Thanks