Вот моя схема базы данных

enter image description here

А у меня есть такие модели:

  • Администратор
  • Пользователь
  • Держать пари
  • Совпадение
  • Команда

Я не понимаю, как определить отношение между matches и teams в моделях

вот что я делал до сих пор ...

User.php

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

Bet.php

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

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

Match.php

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

//?????????????

< Сильный > Team.php

//?????????????

//???... Team.php Match.php
$team->matches();
$match->team1();
$match->team2();


Благодарность

0
bobD 6 Сен 2016 в 15:27

3 ответа

Лучший ответ

Должно получиться примерно так:

Match.php

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

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

< Сильный > Team.php

public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}
2
Mihai Matei 6 Сен 2016 в 13:10

Вы можете указать, какой столбец должен быть выбран для каждой связи:

public function team1() {
        return $this->belongsTo('\App\Match', 'team1_id');
    }
 public function team2() {
        return $this->belongsTo('\App\Match', 'team2_id');
    }

Позвольте мне знать, если это помогает.

0
J3Rik0 6 Сен 2016 в 12:33

Было бы что-то вроде этого. Попробуйте.

  1. Match.php

    public function team1(){
      return $this->belongsTo('App\Team', 'team1_id');
    }
    
    public function team2(){
      return $this->belongsTo('App\Team', 'team2_id');
    }
    
  2. Team.php

    public function matches1(){
      return $this->hasMany('App\Match', 'team1_id', 'id');
    }
    
    public function matches2(){
      return $this->hasMany('App\Match', 'team2_id', 'id');
    }
    
0
Abdullah Al Shakib 6 Сен 2016 в 13:00