Php Laravel约束急加载不起作用

Php Laravel约束急加载不起作用,php,mysql,laravel,Php,Mysql,Laravel,我正在进行一个查询,我想在其中检查我已设置的关系,如下所示: $content = Content::where('slug', $arguments['slug']) ->with(['contentType' => function ($query) { $query->where('slug', 'post'); }])->get(); return $cont

我正在进行一个查询,我想在其中检查我已设置的关系,如下所示:

$content = Content::where('slug', $arguments['slug'])
                ->with(['contentType' => function ($query) {
                    $query->where('slug', 'post');
                }])->get();

return $content;
内容模型:

public function contentType()
{
    return $this->belongsTo('App\ContentType', 'ct_id');
}
内容类型

public function contents()
{
    return $this->hasMany('App\Content', 'ct_id');
}
然后,我试着做这样一个查询:

$content = Content::where('slug', $arguments['slug'])
                ->with(['contentType' => function ($query) {
                    $query->where('slug', 'post');
                }])->get();

return $content;

我想从数据库中获取$content,它有一个名为slug post的contentType,我从表中获取内容,但是经过测试,无论我将什么作为contentType的slug,每次都会得到相同的记录。我在数据库中只有一条记录,这就是为什么我得到了相同的记录,但如果它没有与post不同的段塞的contentType,我甚至应该得到那条记录。我做错了什么?

我能够通过Where Has query实现我想要的:


这样,我只获取在contentType关系中以post或page作为段塞的内容。

我不完全理解您的问题,您正在从表内容中检索一条记录,这在contentType表中有一条关联的记录。您总是获取contentType的相同记录吗?我总是从content表中获取相同的记录,无论我将什么放为contentType