Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Relationship join on pivot #49

Open
saibotk opened this issue Apr 4, 2021 · 5 comments
Open

Feature request: Relationship join on pivot #49

saibotk opened this issue Apr 4, 2021 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@saibotk
Copy link

saibotk commented Apr 4, 2021

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

@luisdalmolin
Copy link
Member

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

@luisdalmolin luisdalmolin added enhancement New feature or request good first issue Good for newcomers labels May 8, 2021
@beshoo
Copy link

beshoo commented Dec 20, 2022

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.

@luisdalmolin
Copy link
Member

Thanks @beshoo

@brunolopesr
Copy link
Contributor

brunolopesr commented Jun 15, 2023

@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?

@luisdalmolin
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants