gpt4 book ai didi

mysql - Eloquent 中的 IN 语句

转载 作者:行者123 更新时间:2023-11-30 22:58:38 26 4
gpt4 key购买 nike

我正在尝试在 Eloquent 查询中执行 IN 语句。我试过这个:

$id = urldecode($id);

$news = News::whereHas('newsCategories', function($q) use($id)
{
$q->where('category_id', 'IN', '('.$id.')')->orderBy('created_at', 'desc');
})->get();

但我得到的结果是空的。

当我将 IN 切换为 = 并传递一个数字而不是数组时,我得到了结果。我做错了什么吗?

所以这是可行的:

$id = 2;

$news = News::whereHas('newsCategories', function($q) use($id)
{
$q->where('category_id', '=', $id)->orderBy('created_at', 'desc');
})->get();

最佳答案

你想在 whereIn 中检查你的 id 数组......

$news = News::whereHas('newsCategories', function($q) use($array_of_ids)
{
$q->whereIn('category_id', $array_of_ids)
->orderBy('created_at', 'desc');
})->get();

只需确保您传递的是真正的数组 :)

关于mysql - Eloquent 中的 IN 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090638/

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