【发布时间】:2020-11-06 15:40:42
【问题描述】:
-- Laravel 8.* --
您好,我正在尝试在 api 中回复有关足球的数据,domain.test/api/fixtures/2020-11-06 应该提供一系列联赛以及给定日期的赛程。
此时我的 App\Models\League 带有一个过滤器,用于过滤固定装置 hasMany。
/**
* Get all the fixtures from this league
*/
public function fixtures()
{
return $this->hasMany('App\Models\Fixture', 'league_id', 'id');
}
在 FixturesController 中我用 whereHas 来调用它:
$leagues = League::with('fixtures')->whereHas('fixtures', function($q) use ($date) { $q->where('date', $date); })->get();
return $this->responseWithSuccess(['leagues' => $leagues ]);
但这是返回错误的答案。
"leagues": [
{
"id": 4,
"...",
"name": "Premier League",
"fixtures": [
{
"id": 62,
"...",
"date": "2020-10-17",
请帮忙! 我究竟做错了什么? 有人可以告诉我调试此类错误的最佳方法吗?
【问题讨论】:
标签: laravel eloquent laravel-8