I have a huge amount of records I want to upload into MySQL database in my Laravel app, in order to avoid uploading duplicates I want to check if name there is already a model in database with the same name, I'm doing this check with Eloquent but given I'll be handling large amount of data I was wondering if there is a more performant way to do this, my objective is to perform the less DB operations possible!
My code:
public function uploadIntoDatabase(Request $request)
{
$venuesToUpload = $request['venuesToUpload'];
$allVenues = Venue::all();
foreach($venuesToUpload as $index => $venue)
{
$alreadyAdded = Venue::where('name', $venue['name'])->first();
if(!$alreadyAdded)
{
$newVenue = new Venue();
$newVenue->name = $venue['name'];
$newVenue->save();
}
}
return response()->json([
'message' => 'All venues uploaded',
]);
}