Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rezuankassim's avatar

Eloquent make exists return true

Hi guys I have the code below

$post = Post::make();
dd($post->exists()); // true

Is this the correct behaviour for exists method? I thought exists is to check whether the record exists in database?

Thanks in advance.

0 likes
7 replies
rezuankassim's avatar

So to check if the model is exists in the database or not, we can use $post->exists or $post->getKey() right?

Sinnbeck's avatar

Or you could just query the database.. Be aware that make() does not query the data

$post = Post::find(22);
if ($post) {
    //post will be null if not found and this code isnt run
}
Sinnbeck's avatar

Just tried that exact code and the exists query is compiled to the following

select exists(select * from `posts` where `posts`.`deleted_at` is null) as `exists`
rezuankassim's avatar

So this is why it return true? Hmm, I always thought that exists() will if there is a record in the database..

mvd's avatar
mvd
Best Answer
Level 48

You can use exists() on queries, $post = Post::make(); is not a query, with this code you are only creating and returning an un-saved model instance.

Please or to participate in this conversation.