Laravel合并关系

Kiw*_*iwi 13 php relationships eloquent laravel-4

有没有办法合并laravel中的两个关系?

这就是它现在设置的方式,但是有没有办法让两者合并?

  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
  }
Run Code Online (Sandbox Code Playgroud)

And*_*yco 23

尝试使用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);
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以在返回集合之前调用其他方法以获取不同的结果集.

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 尽管此方法有效,但应注意,它不会返回关系实例。因此,您无法执行类似App \ User :: find(1)-> with('competitions')`的操作,但是可以执行App \ User :: find(1)-> with('competitions_guest') `(除非出现语法错误) (2认同)
  • @daraul 是否有可能如何合并关系并在查询构建器中使用它们? (2认同)

spe*_*naz 5

如果您更喜欢使用merge()方法来组合两个集合(关系),它将覆盖具有相同索引键的元素,因此您将丢失从一个关系中获得的一些数据.

您应该选择push()方法,通过将一个集合推送到其他集合的末尾来创建新的数组键

这是一个示例:

public function getCompetitionsAttribute($value) {
    $competitionsHome = $this->competitionsHome;
    $competitionsGuest = $this->competitionsGuest;

    // PUSH ONE TO OTHER!
    return $competitionsHome->push($competitionsGuest);
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*eir 5

我创建了一个使用视图合并关系的包:
https: //github.com/staudenmeir/laravel-merged-relations

首先,在迁移中创建合并视图:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createMergeView(
    'competitions',
    [(new YourModel)->CompetitionsHome(), (new YourModel)->CompetitionsGuest()]
);
Run Code Online (Sandbox Code Playgroud)

然后定义关系:

class YourModel extends Model
{
    use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;

    public function competitions()
    {
        return $this->mergedRelationWithModel(Competition::class, 'competitions');
    }
}
Run Code Online (Sandbox Code Playgroud)

像任何其他关系一样使用它:

$model->competitions;

$model->competitions()->paginate();

YourModel::with('competitions')->get();
Run Code Online (Sandbox Code Playgroud)


kor*_*dor 5

我为 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']);
  }

}
Run Code Online (Sandbox Code Playgroud)