Creating and Update Laravel Eloquent
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficient Data Management with Laravel Eloquent: Creating or Updating Records - A Comprehensive Guide
Managing the lifecycle of records in your database can sometimes be a complex task, especially if you're dealing with millions of records. The Laravel Eloquent ORM provides developers with powerful tools to interact with their databases, making this task easier. In this blog post, we will explore how you can efficiently create new records or update them using the shorthand method available in Laravel.
Laravel Eloquent: A Brief Introduction
Eloquent is a part of the Laravel framework that provides an object-relational mapper (ORM) for working with database tables. It allows you to use PHP classes to interact with your relational database, making it easier and more intuitive. Eloquent models are simply PHP classes that represent database table rows as objects. This approach enables developers to perform CRUD operations (Create, Read, Update, Delete) on the data without explicitly interacting with the database.Shorthand Method for Inserting New Records
To insert a new record into your database using Laravel Eloquent, you can use the shorthand method `firstOrCreate`. This method will check if there exists any row in the database that matches the specified criteria. If no records match, it'll create a new one for you. Consider the following example:<?php
$shopOwner = ShopMeta::where('shopId', '=', $theID)
->where('metadataKey', '=', 2001)->firstOrCreate();
The above code snippet will first search for a record with the given `shopId` and `metadataKey`. If no records match, it will then create a new record in the database. The `ShopMeta` model represents an instance of the database table 'shops_meta', and the `firstOrCreate()` method handles the shorthand insertion logic.
Shorthand Method for Updating Existing Records
Similar to creating a new record, you can use Laravel Eloquent's shorthand method, `updateOrCreate`, to update an existing record or create a new one if it doesn't exist. This method will first check if any rows in the database match the specified criteria. If a match is found, it will update the selected fields of that row; otherwise, it creates a new record corresponding to your query. Here's an example:<?php
$shopOwner = ShopMeta::where('shopId', '=', $theID)
->updateOrCreate([
'metadataKey' => 2001,
'unique_identifier' => uniqid(),
]);
In this example, the code searches for a record with the given `shopId` and checks if it has the specific `metadataKey`. If a match is found, the method will update the value of 'unique_identifier' to a new random string. Otherwise, a record will be created with the specified values: