Closed emilebourquin closed 1 day ago
Hi @emilebourquin,
It's not possible to support the trait in deep relations like BelongsToThrough
.
Consider the example from the README:
$post->country()->is($country)
wouldn't work because the method can't know the value of users.country_id
, but that's the foreign key it needs to compare with $country->id
. It works for BelongsTo
relationships because there aren't any intermediate tables.
BelongsToThrough
could override is()
and execute a query to compare the related models, but I'm not sure that's a good idea. In the trait's PR, the author wrote that the whole point was that it doesn't require an additional query.
Thank you for considering it, Jonas, I knew you'd have better insight as to whether this was possible. Having the ->is() function is handy for testing, but is not a show-stopper for me. Thank you for your very useful library.
Is it possible to have this fantastic library support the ComparesRelatedModels trait, like BelongsTo does?
Dropping the ComparesRelatedModels trait into BelongsToThrough throws the fatal error:
Class Znck\Eloquent\Relations\BelongsToThrough contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Znck\Eloquent\Relations\BelongsToThrough::getParentKey, Znck\Eloquent\Relations\BelongsToThrough::getRelatedKeyFrom) in /var/www/html/vendor/staudenmeir/belongs-to-through/src/Relations/BelongsToThrough.php on line 22