有没有办法在laravel中合并2个关系?
这就是它现在设置的方式,但是有没有办法可以返回两者合并?
public function CompetitionsHome() {
return $this->HasMany( 'Competition', 'home_team_id' );
}
public function CompetitionsGuest() {
return $this->HasMany( 'Competition', 'guest_team_id' );
}
public function Competitions() {
// return both CompetitionsHome & CompetitionsGuest
}
答案 0 :(得分:20)
尝试使用getter方法获取从关系返回的合并集合的属性。
public function getCompetitionsAttribute($value)
{
// There two calls return collections
// as defined in relations.
$competitionsHome = $this->competitionsHome;
$competitionsGuest = $this->competitionsGuest;
// Merge collections and return single collection.
return $competitionsHome->merge($competitionsGuest);
}
或者,您可以在返回集合之前调用其他方法以获取不同的结果集。
public function getCompetitionsAttribute($value)
{
// There two calls return collections
// as defined in relations.
// `latest()` method is shorthand for `orderBy('created_at', 'desc')`
// method call.
$competitionsHome = $this->competitionsHome()->latest()->get();
$competitionsGuest = $this->competitionsGuest()->latest()->get();
// Merge collections and return single collection.
return $competitionsHome->merge($competitionsGuest);
}
答案 1 :(得分:5)
如果您更喜欢使用merge()方法来组合两个集合(关系),它将覆盖具有相同索引键的元素,因此您将丢失从一个关系中获得的一些数据。
您应该选择push()方法,通过将一个集合推送到其他集合的末尾来创建新的数组键
以下是一个示例:
public function getCompetitionsAttribute($value) {
$competitionsHome = $this->competitionsHome;
$competitionsGuest = $this->competitionsGuest;
// PUSH ONE TO OTHER!
return $competitionsHome->push($competitionsGuest);
}
答案 2 :(得分:0)
如果有人由于谷歌而像我一样登陆这里:由于merge()和push()都不允许快速加载(以及其他不错的关系功能),因此讨论并未以该主题结束,而是在此处继续:{{3 }}
我提出了一种解决方案Laravel Eloquent Inner Join on Self Referencing Table,欢迎您提出任何其他想法和建议。
答案 3 :(得分:0)
我创建了一个用于使用视图合并关系的程序包:
https://github.com/staudenmeir/laravel-merged-relations
首先,在迁移中创建合并视图:
use Staudenmeir\LaravelMergedRelations\Facades\Schema;
Schema::createMergeView(
'competitions',
[(new YourModel)->CompetitionsHome(), (new YourModel)->CompetitionsGuest()]
);
然后定义关系:
class YourModel extends Model
{
use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;
public function competitions()
{
return $this->mergedRelationWithModel(Competition::class, 'competitions');
}
}
像其他任何关系一样使用它:
$model->competitions;
$model->competitions()->paginate();
YourModel::with('competitions')->get();
答案 4 :(得分:0)
我为 Laravel 创建了一个包,它允许您创建一个合并多个 hasMany 关系的关系,支持预先加载(->with(...)
)并且您不需要创建视图。
GitHub 上的软件包:https://github.com/korridor/laravel-has-many-merged
您的问题将通过以下方式解决:
use Korridor\LaravelHasManyMerged\HasManyMergedRelation;
class Example extends Model
{
use HasManyMergedRelation;
// ...
public function Competitions() {
return $this->hasManyMerged('Competition', ['home_team_id', 'guest_team_id']);
}
}
答案 5 :(得分:0)
在 Laravel 8 中你可以这样做:
public function Competitions() {
$home= $this->CompetitionsHome();
$guest => $this->CompetitionsGuest();
$all = $home->union($guest)
->orderBy('created_at')
->get();
}