$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.
Related
SELECT Code_tank, temperature, salinity, PH, parameters.created_at as created_at FROM parameters WHERE id IN (SELECT MAX(id) FROM parameters group by code_Tank) ORDER BY code_tank ASC;
I have been trying to put this SQL sentence in laravel but could not, this is the code with that i was trying:
$testMonitor= DB::table('parameters')
->select('code_Tank', 'salinity', 'PH', 'temperature',
DB::raw('parameters.created_at AS created_at'))
->whereIn('id', DB::raw('SELECT MAX(id) FROM parameters group by code_Tank'))
->orderby('code_Tank', 'asc')->get();
And this is the error, I assumed that one of the parameters in WHEREIN must be an array but I can't think of any other way to accommodate the SQL statement in laravel, please if you could help me
TypeError: Argument 1 passed to Illuminate/Database/Query/Builder::cleanBindings() must be of the type array, object given, called in C:/laragon/www/SoftwareOzimandias/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php on line 907
DB::Raw() will return an object as the second parameter for whereIn, but whereIn want to take the parameter that is array.
You can use closure, check the where-clauses reference:
DB::table('parameters')->whereIn('id', function($query){
$query->select(DB::raw('MAX(id)'))
->from('parameters')
->groupBy('code_Tank');
})->orderby('code_Tank', 'asc')
->select('code_Tank', 'salinity', 'PH', 'temperature',
DB::raw('parameters.created_at AS created_at'))
->get();
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
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
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.
I have this code to create a collection:
$rank_entities_by_capacity = Entity::join('entity_capacitytypes', function($q){
$q->on('entitys_id', '=', 'entities.id');
$q->where('capacitytypes_id','=', '23');
})->
leftJoin('user_attitudes', function($q){
$q->on('entity_id', '=', 'entities.id');
$q->where('item_type', '=', 'entity');
})
->selectRaw('entities.*, SUM(user_attitudes.importance) AS importance')
->groupBy('entities.id')
->orderBy('importance', 'desc')
->take(6)
->get();
First problem:
1052 Column 'entity_id' in on clause is ambiguous:
[2015-01-01 13:22:28] production.ERROR: FATAL DATABASE ERROR: 500 = SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in on clause is ambiguous (SQL: select entities.*, SUM(user_attitudes.importance) AS importance from entities inner join entity_capacitytypes on entity_id = entities.id and capacitytypes_id = 23 left join user_attitudes on entity_id = entities.id and item_type = entity group by entities.id order by importance desc limit 6) [] []
I traded over an hour for finding a trick to bypass the problem:
I was forced to change column name in table entity_capacitytypes from entity_id (it was causing problems) to entitys_id just in order to avoid this error.
Now my database names are not consistent. Any other way to avoid the error?
Problem 2:
If I add this part to the query, and try to use a previously perfectly working variable in the where line
join('entity_capacitytypes', function($q){
$q->on('entitys_id', '=', 'entities.id');
$q->where('capacitytypes_id','=', $capacity);
})->
I get this error:
Undefined variable: capacity
How make the variable work?
My solution: avoidance again.
I ould't fix join, so I used a relation defined in Capacitytype model:
public function entities()
{
return $this->belongsToMany('Entity', 'entity_capacitytypes', 'capacitytypes_id', 'entitys_id');
}
and instead using join, I accessed Entity from another model
$rank_entities_by_capacity = Capacitytype::find($capacity)->entities()->
leftJoin('user_attitudes', function($q){
$q->on('entity_id', '=', 'entities.id');
$q->where('item_type', '=', 'entity');
})
->selectRaw('entities.*, SUM(user_attitudes.importance) AS importance')
->groupBy('entities.id')
->orderBy('importance', 'desc')
->take(6)
->get();
To do:
make the variable work in join
Problem 1
To fix the "ambiguous column" problem you only need to specify the full column name including the table
entity_capacitytypes.entity_id instead of only entity_id
Problem 2
To use a local variable like $capacity inside a closure (aka anonymous function) you need to inject them with use
join('entity_capacitytypes', function($q) use ($capacity){
$q->on('entitys_id', '=', 'entities.id');
$q->where('capacitytypes_id','=', $capacity);
})