1 month ago

Laravel: check if model property already exists before uploading into DB using Eloquent, performance issues

Posted 1 month ago by Gabotronix

I have a huge ammount 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 ammount 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();

                $newVenue = new Venue();
                $newVenue->name = $venue['name'];

        return response()->json([
            'message' => 'All venues uploaded',

Help me out please!

Please sign in or create an account to participate in this conversation.