php 如何使 Laravel Eloquent “IN” 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29115385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:14:54  来源:igfitidea点击:

How to Make Laravel Eloquent "IN" Query?

phplaravelormeloquent

提问by Ravi Jethva

I want to make query in Laravel Eloquent like here its raw MySQL query

我想在 Laravel Eloquent 中进行查询,就像这里的原始 MySQL 查询一样

SELECT  * from  exampleTbl where id in(1,2,3,4)

I have tried this in Laravel Eloquent but it's not working

我已经在 Laravel Eloquent 中尝试过这个,但它不起作用

DB::where("id IN(23,25)")->get()

回答by Raheel

Here is how you do in Eloquent

这是你在 Eloquent 中的做法

$users = User::whereIn('id', array(1, 2, 3))->get();

And if you are using Query builder then :

如果您使用的是查询构建器,则:

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();

回答by Nikunj K.

If you are using Query builder then you may use a blow

如果您使用的是查询构建器,那么您可能会使用一击

DB::table(Newsletter Subscription)
->select('*')
->whereIn('id', $send_users_list)
->get()

If you are working with Eloquent then you can use as below

如果你正在使用 Eloquent 那么你可以使用如下

$sendUsersList = Newsletter Subscription:: select ('*')
                ->whereIn('id', $send_users_list)
                ->get();

回答by srmilon

Syntax:

句法:

$data = Model::whereIn('field_name', [1, 2, 3])->get();

Use for UsersModel

用于用户模型

$usersList = Users::whereIn('id', [1, 2, 3])->get();