[英]Eloquent whereIn with closure
I want to do a whereIn with a closure. 我想用闭包做一个whereIn。 Is this possible?
这可能吗?
Normally, you would go and create it like this 通常,您将像这样创建它
return User::whereIn('id', [1, 2, 3])
->whereLang($channel)
->get();
But, what I wanted to do, is to do it with a closure - if possible. 但是,我想做的就是尽可能地关闭它。 I have a database field, which has ids of users as a string (The table design is not mine, so I can't change it currently and have to work with it);
我有一个数据库字段,该字段以字符串形式包含用户ID(表设计不是我的,因此我目前无法更改它,必须使用它);
So I tried to do it like this 所以我尝试这样做
return User::whereIn('id', function () {
return FooBar::whereFooId('u' . $user->id)->get(['users'])->toArray();
})
->whereLang($lang)
->get();
However, when doing that, I'm getting the error 但是,这样做时出现错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1096 No tables used'
致命错误:消息为'SQLSTATE [HY000]的未捕获异常'PDOException':常规错误:1096未使用表'
I mean, in the database table I have the string with the ids stored like 1, 2, 3, 4
, so I would just need to do an explode(', ', $ids);
我的意思是,在数据库表中,我有一个字符串,其ID像
1, 2, 3, 4
一样存储,所以我只需要做一个explode(', ', $ids);
. 。 This would solve my problem, when I do it in two steps, I was just wondering if I can do it in one, with the closure.
这将解决我的问题,当我分两步进行操作时,我只是想知道是否可以通过闭合操作一并完成。 And if so, how?
如果是这样,怎么办?
You probably want to do. 您可能想做。
return User::whereIn('id', FooBar::lists('uid'));
Assuming the user ids are stored in uid field in FooBar
table 假设用户ID存储在
FooBar
表的uid字段中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.