Select 来自数据透视表 table 的所有行
Select all rows from pivot table
我在 Slim Framework 上使用 Eloquent 选择数据透视表 table 行时遇到了一些问题。我有以下关系:
tb_bet (id, ...)
tb_fixture (id, ...)
tb_bet_fixture (bet_id, fixture_id, status)
型号:
Bet.php:
use \Illuminate\Database\Eloquent\Model;
class Bet extends Model {
protected $connection = 'client';
protected $table = 'tb_bet';
public $timestamps = false;
public function fixtures() {
return $this->belongsToMany('Fixture')->withPivot('status');
}
}
Fixture.php:
use \Illuminate\Database\Eloquent\Model;
class Fixture extends Model {
protected $connection = 'client';
protected $table = 'tb_fixture';
public $timestamps = false;
public function bets() {
return $this->belongsToMany('Bet')->withPivot('status');
}
}
我可以查询一个赛程的所有投注和一个投注的所有赛程,但我真正需要的是这样做:select * from tb_bet_fixture where bet_id = 1
。所以我有一个给定投注的所有赛程状态列表。有点像这样:
bet:{
'id': 1,
'pivot': [
{bet_id: 1, fixture_id: 2, status: true},
{bet_id: 1, fixture_id: 3, status: false},
{bet_id: 1, fixture_id: 4, status: true}
]
}
我想使用原始查询,但每当我尝试在我的控制器上声明 use Illuminate\Database\Capsule\Manager as DB;
并使用 $users = DB::select('select * from tb_users');
作为测试时,我都会收到 Database [default] not configured.
错误,我无法理解如何访问包含当前连接(是的,我使用到不同数据库的多个连接)并在中间件上定义的“$capsule”变量。无论如何,我将不胜感激。
选项 1:创建模型
如果我使用枢轴作为模型。我会为 pivot table 创建一个模型。这样就可以直接查询table了。像这样。
use \Illuminate\Database\Eloquent\Model;
use App\Bet;
class BetFixture extends Model
{
protected $table =‘tb_bet_fixture’;
public function bets()
{
return $this->hasMany(Bet::class);
}
}
这比通过像这样 select 直接从枢轴查询
更容易、更清晰
select * from tb_bet_fixture where bet_id = 1.
选项 2:Select 使用夹具,特定投注
如果您想select通过赛程进行特定投注,您可以这样查询。
Fixture::with(‘bets’ => function($query) use ($betId) {
$query->where(‘tb_bet_fixture.bet_id,$betId);
});
我在 Slim Framework 上使用 Eloquent 选择数据透视表 table 行时遇到了一些问题。我有以下关系:
tb_bet (id, ...)
tb_fixture (id, ...)
tb_bet_fixture (bet_id, fixture_id, status)
型号:
Bet.php:
use \Illuminate\Database\Eloquent\Model;
class Bet extends Model {
protected $connection = 'client';
protected $table = 'tb_bet';
public $timestamps = false;
public function fixtures() {
return $this->belongsToMany('Fixture')->withPivot('status');
}
}
Fixture.php:
use \Illuminate\Database\Eloquent\Model;
class Fixture extends Model {
protected $connection = 'client';
protected $table = 'tb_fixture';
public $timestamps = false;
public function bets() {
return $this->belongsToMany('Bet')->withPivot('status');
}
}
我可以查询一个赛程的所有投注和一个投注的所有赛程,但我真正需要的是这样做:select * from tb_bet_fixture where bet_id = 1
。所以我有一个给定投注的所有赛程状态列表。有点像这样:
bet:{
'id': 1,
'pivot': [
{bet_id: 1, fixture_id: 2, status: true},
{bet_id: 1, fixture_id: 3, status: false},
{bet_id: 1, fixture_id: 4, status: true}
]
}
我想使用原始查询,但每当我尝试在我的控制器上声明 use Illuminate\Database\Capsule\Manager as DB;
并使用 $users = DB::select('select * from tb_users');
作为测试时,我都会收到 Database [default] not configured.
错误,我无法理解如何访问包含当前连接(是的,我使用到不同数据库的多个连接)并在中间件上定义的“$capsule”变量。无论如何,我将不胜感激。
选项 1:创建模型
如果我使用枢轴作为模型。我会为 pivot table 创建一个模型。这样就可以直接查询table了。像这样。
use \Illuminate\Database\Eloquent\Model;
use App\Bet;
class BetFixture extends Model
{
protected $table =‘tb_bet_fixture’;
public function bets()
{
return $this->hasMany(Bet::class);
}
}
这比通过像这样 select 直接从枢轴查询
更容易、更清晰select * from tb_bet_fixture where bet_id = 1.
选项 2:Select 使用夹具,特定投注
如果您想select通过赛程进行特定投注,您可以这样查询。
Fixture::with(‘bets’ => function($query) use ($betId) {
$query->where(‘tb_bet_fixture.bet_id,$betId);
});