eloquent-power-joins
eloquent-power-joins copied to clipboard
Feature request: Relationship join on pivot
First of all: Thank you for the awesome package and happy easter :rabbit: !
In our current project, we are using the relationshipJoins with various many-to-many relations, but do not really care about the data on the relationship table and instead only need to join the pivot table. So I wanted to suggest adding a function, which does the same as relationshipJoin, which already does an inner join on the pivot, but does not also join the table of the related model.
Thanks
@saibotk Seems like a valid feature request. I'll take a look at this when I have some time, probably next weekend.
First of all: Thank you for the awesome package and happy easter 🐰 !
In our current project, we are using the relationshipJoins with various many-to-many relations, but do not really care about the data on the relationship table and instead only need to join the pivot table. So I wanted to suggest adding a function, which does the same as relationshipJoin, which already does an inner join on the pivot, but does not also join the table of the related model.
Thanks
Here's an example of how you can modify the relationshipJoin function to only join the pivot table and not the related model's table:
public function modifiedRelationshipJoin($relation, $table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
{
if ($relation instanceof BelongsToMany) {
$related = $relation->getRelated();
$table = $relation->getTable();
// Modify the join clause to only join the pivot table
$this->join($table, $first, $operator, $second, $type, $where);
$this->addBinding($relation->getBindings(), 'join');
}
return $this;
}
This function works similarly to the relationshipJoin function, but instead of joining both the pivot table and the related model's table, it only joins the pivot table. You can use it like this:
$query->modifiedRelationshipJoin($relation, $table, $first, $operator, $second, $type, $where);
I hope this helps! Let me know if you have any questions.
Thanks @beshoo
@luisdalmolin
What do you think is the best way to achieve this? A extra method joinRelationshipPivot (or something like that) or a extra parameter to the joinRelationship method? Something like a $pivotOnly check?
@brunolopesr I thought about the API for a while and couldn't come up with something that I liked, so not quite sure. It would be nice to do something like this:
Post::joinRelationship('images', fn ($join) => $join->onlyPivot());