How to Make Laravel Eloquent “IN” Query?

前端 未结 4 1120
迷失自我
迷失自我 2020-12-25 09:28

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

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

I have tried this i

相关标签:
4条回答
  • 2020-12-25 09:39

    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

    $sendUsersList = Newsletter Subscription:: select ('*')
                    ->whereIn('id', $send_users_list)
                    ->get();
    
    0 讨论(0)
  • 2020-12-25 09:39

    Syntax:

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

    Use for Users Model

    $usersList = Users::whereIn('id', [1, 2, 3])->get();
    
    0 讨论(0)
  • 2020-12-25 09:41

    As @Raheel Answered it will fine but if you are working with Laravel 7/6

    Then Eloquent whereIn Query.

    Example1:

    $users = User::wherein('id',[1,2,3])->get();
    

    Example2:

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

    Example3:

    $ids = [1,2,3];
    
    $users = User::wherein('id',$ids)->get();
    
    0 讨论(0)
  • 2020-12-25 09:55

    Here is how you do in 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();
    
    0 讨论(0)
提交回复
热议问题