Laravel merge relationships

前端 未结 4 610
深忆病人
深忆病人 2020-11-29 09:21

Is there a way to merge 2 relationships in laravel?

this is the way it\'s setup now, but Is there a way I could return both merged?

  public function         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 09:48

    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);
    }
    

提交回复
热议问题