2
我有两个数据库表:Laravel:如果选择了雄辩的预先加载关系
帖子
$table->increments('id');
$table->integer('country_id')->unsigned();
$table->foreign('country_id')->references('id')->on('countries');
国家
$table->increments('id');
$table->string('name', 70);
我用laravel作为后端。现在我想为我的前端实现过滤数据。因此,用户可以选择一个国家名称,laravel应该仅回答具有指定名称的国家的帖子的请求。
如何将此条件添加到我现有的分页查询中?我尝试这样做:
$query = app(Post::class)->with('country')->newQuery();
// ...
if ($request->exists('country')) {
$query->where('country.name', $request->country);
}
// ...
...导致以下错误:
Column not found: 1054 Unknown column 'country.name' in 'where clause' (SQL: select count(*) as aggregate from `posts` where `country`.`name` = Albania)
给我:'FatalErrorException在PostController.php线77: 语法错误,意想不到 '=>'(T_DOUBLE_ARROW)'错误。 – HelloWorld0815
我已更新它。我犯了一个错误把箭头放在那里 – oseintow