Allow for MorphMap relationships inside traits
logan-jobzmall opened this issue · 3 comments
Hi,
It seems as though MorphMap relationships are not handled in the following methods on traits. I extended them in my own project, but I don't have time to be making pull requests. Here is the suggested fix (this could be simplified I am sure):
public function following($model = null)
{
$class = $model ?: $this->getMorphClass();
$relation = Relation::getMorphedModel($class);
// Account for standard classes being passed in
if (!isset($relation) && isset($model)){
$relation = $class;
$class = (new $model())->getMorphClass();
}
return $this->morphToMany($relation ?: $class , 'follower', 'followers', 'follower_id', 'followable_id')
->withPivot('followable_type')
->wherePivot('followable_type', $class)
->wherePivot('follower_type', $this->getMorphClass())
->withTimestamps();
}
More than anything, just wanted to let you know.
This was fixed in the 2.0.0
version :D
@rennokki - It is has been a while. It seems as though the fix implemented didn't fix certain scenarios (in which I had my isset for) - for instance $user->blocks($viewingUser)
causes a 500 exception:
message: "class 'user' not found" file: "/Users/logan/project/vendor/rennokki/befriended/src/Traits/CanBlock.php" function: "blocking" line: 39
Mainly this seems to be caused by this line's result being passed to blocking
method:
return ! is_null($this->blocking((new $model)->getMorphClass())->find($model->getKey()));
It is trying to take the morphclass passed in and run getMorphClass again on it which will fail:
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
I'm having the same issue