In this tutorial, we’ll explore the difference between the property exists
and the exists()
method in Laravel.
Let’s get started!
🎥 Prefer a video instead? I got you covered:
The Example
use App\Models\Product;
$product = Product::query()->find(1);
$newProduct = new Product();
In this example, we have two Product
models:
- One retrieved from the database (we assume the
product
table is not empty) - Another newly created but not saved
Let’s explore how the exists
property works.
How Does exist
Work?
The exists
is a property of an Eloquent model that indicates whether the model is stored in the database.
use App\Models\Product;
$product = Product::query()->find(1);
$newProduct = new Product();
dd($product->exists, $newProduct->exists); // true, false
$product->exists
returns true
because the model was retrieved from the database, meaning it already exists.
$newProduct->exists
returns false
because we created it but haven’t saved it yet, so it doesn’t exist in the database.
Let’s now explore how the exists()
Query Builder method works.
How Does exists()
Work?
use App\Models\Product;
$product = Product::query()->find(1);
$newProduct = new Product();
dd($product->exists(), $newProduct->exists()); // true, true
🤔 Surprisingly, both return true
. But why?
For $product->exists()
, this makes sense since it was retrieved from the database.
However, to understand why $newProduct->exists()
also returns true
, we need to take a closer look.
Let’s dump the query log to inspect it.
use App\Models\Product;
use Illuminate\Support\Facades\DB;
$product = Product::query()->find(1);
$newProduct = new Product();
DB::enableQueryLog();
$newProduct->exists();
dd(DB::getQueryLog());
// returns
array:1 [
0 => array:3 [
"query" => "select exists(select * from `products`) as `exists`"
"bindings" => []
"time" => 0.27
]
]
The query checks if there is at least one row in the products
table. If the table has rows, the query will return true
. If the table is empty, it will return false
.
In my case, the products
table is not empty and that’s why $newProduct->exists()
returns true
. If the products
table was empty, then $newProduct->exists()
would return false
.
📖 If you want to learn more about the exists()
method, check out How to Efficiently Check if a Record Exists.
Conclusion
In this tutorial, we’ve explored the difference between the exists
property and the exists()
method in Laravel.
While both are related to checking whether a model exists in the database, they serve different purposes.
The exists
property is specific to an individual Eloquent model and indicates whether the model was retrieved from the database or not.
It returns true
if the model exists in the database, and false
if it’s a newly created model that hasn’t been saved.
On the other hand, the exists()
method is a Query Builder method that checks if at least one record exists in the database for a given table (products
in our case). This method works by executing a query to verify the presence of rows in the table, not necessarily tied to a specific model instance.
I hope you found this article helpful! If you have any questions or comments, feel free to leave them below. I’d love to hear your thoughts! 💜
Let me know what you think about this article in the comments section below.
If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.