Laravel merge relationships
In case you prefer merge() method to combine two collections (relationships), it will override elements with the same index keys so you will loose some of your data gained from one relationship.
You should choose push() method instead, which creates new array keys by pushing one collection to the end of other collection
Here is a sample :
public function getCompetitionsAttribute($value) {
$competitionsHome = $this->competitionsHome;
$competitionsGuest = $this->competitionsGuest;
// PUSH ONE TO OTHER!
return $competitionsHome->push($competitionsGuest);
}
Try out getter method for property which returns merged collections returned from relations.
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);
}
Or you can call additional methods before collection is returned to get different result sets.
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);
}