Clinton Green Clinton Green - 1 year ago 278
PHP Question

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

I am busy with Laravel From Scratch: Updating Records and Eager Loading. I have followed the tut but I am getting this error when trying to add user data in CardsController. I am assuming that I've missed a step in the card user relationship somewhere but I've watched the video 3 times and my database queries for User, Card & Note matches the video exactly.

Is there another step I need to perform after creating the Users table via the migration perhaps?


BadMethodCallException in Builder.php line 2345:

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

CardsController code

namespace App\Http\Controllers;
use App\Card;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CardsController extends Controller
public function index()
$cards = Card::all();
return view('cards.index', compact('cards'));

public function show(Card $card)
$card = Card::with('notes.user')->get();
return $card;
return view('', compact('card'));

Answer Source

Your note model is lacking the relationship definition for it's associated user, it looks like.

You should just be able to add the relationship in the Notes model like this:

public function user()
    return $this->belongsTo(User::class);