eloquent-power-joins icon indicating copy to clipboard operation
eloquent-power-joins copied to clipboard

Feature request: Relationship join on pivot

Open saibotk opened this issue 4 years ago • 5 comments

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 avatar Apr 04 '21 21:04 saibotk

@saibotk Seems like a valid feature request. I'll take a look at this when I have some time, probably next weekend.

luisdalmolin avatar Apr 06 '21 17:04 luisdalmolin

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.

beshoo avatar Dec 20 '22 17:12 beshoo

Thanks @beshoo

luisdalmolin avatar Dec 26 '22 20:12 luisdalmolin

@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 avatar Jun 15 '23 11:06 brunolopesr

@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());

luisdalmolin avatar Jun 19 '23 19:06 luisdalmolin