eloquent-power-joins
eloquent-power-joins copied to clipboard
Nested joins with belongs to many relations
Hi,
I use
->JoinRelationship('groups.roles.permissions.module', $powerJoinClause);
with
$powerJoinClause = [
'permissions' => [
'permissions' => function ($join) use ($permissionName) {
$join->name($permissionName);
},
],
'module' => function ($join) use ($moduleName) {
$join->name($moduleName);
}
];
in my code. 'permission' is a belongs to many relation. This already worked. But with the newest version of eloquent-power-joins i get the error 'Array callback must have exactly two elements'. The error seems to occur in line 437 in the PowerJoins.php
if (is_array($callback) && isset($callback[$tableName])) {
$fakeJoinCallback = new FakeJoinCallback();
$callback[$tableName]($fakeJoinCallback);
if ($fakeJoinCallback->getAlias()) {
return $fakeJoinCallback->getAlias();
}
}
because $callback[$tableName] is an array in this case.
But if I change the line 138 in the PowerJoins.php file from
$alias = $this->getAliasName($useAlias, $relation, $relationName, $relation->getQuery()->getModel()->getTable(), $callback);
to
$alias = $this->getAliasName($useAlias, $relation, $relationName, $relation->getQuery()->getModel()->getTable(), $relationCallback);
(change $callback to $relationCallback) everything works as expected.
@wangarrius are you using the latest version? Just to make sure. I'm going to take a look at this if so
@luisdalmolin I am using version 2.6.4.
@luisdalmolin any update on this? can we consider it a bug?
To use the JoinRelationship method in a way that is compatible with the current version of the eloquent-power-joins package, you will need to make sure that the $powerJoinClause parameter is a closure with two parameters.
In your code, the $powerJoinClause parameter is currently an array, which is causing the error you're seeing. To fix this, you can modify your code to pass a closure to the JoinRelationship method instead of an array.
Here's an example of how you could modify your code to do this:
$powerJoinClause = function ($join, $relationship) use ($permissionName, $moduleName) {
if ($relationship === 'permissions') {
$join->where('permissions.name', '=', $permissionName);
}
if ($relationship === 'module') {
$join->where('module.name', '=', $moduleName);
}
};
$query->JoinRelationship('groups.roles.permissions.module', $powerJoinClause); In this example, the $powerJoinClause closure is passed two parameters: $join and $relationship. The $join parameter is an instance of the Illuminate\Database\Eloquent\Builder class, which you can use to add constraints to the join clause. The $relationship parameter is a string that represents the name of the relationship being joined.
In the closure, you can use an if statement to check the value of $relationship, and add constraints to the $join clause accordingly.
I hope this helps! Let me know if you have any questions or if you need further assistance.
Can you verify if this is still an issue with 3.0.0? Feel free to reopen if so