Laravel - Call to undefined method Illuminate\Database\Query\Builder::user()

Laravel - Call to undefined method Illuminate\Database\Query\Builder::user()

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Cost of Returning Model Instances in Laravel Controllers

When debugging a BadMethodCallException for an undefined method like user(), it is tempting to assume the relationship isn't defined or the migration hasn't run. However, the root cause often lies in how you are interacting with the query builder versus the model instance itself. The error message explicitly states Illuminate\Database\Query\Builder::user() is undefined. This tells us that the code is calling a method on the raw Query Builder object rather than a Model instance.

In the context of the example provided, the issue resides in the show method of the controller. The line $card = Card::with('notes.user')->get(); executes a query and returns an array of data. When you assign this result directly to the variable $card, you are storing an array inside a variable that expects a single model instance for the subsequent return $card; statement. While this might work depending on how the view is rendered, it fundamentally breaks the expectation of returning a resource or a single entity from a controller method.

More critically, if you attempt to access properties or relationships on an array as if it were a model, Laravel cannot resolve those requests. The user() method belongs to the User model class, not the Builder. To access related data, you must retrieve the relationship through the specific model instance returned by the query.

Consider this corrected implementation for the controller:

public function show(Card $card)
{
    // Execute eager loading and store the result in a temporary variable
    $loadedCard = Card::with('notes.user')->find($card->id);
    
    // If you need to return a JSON response or a view with the model
    return $loadedCard; 
}

Notice the use of find() instead of all(). The show method is typically invoked via a route that accepts an ID (e.g., /cards/5). When using dependency injection to pass $card, you are receiving a resolved instance, but if you rely on querying by ID inside the method, find() ensures you get a single model instance, not an array. If you use all(), you get a collection. You cannot call methods on a collection that belong to a single model unless you iterate over them first.

The confusion often stems from mixing up Query Builder calls with Model instance calls. The with method is called on the query builder to optimize database hits by fetching related data in a single JOIN or separate queries. However, the actual relationship accessor (like $card->user) only becomes available after the query executes and populates the model's attributes.

Another common pitfall occurs when developers try to access the eager-loaded relationship immediately after get() without ensuring they are working with the correct scope. If you want to display the user associated with a note on a card, you should not call $card->user(). Instead, you must traverse the relationship chain defined in your models: $card->notes returns a collection of notes, and each note has a user property. You might access it like this:

$note = $card->notes->first();
$user = $note ? $note->user : null;

This pattern ensures you are accessing the relationship on the model instance, not trying to invoke a method on the builder object itself. Always remember that with() prepares the data for retrieval, but the actual relationship methods are properties of the Model class. If your migration created the users table and defined the relationships correctly in the Card and Note models, the user() method will be available once you are interacting with the hydrated model instances returned by Eloquent.

Our network