I have two tables - the first one is called 'users' and the second one is called 'buy_courses'.
I am trying to select all users those user_name is not in buy_courses. I tried something like -
$users = DB::table('users')
->rightjoin('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
->get();
It returns all users, whose user_name is in 'buy_courses', when I am using '<>', then I'm getting all users. What should be the right query?
DB::table("users")->select('*')->whereNotIn('user_name',function($query) {
$query->select('user_name')->from('buy_courses');
})->get();
just join actually is inner join in Laravel so actually maybe also you can try:
DB::table('users')
->join('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
->get();
Try it using Eloquent:
$courseUserNames = BuyCourses::pluck('user_name')->all();
$users = User::whereNotIn('user_name', $courseUserNames)->select(...)->get();
Or if you prefer using DB query:
$courseUserNames = DB::table('buy_courses')->pluck('user_name')->all();
$users = DB::table('users')->whereNotIn('user_name', $courseUserNames)->select(...)->get();
just replace = with != , use function join
$users = DB::table('users')
->join(
'buy_courses',
function ($join)
{$join->on('users.user_name', '!=', 'buy_courses.user_name');}
)
->get();
You can use SQL's 'NOT IN'.
Example:
mysqli_query($con, "SELECT * FROM users WHERE user_name NOT IN (SELECT user_name FROM buy_courses)");
Related
I currently have two tables with relationships between the tables: a users table and a comments table.
What I am trying to do is select user.username where comments.location has the same value as user.location. How do I get the currently logged in users comment?
I've tried using Auth::id() within the leftJoin however the error message received is query undefined and also the below statement.
$query = DB::table('recommendation')
->select('users.username')
->leftJoin('users','recommendation.location', '=', 'users.location')
->leftJoin('users','recommendation.user_id', '=' , Auth::id())
->get();
The below statement prints all usernames which have the same location.
$query = DB::table('comments')
->select('users.username')
->leftJoin('users','comments.location', '=', 'users.location')
->get();
Perhaps you need a WHERE clause here:
$query = DB::table('recommendation')
->select('users.username')
->leftJoin('users','recommendation.location', '=', 'users.location')
->where('users.user_id', '=', Auth::id())
->get();
I am try
ing to get something like this
select * from `users`
inner join `settings`
on `users`.`id` = `settings`.`user_id`
and NOW() NOT BETWEEN quit_hour_start AND quit_hour_end
where `notification_key` != ''
and `device_type` = 'Android'
in eloquent. Does anyone try and get success to build this query in eloquent.
I know I can use \DB::select(DB::raw()); and get my result. But I want to use ie with Laravel eloquent method.
====== update comment for tried queries========
$androidUser = User::join('settings', function ($join) {
$join->on('users.id', '=', 'settings.user_id')
->where(DB::raw("'$currentTime' NOT BETWEEN quit_hour_start AND quit_hour_end"));
})
->where('notification_key', '!=', '')
->where('device_type' ,'=', 'Android')
->get();
$users = DB::table('users')
->whereNotBetween('votes', [1, 100]) // For one column
->whereRaw("? NOT BETWEEN quit_hour_start AND quit_hour_end", [$currentTime]) // Use whereRaw for two columns
->get();
https://laravel.com/docs/5.5/queries, or you can rewrite as to wheres
I am trying to make the following query in laravel:
SELECT a.name AS subname, a.area_id, b.name, u. id, u.lawyer_id,u.active_search,
FROM subarea a
LEFT JOIN user_subarea u ON u.subarea_id = a.id
AND u.user_id = ?
LEFT JOIN branch b ON a.area_id = b.id
The idea is to obtain the subareas and see if the search is activated by the user.
The user_subarea table might have a record that matches the id of the subarea table where the active_search is equal to 0 or 1. If it doesn't exist I would like the query to return null.
While I was able to achieve this in raw SQL when I try the same with eloquent in Laravel I am not returning any value. I have done the following:
$query = DB::table('subarea')
->join('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Any help will be much appreciated.
I found by myself the answer it was just to add a lefjoin in the first join. It is not in the laravel docs but works too.
$query = DB::table('subarea')
->lefjoin('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Try this one, If you get a problem, please comment.
$value = \Auth::user()->id;
$query = DB::table('subarea')
->where('user_subarea.user_id', '=',$value)
->leftJoin('user_subarea', 'subarea.id', '=', 'user_subarea.subarea_id')
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('subarea.name AS subname','subarea.area_id', 'branch.name', 'user_subarea.id','user_subarea.lawyer_id','user_subarea.active_search')
->get();
I have a sql query:
select `id` from `users`
where (
select count(*)
from `user_event` as `uev`
where `uev`.`leader_id` = `users`.`id`
) > 1
How can I convert it to Eloquent Laravel?
Assuming you have set up the relationship you can use the has() method for that:
$users = User::select('id')->has('events', '>', 1)->get();
If you want an array of users ids (since you're only selecting the id) you can also use lists():
$ids = User::has('events', '>', 1)->lists('id');
Since you asked, this would be an alternative method (not tested though)
User::where(DB::raw('1'), '<', function($q){
$q->from('user_event')
->where('user_event.leader_id', 'users.id');
})->get();
Hi I am trying to get the current count of my statement below , but I am getting only the count not the whole result:
$admins = DB::table('users')
->select(DB::raw('count(users.id) as admin_count'))
->where('users_roles.role_id', '=' ,0)
->join('users_roles', 'users.id', '=', 'users_roles.user_id')
->orderBy('first_name', 'asc')
->get();
Could you tell me what I am doing wrong?
Thanks
Just use * in your SELECT clause, and you get entire resultset. Then, $admins is an array, and you can get its count using count method.
$admins = DB::table('users')
->select('users.*')
->join('users_roles', 'users.id', '=', 'users_roles.user_id')
->where('users_roles.role_id', '=' ,0)
->get();
I assume you have users_roles.role_id = 0 for exactly once for a user. There are no multiple entries for role_id = 0 for one user right?
I hope this helps.
Or you can use...
$admins = User::roles()->where('role_id', '=', 0)->count();
If the relations are setup correctly that should work.
Resource: http://laravel.com/docs/eloquent#many-to-many
I am not sure how laravel handles nested selects with the query builder, in plain sql, it would look like this
SELECT users.id, (SELECT COUNT(users.id) FROM users) AS admin_count
FROM users
JOIN users_roles
ON user_roles.id = users.id
WHERE users_roles.role_id = 0
ORDER BY first_name
which I am pretty sure you can just assign to a variable and run it like $admins = DB::query($sql)