Closed ghost closed 5 years ago
OK I think I found the problem ; for any reason, we decided to have in our Users table the primary key id_user
not id
. I fixed the issue by modifying:
Traits\Favoriteable
public function favoritedBy()
{
return $this->favorites()->with('user')->get()->mapWithKeys(function ($item) {
return [$item['user']->id_user => $item['user']];
});
}
Models\Favorite
public function user()
{
return $this->belongsTo(Config::get('auth.providers.users.model'), 'user_id');
}
So it won't work correctly if we change the default primary key for the Users Model, despite the declaration of protected $primaryKey = 'id_user';
into Users model
Ok, im glad everything is fixed now, ill check it out and see if I can implement something to take the declared primary key instead of the id directly
Detailed description
It worked on last version, but since I upgraded Laravel to 5.8 I have this error when I try to get a collection of users that have "favorited" an item.
Context
My Favoriteable model is links (user can favourite any link of the application). My (very simple) Links model:
My Users model:
In a test controller, I want to
Yes link 22 does exist in database :) The error is on
$link->favoritedBy();
then onThe following are working correctly:
Will generate
select count(*) as aggregate from [favorites] where [favorites].[favoriteable_id] = 22 and [favorites].[favoriteable_id] is not null and [favorites].[favoriteable_type] = 'App\Models\Users\Links'
as expectedWill generate
select top 1 1 [exists] from [favorites] where [favorites].[favoriteable_id] = 22 and [favorites].[favoriteable_id] is not null and [favorites].[favoriteable_type] = 'App\Models\Users\Links' and [user_id] = 84
andinsert into [favorites] ([user_id], [favoriteable_id], [favoriteable_type], [updated_at], [created_at]) values (84, 22, 'App\Models\Users\Links', '2019-03-19 16:41:19.984', '2019-03-19 16:41:19.984')
as expected (same for removing)Possible implementation
-
Your environment