cmgmyr / laravel-messenger

Simple user messaging package for Laravel
MIT License
2.45k stars 514 forks source link

Call to undefined method User::addEagerConstraints() #342

Closed diegomagikal closed 4 years ago

diegomagikal commented 4 years ago

Using Laravel v7.X laravel-messenger v2.20

Hello,

I get this error when using the creator relationship from Thread model:

Call to undefined method User::addEagerConstraints()

I'm trying to get the creator id in show.blade.php, adding the following code in MessagesController:

$thread = Thread::with('creator')->findOrFail($id);

cmgmyr commented 4 years ago

Hi @diegomagikal, The reason for this is because creator() isn't a relationship on the Thread model, but just a getter method. So you'll have to get the thread first, then call for the creator.

$thread = Thread::findOrFail($id);
$creator = $thread->creator();

if you'd like to eager load the users, you can do that with

$thread = Thread::with('participants.user')->findOrFail($id);