Eloquent: find() and where() usage laravel

Eloquent: find() and where() usage laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Behavior Behind find() and where() in Eloquent

When debugging a query where $post = Post::find($id) returns null but $post = Post::where('id', $id)->delete() executes successfully, it often signals a misunderstanding of how Eloquent handles model hydration versus raw query chaining. The confusion stems from the fundamental difference between these two methods: find() is a convenience method that attempts to hydrate a single record immediately, whereas where() returns an unhydrated Query Builder instance.

The core issue with the initial approach lies in the return value of Post::find($id). If the record does not exist, find() explicitly returns null. In the provided code snippet, if $post is null, calling $post->delete() results in a fatal error because you cannot invoke methods on a null object. Conversely, when using Post::where('id', $id) without chaining a finder method like first(), the expression does not return a model instance or null. Instead, it returns an instance of Illuminate\Database\Eloquent\Builder. This builder object represents the query itself but has no specific model data attached yet.

The reason the second approach "works" in the context of deletion is that Eloquent's Query Builder allows you to chain destructive methods directly onto the query chain. When you call $builder->delete(), Eloquent executes the SQL DELETE statement corresponding to the current scope. The method returns the number of affected rows, but it does not require the model to be hydrated first. This is why the code block using where succeeds even if the record doesn't exist; however, attempting to access $post->id immediately after a failed find() call will throw an error because $post is null.

Understanding Query Builder vs. Model Instances

To clarify the mechanics, consider how Eloquent processes these calls internally. The find() method is essentially a shorthand for:

return Post::where('id', $id)->first();

If no record matches the ID, first() returns null. Therefore, assigning this to $post means $post holds a null value. Any subsequent property access or method call on $post fails immediately.

On the other hand, Post::where('id', $id) constructs a query scope but stops short of execution or hydration unless you explicitly tell it to (e.g., via get(), first(), or chaining a mutation like delete()). This behavior is consistent with how the Query Builder works: it maintains the state of your query conditions until you decide to execute the action.

Practical Implications for Deletion

When implementing deletion logic, understanding this distinction prevents runtime errors and improves code clarity. If your goal is simply to remove a record by ID, using the builder chain is often more robust because it bypasses the hydration step entirely:

// This works regardless of whether the record exists or not
Post::where('id', $id)->delete();

This line executes the SQL DELETE statement immediately. If the row exists, it deletes it and returns the count (usually 1). If the row does not exist, the SQL engine handles the non-matching condition gracefully, returning 0 affected rows, but no exception is thrown.

However, if you need to inspect the model before deleting—for example, logging which post is being removed or validating permissions—you must hydrate the record first:

$post = Post::where('id', $id)->first();

if ($post) {
    // Now we can safely access properties and delete
    Log::info("Deleting post ID {$post->id}");
    $post->delete();
}

Attempting to use Post::find($id) in the scenario where you only intend to delete is technically valid but prone to failure if you mistakenly try to access $post properties before checking for null. The explicit where() chain ensures that the operation proceeds at the SQL level without forcing an object instantiation that might fail silently or crash your application.

For developers maintaining large codebases, recognizing that find() is a hydrating shortcut while where() returns a builder is essential for writing performant and error-free database interactions. If you need further guidance on optimizing Eloquent queries or handling complex relationships, resources like https://laravelcompany.com offer detailed documentation on best practices.

Our network