How can I select with count() on specific user in Laravel? - mysql

Here is my Database structure:
Also - there is a table users and reciever_id references that table id.
I use that query to get count of each type of notifications as well as data for that type of notifications from notification_types table.
Notification::
select('notification_types.*', DB::raw('count(*) as total'))
->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')
->groupBy('notifications.type_id')
->get()
What I need - is to set constraint on reciever_id, I just don't get - where should I put the where clause?

Just chain the where method anywhere before get, since your condition will be applied on the notifications table:
Notification::select('notification_types.*', DB::raw('count(*) as total'))
->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')
->where('reciever_id', $receiverId)
->groupBy('type_id')
->get();
Also, there's no need with this query to group by notifications.type_id, type_id will do, because there are no ambiguities created here because there are no other columns named type_id.

Related

groupBy on laravel

$tren = DB::table('suka')
->join('buku', 'buku.ISBN', '=', 'suka.ISBN')
->join('user', 'user.id_user', '=', 'suka.id_user')
->raw('count(*) as total')
->groupBy('suka.ISBN')
->orderBy('maximum')
->LIMIT(2)
->get();
I want to group a join table by ISBN and then find the maximum count of them and take 2 of the most.
here is the error message
Call to undefined method Illuminate\Database\Query\Expression::groupBy()
There is more than one ISBN column in the result set (because it exists in multiple tables). You should specify which table's ISBN column you want to group by in your group by call.
Also, try calling selectRaw instead of raw:
->selectRaw('count(*) as total')
You can read more about raw queries in this documentation.

Couldn't use the whereNotExists clause correctly in Laravel 8 Eloquent

I have 2 tables, one with different users, and the second table is an invoice table called "factures" and has a foreign key of userid, I called it client_id, which I am trying to get is the number of clients created_by a certain administrator and who have no invoices yet, here is what I tried:
$clients = User::select('id')
->where([['created_by',$membre_id],['role','Client']])
->orWhere([['updated_by',$membre_id],['role','Client']])
->whereNotExists(function($query)
{
$query->select(DB::raw('client_id'))
->from('factures')
->where('created_by',$member_id);
})->get();
but this query gives me all clients created_by $member_id without exception.
What is wrong with my query?
Did you try the following:
$clients = User::select('id')
->where(function($query) use($member_id){
$query->where([['created_by',$membre_id],['role','Client']])
->orWhere([['updated_by',$membre_id],['role','Client']])
})
->whereNotExists(function($query) use($member_id){
$query->select(DB::raw('client_id'))
->from('factures')
->where('created_by',$member_id);
})
->get();
}
This answer applied the OR condition only between the first two conditions (created_by and updated_by) and its result is AND with the third condition.

groupBy is expecting more table columns in many to many relationship

I'm trying to get all the roles permission which has a many to many relationship. I want to get all the permissions of a single role. Here I'm trying to use groupBy but it gives me error.
$search_role = DB::table('roles')
->join('roles_permissions','roles_permissions.role_id','roles.id')
->join('permissions','permissions.id','roles_permissions.permission_id')
->where('roles.name', 'like', "%$request->searcher%")
->orWhere('permissions.name', 'like', "%$request->searcher%")
->select('roles.name as role_name', 'permissions.name as permission_name','roles_permissions.*')
->groupBy('roles_permissions.role_id')
->get();
Error here:
roles_permissions.permission_id isn't in groupBy.
If I add that I get another column isn't in groupBy.
you don't have any aggregation operation in select statement. check my example
$search_role = DB::table('roles')
->join('roles_permissions','roles_permissions.role_id','roles.id')
->join('permissions','permissions.id','roles_permissions.permission_id')
->where('roles.name', 'like', "%$request->searcher%")
->orWhere('permissions.name', 'like', "%$request->searcher%")
->select('role_id', DB::raw('count(*) as total')) //example
->groupBy('roles_permissions.role_id')
->get();
what you are trying to do does not makes sense. Group by does not work in that way.
SELECT role_id, count(*)
from roles
group by role_id
is valid
SELECT roles.*, count(*)
from roles
group by role_id
is not. in the latter case, you generally need to add every column which is not aggregated to group by statement

Count a field in database

I want to count a field name 'leave_policy' depending on the name of 'leave_policy' and I am using joins to do it. My database is like this
https://imgur.com/a/gR3Dw
and I want to show count in applied section. For example if I want to count all "Urgent Leaves" and "Paternal Leaves" seperately I have applied till now. https://imgur.com/a/dvjuX
$join = DB::table('leaves_policy')
->join('leaves_policies', 'leaves_policy.leave_policy', '=', 'leaves_policies.title')
->join('leaves_requests', 'leaves_policy.requested_by' , '=', 'leaves_requests.requested_by')
->select('leaves_policy.*', 'leaves_policies.title', 'leaves_policies.total_no_of_leaves_allowed_per_year',
'leaves_policies.no_of_months_leaves_valid', 'leaves_policies.max_leaves_per_month', 'leaves_policies.max_con_leaves_per_month',
'leaves_requests.leave_status', DB::raw('COUNT(leaves_policy.leave_policy) as count'))
->groupBy('id')
->get();
For example,
I have a table Leave_table. leave_policy and purpose are the fields.
I am displaying the leave count based on the leave_policy column. i have used the below query to get the count.
select leave_policy,count(leave_policy) from leave_table group by leave_policy

Laravel-Update another table with sum from one table

I want to get cumulative salary monthly(the table name is cumulative_salary) from another table(the table name is salary), then update the data. I just can 'get()' the data but i can't update. I'm so confusing and i don't know the correct syntax. So, this is my code.
$data = DB::table('salary')
->where('employee_id', $employee_id)
->leftJoin('employees', 'salary.employee_id', '=', 'employees.id')
->select(DB::raw('SUM(salary_overtime) as `salary`'),
DB::raw('YEAR(overtime_date) year, MONTH(overtime_date) month'))
->groupby('month','year')
->get();