Example: I want to get the user model instead of the user ID. I will use that accesor in a user pagination page too. The laravel debug bar is not showing the query(s) from the accesor and i'm not sure if should use that accesor or not.
I want to get the user model instead of the user ID
You will want to be careful about messing with the identity...
There is nothing to execute the mutator unless the property is called on the object, or you are appending auomatically whwnever you represent the model as an array.
Seems to me to be a use case for relationships rather than accessor attributes:
// Conversation Model
public function userOne()
{
return $this->belongsTo(User::class, 'user_one');
}
public function userTwo()
{
return $this->belongsTo(User::class, 'user_one');
}
// bonus scope
public function scopeWithParticipant(Builder $builder, User $user)
{
return $builder->where('user_one', $user->id)
->orWhere('user_two', $user->id);
}