我尝试在当前查询中使用select获取相关模型,但是相关模型返回了null
值。怎么了?
Ubuntu 18.04,PHP 7.1,Postgres 11,NGINX,Laravel 5.7
查询:
public function datesFind(Request $request)
{
$games = Game::where('date_time_start', '<=', $request->get('date'))->where('date_time_end', '>=', $request->get('date'))
->where('sport_id', $request->get('sport_id'))->with('gameLevel')->get(['id', 'name', 'sport_id', 'date_time_start', 'date_time_end']);
return $games;
}
结果:
[
{
"id": 135,
"name": "Новая игра",
"sport_id": 3,
"date_time_start": "2019-01-25 00:00:00",
"date_time_end": "2019-02-25 00:00:00",
"game_level": null
},
]
答案 0 :(得分:0)
您必须在字段列表中包括外键才能加载关系...
更改:
$games = Game::where('date_time_start', '<=', $request->get('date'))
->where('date_time_end', '>=', $request->get('date'))
->where('sport_id', $request->get('sport_id'))
->with('gameLevel')
->get([
'id',
'name',
'sport_id',
'date_time_start',
'date_time_end'
]);
收件人:
$games = Game::where('date_time_start', '<=', $request->get('date'))
->where('date_time_end', '>=', $request->get('date'))
->where('sport_id', $request->get('sport_id'))
->with('gameLevel')
->get([
'id',
'name',
'sport_id',
'date_time_start',
'date_time_end',
'game_level_id' // or replace with your foreign key col name...
]);