Laravel 5.7 Связь один ко многим столбцам

у меня есть команда и таблица матчей. Вся информация о матче сохраняется в таблице матчей с двумя командами, командой-победителем, временем матча и т. д.

в таблице матчей у меня есть три поля team_1, team_2, winner_team, я хочу связать эти поля с таблицей команд

Вот мой код

Модель команды

class Team extends Model
{
    public function league()
    {
        return $this->belongsTo('App\League');
    }



      public function matchs()
        {
            return $this->hasMany('App\Match');
        }

    }

Соответствие модели

class Match extends Model
{
    public function team_1()
    {
        return $this->belongsTo('App\Team','id');
    }

    public function team_2()
    {
        return $this->belongsTo('App\Team','team_2');
    }

    public function winner()
    {
        return $this->belongsTo('App\Team','winner');
    }

    public function league()
    {
        return $this->belongsTo('App\League');
    }

    public function teams() {
        return $this->team_1()->merge($this->team_2());
    }
}

Файл переноса

Schema::create('matches', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('team_1')->unsigned()->index();
            $table->integer('team_2')->unsigned()->index();
            $table->integer('league_id')->unsigned()->index();
            $table->integer('winner')->nullable();
            $table->dateTime('match_time');
            $table->timestamps();

            $table->foreign('team_1')->references('id')->on('teams')
                ->onDelete('restrict')
                ->onUpdate('cascade');

            $table->foreign('team_2')->references('id')->on('teams')
                ->onDelete('restrict')
                ->onUpdate('cascade');

            $table->foreign('league_id')->references('id')->on('leagues')
                ->onDelete('restrict')
                ->onUpdate('cascade');

            $table->foreign('winner')->references('id')->on('teams')
                ->onDelete('restrict')
                ->onUpdate('cascade');
        });

person AH Rasel    schedule 09.01.2019    source источник


Ответы (1)


person    schedule
comment
какая модель содержит эти отношения? - person AH Rasel; 09.01.2019
comment
Модель On Match содержит это отношение - person Raúl Monge; 09.01.2019