gpt4 book ai didi

php - Laravel 限制急切加载不起作用

转载 作者:行者123 更新时间:2023-11-29 10:44:37 25 4
gpt4 key购买 nike

我正在查询,我想检查我已经设置的关系,如下所示:

内容模型:

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,它的contentType为slug post,我正在从以下位置获取内容表,但经过测试后,无论我将什么内容作为 contentTypeslug,我每次都会得到相同的记录。我在数据库中只有一条记录,这就是为什么我得到相同的记录,但如果它没有包含 slug< 的 contentType ,我什至应该得到该记录post 不同。我做错了什么?

最佳答案

我能够通过 whereHas 查询实现我想要的目标:

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

这样,我只获取在 contentType 关系中将帖子或页面作为 slug 的内容。

关于php - Laravel 限制急切加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44860981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com