Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel 5.2中的一列上有2个外键_Php_Database_Laravel_Laravel 5.2_Database Relations - Fatal编程技术网

Php laravel 5.2中的一列上有2个外键

Php laravel 5.2中的一列上有2个外键,php,database,laravel,laravel-5.2,database-relations,Php,Database,Laravel,Laravel 5.2,Database Relations,这是我的数据库模式 我有这些模型: 管理员 使用者 打赌 匹配 团队 我不知道如何定义模型中的匹配和团队之间的关系 这是我到现在为止所做的 User.php public function bets() { return $this->hasMany('\App\Bet'); } public function bets() { return $this->hasMany('\App\Bet'); } //????????????? //?????????

这是我的数据库模式

我有这些模型:

  • 管理员
  • 使用者
  • 打赌
  • 匹配
  • 团队
我不知道如何定义模型中的
匹配
团队
之间的关系

这是我到现在为止所做的

User.php

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

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}
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');
}
public function bets()
{
    return $this->hasMany('\App\Bet');
}

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}
Team.php

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

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}

实际上,我需要的是应该在
Team.php
Match.php
中放置的代码,而不是
/?…
,这样我就可以轻松地做这些事情了

$team->matches();
$match->team1();
$match->team2();


谢谢

您可以指定每个关系的目标列:

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

让我知道这是否有帮助。

您可以指定每个关系的目标列:

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

如果有帮助,请告诉我。

应该是这样的:

Match.php

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

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}
Team.php

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

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}

应该是这样的:

Match.php

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

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}
Team.php

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

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

public function team2()
{
    return $this->belongsTo('\App\Team', 'team2_id');
}
public function matches()
{
    return $this->hasMany('\App\Match', 'team1_id')
                ->orWhere('team2_id', $this->id);
}

应该是这样的。试试看

  • 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 matches1(){
      return $this->hasMany('App\Match', 'team1_id', 'id');
    }
    
    public function matches2(){
      return $this->hasMany('App\Match', 'team2_id', 'id');
    }
    

  • 应该是这样的。试试看

  • 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 matches1(){
      return $this->hasMany('App\Match', 'team1_id', 'id');
    }
    
    public function matches2(){
      return $this->hasMany('App\Match', 'team2_id', 'id');
    }
    

  • 不工作…,我做了这个
    returndd($team->matches()->where('id','=',1)->get())这是找不到的错误基表或视图:1146表“football\u bet.match\u team”不存在只是编辑了。。你能再核实一下吗<代码>dd($team->matches()->find(1))
    不起作用…,我做了这个
    返回dd($team->matches()->其中('id','=',1)->get())这是找不到的错误基表或视图:1146表“football\u bet.match\u team”不存在只是编辑了。。你能再核实一下吗<代码>dd($team->matches()->find(1))
    工作很好,谢谢,但是我如何访问团队参与的所有比赛?无论是第一个团队还是第二个团队,这是加入数组结果的唯一方法吗?使用with()。示例:团队::with('matches1','matches2')。要了解更多信息,请检查这项工作,谢谢,但我如何访问球队参加的所有比赛?无论是第一个团队还是第二个团队,这是加入数组结果的唯一方法吗?使用with()。示例:团队::with('matches1','matches2')。要了解更多信息,请检查此项