-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
hasManyDeepFromRelation with table aliases #40
Comments
What do your individual relationships look like? |
This is essentially what I'm doing: class User extends Model
{
public function lowerLevels()
{
return $this->belongsToMany(LowerLevel::class);
}
public function allLowerLevelsBelongingToAllLowerLevelsUpperLevels()
{
return $this->hasManyDeepFromRelations($this->lowerLevels(), (new LowerLevel)->upperLevel(), (new UpperLevel)->lowerLevels());
}
}
class LowerLevel extends Model
{
public function upperLevel()
{
return $this->belongsTo(UpperLevel::class);
}
}
class UpperLevel extends Model
{
public function lowerLevels()
{
return $this->hasMany(LowerLevel::class);
}
} |
You need to define a "real" class User extends Model
{
public function allLowerLevelsBelongingToAllLowerLevelsUpperLevels()
{
return $this->hasManyDeep(
LowerLevel::class,
['lower_level_user', LowerLevel::class.' as alias', UpperLevel::class],
[null, null, 'id'],
[null, null, 'upper_level_id']
);
}
}
class LowerLevel extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasTableAlias;
} |
That does indeed work, thanks :) However I find it much harder to work out how to construct the relationship that way. I feel like the Having said that, is there any way something like the following could be made to work: $lowerLevel = new LowerLevel;
$lowerLevel->setTable($lowerLevel->getTable() . ' as table_alias');
$this->hasManyDeepFromRelations($this->lowerLevels(), $lowerLevel->upperLevel(), (new UpperLevel)->lowerLevels()); or as as an additional method in the $this->hasManyDeepFromRelations($this->lowerLevels(), (new LowerLevel)->alias('table_alias')->upperLevel(), (new UpperLevel)->lowerLevels()); and |
Yeah, that's not ideal. I've looked into alternatives like automatic aliasing of duplicate tables, but haven't found a good solution yet. I'll try again and see if I can make your suggestions work. |
I've released a new version that adds the return $this->hasManyDeepFromRelations(
$this->lowerLevels(),
(new LowerLevel)->setAlias('table_alias')->upperLevel(),
(new UpperLevel)->lowerLevels()
); |
This was already brought up in #22, but that ticket went in a different direction. Can table aliases be used with
hasManyDeepFromRelations
?The text was updated successfully, but these errors were encountered: