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

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Resolving "Call to undefined method Illuminate\Database\Query\Builder::user()" Error in Laravel Applications Body: It can be frustrating when you encounter a seemingly nonsensical error while coding along with a tutorial or working on your own application, especially if you're confident that your code matches the video or documentation. This issue occurs when calling a non-existing method in a particular class, and in this context, it seems to be an instance of Laravel's Eloquent ORM query builder incorrectly using a user() method. Let us break down the problem you are facing, which is likely due to how you have set up your relationship between card models and users on both ends. According to the code snippet you provided:
public function show(Card $card)
{
    $card = Card::with('notes.user')->get();
    return view('cards.show', compact('card'));
}
Here, you are attempting to eager load notes (which are related to cards via a 'notes' table) and their user data using the 'user()' method within the Card model Eloquent query. It looks like you have set up the relationship in your models correctly, according to Laravel From Scratch: Updating Records and Eager Loading tutorial. Nevertheless, it seems that you might have missed a crucial step when running migrations or setting up foreign keys between the tables, causing this error. In a Laravel application, Eloquent models can be associated with each other using relationships such as hasMany(), belongsTo(), and belongsToMany(). To successfully connect your Card model to users via user_id association, you need to include the following steps in your migration file: 1. Create the foreign key for the 'user_id' column:
Schema::table('users', function(Blueprint $table) {
        $table->integer('card_id')->unsigned();
        $table->foreign('card_id')->references('id')->on('cards')->onDelete('cascade');
    });
This code snippet adds a foreign key ('card_id') to the 'users' table, referencing the primary key column in the 'cards' table. Moreover, it ensures that cards are deleted when their associated users get removed (cascade deletion). 2. Add the relationship within your Card model:
class Card extends Model {
    public function notes()
    {
        return $this->hasMany(Note::class);
    }

    public function user() // This is the missing relationship in our issue example
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
In this step, you create a belongsTo relationship between your Card model and User model using the user_id foreign key column. 3. Add a similar relationship within your User model:
class User extends Model {
    public function cards()
    {
        return $this->hasMany(Card::class);
    }
}
This is the companion relationship, indicating that each user can have multiple card models associated with them. After completing these steps, you can use the 'user()' method in your Card model as intended within your show action:
public function show(Card $card)
{
    $card = Card::with('notes', 'user')->get();
    return view('cards.show', compact('card'));
}
Ensuring that the foreign key, relationships, and migrations are setup correctly will help you avoid errors like "Call to undefined method Illuminate\Database\Query\Builder::user()" in your Laravel application's code. Remember always to check your database queries against your application code to confirm the correct structure of relationships as per the Laravel documentation. Remember that when working with relationships, it is essential to maintain a clear understanding of the data model and its connections. Always consult official resources from Laravel Company [1] to ensure proper setup for any given application. By following these guidelines and staying organized, you can avoid such errors and focus on building robust applications. [1]: https://laravelcompany.com