How to check if a record is new in Laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
One of the most important features of Laravel's object-relational mapper (ORM) is Eloquent, which provides a convenient way to interact with your database tables as objects. This makes working with data much easier and more efficient compared to traditional SQL queries.
While Eloquent offers numerous advantages, it doesn't directly provide a built-in function to check if a record is new or existing in the database. However, that does not mean we cannot determine whether a model belongs to an already inserted record or a newly created one. In fact, there are multiple ways you can approach this task.
1. Manual Check: The most straightforward method is to compare the primary key value of the model with its actual database identifier. For this, you can use the 'exists' method on the given query builder. Here is an example:php
$article = new Article;
if(!$article->exists()) {
// New article instance with no primary key assigned
} else {
// Existing article instance with a valid primary key
}php
$article = new Article;
if($article->scope('new')->get()->count() == 0) {
// New article instance with no primary key assigned
} else {
// Existing article instance with a valid primary key
}php
$article = new Article;
$article->saving(function($model) {
if(!$model->contains('id')) {
// New article instance with no primary key assigned
} else {
// Existing article instance with a valid primary key
}
});php
$article = new Article;
$article->isNew = true;
$article->save();
if($article->isNew) {
// New article instance with no primary key assigned
} else {
// Existing article instance with a valid primary key
}