I want to sort my Laravel query builder results on a custom column (concat of first_name and last_name).
What I have done is-
$summary = DB::table('service_rating')
->join('partners', 'partners.id', '=', 'service_rating.partner_id')
->join('users', 'users.id', '=', 'partners.user_id')
->select(
DB::raw("CONCAT( users.first_name,' ', users.last_name) as lawn_pro"),
DB::raw ('AVG(service_rating.rating) as rating'),
DB::raw ('COUNT(service_rating.rating) as jobs'),
DB::raw ('SUM(service_rating.rating) as payout')
)
->where('customer_id', '=', Auth::user()->id)
->whereRaw('service_rating.created_at >= DATE(NOW()) - INTERVAL '.$no_of_day_to_show.' DAY')
->groupBy('service_rating.partner_id')
->orderBy('lawn_pro', 'asc');
So, I am getting error for this line -
->orderBy('lawn_pro', 'asc');
And error is like this-
Can anyone please help ?
Apparently you are using the count() function on your query, this ignores the select attributes because we only want to know the count of the rows. Because of this, lawn_pro is not available in the query.
I would suggest to execute the query and then count the available rows.
$rows = $summary->get();
$count = count($rows);
Related
I'm constructing a RAW database query with Laravel (5.4) and I'm getting a problem in that laravel is placing quotes in the SQL output for a variable within a join... The result is that mysql thinks that the variable... Which for example would output to 1 or something numeric is a column and outputs error "Cannot find column '1' in ON clause
PROBLEM SQL CLAUSE:
->join('event_jury_sub_detail', function($join) use($userid)
{
$join->on('event_jury_sub_detail.evtpartsub_id', '=', 'event_partsubmissions.id')
->on('event_jury_sub_detail.authoruser_id','=',$userid);
Problem: $userid gets quotes... And becomes "and event_jury_sub_detail.authoruser_id = 1" - Laravel treats it as a column to look up and fails.
If the quotes are removed it works fine... How to resolve this?
FULL QUERY
$tbl_return = DB::table('event_partsubmissions')
->select(DB::raw('event_partsubmissions.*,
users.usr_firstnames,
users.usr_surnames,
users.email,
users.usr_nationality,
users.usr_country,
event_jury_sub_detail.id as evtjurysubdetid,
event_jury_sub_detail.evtjurysubdet_pointsawarded,
event_jury_sub_detail.evtjurysubdet_notes,
event_jury_sub_detail.evtjurysubdet_randomkey
'))
->join('event_participation', 'event_partsubmissions.evtpartsub_evtpartid', '=', 'event_participation.id')
->join('users', 'evtpart_userid', '=', 'users.id')
->join('event_jury_sub_detail', function($join) use($userid)
{
$join->on('event_jury_sub_detail.evtpartsub_id', '=', 'event_partsubmissions.id')
->on('event_jury_sub_detail.authoruser_id','=',$userid);
})
->where('event_partsubmissions.evtpartsub_evtid', '=', $id)
->where('event_partsubmissions.evtpartsub_enabled', '>=', $partlevel)
->where('event_participation.evtpart_enabled', '>=', 1)
->where('event_participation.evtpart_level', '>=', 100)
->where('users.usr_enabled', '>=', 1)
->get();
You can try to use the alias for table joins:
->join('event_jury_sub_detail as ejsd', ...
And use the alias in following places:
->$join->on('ejsd.evtpartsub_id', '=', 'event_partsubmissions.id')
For scalar params in join conditions, you can use DB wrapper like DB::raw($userid):
->on('ejsd.authoruser_id','=',DB::raw($userid));
use DB::raw()
->on(DB::raw("event_jury_sub_detail.authoruser_id = '".$userid."'"))
I'm trying to select a number of columns along with MAX. The raw query would be something like: SELECT users.id, ..., MAX(ur.rank) AS rank but I cannot figure out how to do it using the query builder supplied by Laravel in Eloquent.
This is my attempt:
$user = User::join('users_ranks AS ur', function($join) {
$join ->on('ur.uid', '=', 'users.id');
})
->where('users.id', '=', 7)
->groupBy('users.id')
->first(['users.id', 'users.username', 'MAX(ur.rank) AS rank']);
I simply cannot figure it out. What I want to achieve is I'm selecting a user where users.id = 7, and I'm wanting to select the MAX rank that's in users_ranks where their users_ranks.uid = users.id.
I was told to avoid sub-queries as when working with large result sets, it can slow things down dramatically.
Can anyone point me in the right direction? Thanks.
I think you should rewrite it like this:
DB::table('users')
->select(['users.id', 'users.username', DB::raw('MAX(ur.rank) AS rank')])
->leftJoin('users_ranks AS ur', 'ur.uid', '=', 'users.id')
->where('users.id', '=', 7)
->groupBy('users.id')
->first();
No sense to use User:: if you use table names later and want to fetch not all of the fields ( 'users.id', 'users.username' ).
I'm new to laravel and I have some issues with the query builder.
The query I would like to build is this one:
SELECT SUM(transactions.amount)
FROM transactions
JOIN categories
ON transactions.category_id == categories.id
WHERE categories.kind == "1"
I tried building this but it isn't working and I can't figure out where I am wrong.
$purchases = DB::table('transactions')->sum('transactions.amount')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->select('transactions.amount')
->get();
I would like to get all the transactions that have the attribute "kind" equal to 1 and save it in a variable.
Here's the db structure:
transactions(id, name, amount, category_id)
categories(id, name, kind)
You don't need to use select() or get() when using the aggregate method as sum:
$purchases = DB::table('transactions')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->sum('transactions.amount');
Read more: http://laravel.com/docs/5.0/queries#aggregates
If one needs to select SUM of a column along with a normal selection of other columns, you can sum select that column using DB::raw method:
DB::table('table_name')
->select('column_str_1', 'column_str_2', DB::raw('SUM(column_int_1) AS sum_of_1'))
->get();
You can get some of any column in Laravel query builder/Eloquent as below.
$data=Model::where('user_id','=',$id)->sum('movement');
return $data;
You may add any condition to your record.
Thanks
MyModel::where('user_id', $_some_id)->sum('amount')
I want to run this mysql query in Laravel 5 using the DB query :
// SELECT *, rating/number as total FROM `courses` order by total DESC;
This is what I tried :
$query = \DB::table('courses')->select('*');
$courses = $query->addSelect('rating/number as total')
->orderBY('total DESC')
->get();
but, rating/number is considered as a table column . The same thing happens when I tried it inside parenthesis (rating/number).
Any help?
$courses = \DB::table('courses')
->selectRaw('*, rating/number as total')
->orderBY('total', 'DESC')
->get();
Can you use Raw Expressions for it? Maybe something like this:
$courses = \DB::table('courses')
->select(DB::raw('*, (rating / number) as total'))
->orderBy('total DESC')
->get();
I'm trying to order my query by hour, but I don't know how to do it. In MySql will be:
ORDER BY HOUR(FechaTermino) // FechaTermino = Timestamp... (dd/mm/yy hh:mm:ss)
but in laravel??
this is my Query Builder
$servicios = DB::table('Servicio_Tecnico')
->join('Servicio', 'Servicio_Tecnico.Servicio_idServicio', '=', 'Servicio.idServicio')
->join('Tecnico', 'Servicio_Tecnico.Tecnico_idTecnico', '=', 'Tecnico.idTecnico')
->whereRaw('DAY(Servicio.FechaTermino) = ?', array($dia))
->where('Servicio.Completado', '=', '1')
->orderBy('FechaTermino', 'Desc')
->paginate(10);
Just use a raw query:
->orderBy(DB::raw('HOUR(FechaTermino)'))
Check this answer, very similar.