也许您可以做类似的事情:
我已经开始处理的现有Laravel项目中具有以下数据库表设置:
sports
id
name
lines
id
sport_id - Foreign Key (lines_sport_id_foreign)
bet_id - Foreign Key (lines_bet_id_foreign)
bets
id
stake
[建立了这样的关系:
Sport.php
public function lines()
{
return $this->hasMany('App\Line');
}
Line.php
public function bet()
{
return $this->belongsTo('App\Bet');
}
public function sport()
{
return $this->belongsTo('App\Sport');
}
Bet.php
public function lines()
{
return $this->hasMany('App\Line');
}
现在我需要能够在Sport
和Bet
之间建立关系。具体到
[我曾尝试在hasManyThrough
中使用Laravel的Sport
方法,]
public function bets() { return $this->hasManyThrough('App\Bet', 'App\Line'); }
但可以理解的是,我得到了回应
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bets.line_id' in 'on clause' (SQL: select `bets`.*, `lines`.`sport_id` as `laravel_through_key` from `bets` inner join `lines` on `lines`.`id` = `bets`.`line_id` where `lines`.`sport_id` = 1)
[如果要调用
App\Sport::find(1)->bets
,因为这希望lines
表上的bets
和bets
之间存在链接,而不是当前的lines
表(我无法更改现有结构)] >无论如何都可以按照
$sport->bets
的方式获得一项运动的所有下注
然后是第二个设置,然后将所有体育项目按赌注表上的赌注列排序
? (理想情况下,沿着$sport->bets->orderBy('stake', 'desc')
这可能吗?
我已经开始研究的现有Laravel项目中具有以下数据库表设置:体育ID名称行ID Sport_id-外键(lines_sport_id_foreign)...
也许您可以做类似的事情:
public function bets()
{
return Bet::where('id', $this->lines->pluck('bet_id'));
}
也许您可以做类似的事情: