John Davidson

php - how to exclude some users data from query, laravel

0 comments
Message:


I have an query like:


$users = DB::table('users')->select('*')->where('status', '=', '1')->get();

Now I want to exclude some users from this query which are exists in 2nd table, like the table name is 'users_packages' and the structure is:


id     userid     package
1 85 $ 100
2 61 $ 200
3 99 $ 100
4 15 $ 300

Now the I want to get all users excluding the users against these user ids :'85, 61, 99, 15'.


How can we achieve this goal? Any special query?



My Answer:
>where('status', 'active')
->get();

This query will retrieve all users with a status of 'active'. If you want to exclude certain users from the query, you can use the `whereNotIn` method.

For example, if you want to exclude users with the IDs 1, 2, and 3 from the query, you can modify the query like this:

php
$users = DB::table('users')
->where('status', 'active')
->whereNotIn('id', [1, 2, 3])
->get();


This will retrieve all users with a status of 'active' except for those with the IDs 1, 2, and 3.

Rate this post

5 of 5 based on 8779 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us