I am trying to convert a query to eloquent or find a way to use it in a laravel controller.
The query is:
select employee, workdate, sum(actualhours)
from ts_data group by employee, workdate;"
Using Eloquent Model :
$records = TsData::select(
'employee',
'workdate',
\DB::raw('sum(actualhours) as sumhours')
)
->groupBy('employee', 'workdate')
->get();
or using DB Facade :
$records = \DB::table('ts_data')->select(
'employee',
'workdate',
\DB::raw('sum(actualhours) as sumhours')
)
->groupBy('employee', 'workdate')
->get();
Related
I have the following raw SQL query:
select a.id user_id, a.email_address, a.name_first, a.name_last, count(b.id) number_of_videos, sum(b.vimeo_duration) total_duration, sum(b.count_watched) total_playbacks
from users a,
videos b
where a.id = b.tutor_id
and a.email_address in ('candace_rennie#yahoo.com', 'tjm#hiltoncollege.com', 'matthewjameshenshall#gmail.com', 'nkululeko#syafunda.co.za', 'khulile#syafunda.co.za', 'nzakheni#syafunda.co.za')
group by a.id;
This correctly gets 6 rows from the database. I'm trying to convert this to a Laravel database query like so:
$totals = DB::table('users')
->select(DB::Raw('users.id as user_id'), 'users.email_address', 'users.name_first', 'users.name_last', DB::Raw('count(videos.id) as number_of_videos'), DB::Raw('sum(videos.vimeo_duration) as total_duration'), DB::Raw('sum(videos.count_watched) as total_playbacks'))
->join('videos', 'users.id', '=', 'videos.tutor_id')
->where('users.id', 'videos.tutor_id')
->whereIn('users.email_address', array('candace_rennie#yahoo.com', 'tjm#hiltoncollege.com', 'matthewjameshenshall#gmail.com', 'nkululeko#syafunda.co.za', 'khulile#syafunda.co.za', 'nzakheni#syafunda.co.za'))
->groupBy('users.id')
->get();
This however return 0 rows. Is there anything I'm missing?
It should be smth like below even tho groupBy user id does not help much as id is unique.
$aggregates = [
DB::raw('count(b.id) as number_of_videos'),
DB::raw('sum(b.vimeo_duration) as total_duration'),
DB::raw('sum(b.count_watched) as total_playbacks'),
];
$simpleSelects = ['users.email_address', users.id, 'users.name_first', 'users.name_last'];
$emails = ['candace_rennie#yahoo.com', 'tjm#hiltoncollege.com'....]
$users = Users::select(array_merge($simpleSelects, $aggregates))
->leftJoin('videos as b', function ($join) use ($emails) {
$join->on('b.tutor_id', 'a.id')
->whereIn('users.email_address', $emails);
})
->groupBy('users.id')
->get();
Try to remove this line:
->where('users.id', 'videos.tutor_id')
List item
after sql code convert into laravel
DB::select('posts.id','posts.title','posts.body')
->from('posts')
->where('posts.author_id', '=', 1)
->orderBy('posts.published_at', 'DESC')
->limit(10)
->get();
How can I convert the following query into a laravel eloquent query?
SELECT demand_costings.pr_number as Demand_Costing,work_types.work_types,
SUM(demand_costings.pr_quantity_in_pcs) as Total
FROM demand_costings
INNER JOIN work_types
ON demand_costings.worktype_id=work_types.id
GROUP BY work_types.work_types;
You can use laravel's DB facade:
$data = DB::table('table_name')
->select(DB::raw('emand_costings.pr_number as Demand_Costing,work_types.work_types,SUM(demand_costings.pr_quantity_in_pcs) as Total FROM demand_costings INNER JOIN work_types ON demand_costings.worktype_id=work_types.id'))
->groupBy('work_types')
->get();
Try this:
DB::table('demand_costings')
->select([
'pr_number as Demand_Costing',
'work_types',
'SUM(pr_quantity_in_pcs) as Total'
])
->join('work_types', 'demand_costings.worktype_id', '=', 'work_types.id')
->groupBy('work_types.work_types')
->get();
I have a working SQL query.
SELECT stuid,grade,SUM(full_amount) FROM due_payments group by stuid having SUM(full_amount) !=15600
This is working fine in MySQL workbench and phpmyadmin,But i cant seems to get this work in Laravel 5.3
I tried this on Laravel app with no Luck
$someVariable = Input::get(15600);
$results = DB::select( DB::raw("SELECT stuid,grade,SUM(full_amount) FROM due_payments
group by stuid having SUM(full_amount) =:somevariable)", array(
'somevariable' => $someVariable,
)));
Can someone Help me with this.Thank You.
First if all Input::get() doesn't take value as argument but the element name
$someVariable = Input::get(15600);
You can just use $someVariable = 15600;
Then use Query Builder rather than Raw SQL query
$results = DB::table('due_payments')
->select(array('stuid', 'grade', DB::raw('SUM(full_amount)')))
->groupBy('stuid')
->havingRaw('SUM(full_amount) != '.$someVariable)
->get();
Use query builder.
$results = DB::table('due_payments')
->select('stuid', 'grade',DB::raw('SUM(full_amount)'))
->groupBy('stuid')
->havingRaw('SUM(full_amount) != 15600')
->get();
I have the following sql query
SELECT * FROM exams WHERE exams.id NOT IN (SELECT examId FROM testresults)
how can I convert it into Laravel query builder format?
Thanks.
You can use whereNotIn with a closure:
$result = DB::table('exams')->whereNotIn('id', function($q){
$q->select('examId')->from('testresults');
})->get();
with Eloquent :
$result = Exams::whereNotIn('id', function($q){
$q->select('examId')->from('testresults');
})->get();
Is there a way to do a ORDER BY FIELD query? This is what I currently have.
$sets = DB::connection('mysql')
->table('sets')
->orderBy('type', 'asc')
->orderBy('releaseDate', 'asc')
->select('code', 'type', 'name')
->get();
Or do I have to write the MySQL directly?
You can use DB::raw() to insert your ORDER BY FIELD clause:
$sets = DB::connection('mysql')
->table('sets')
->orderBy(DB::raw('FIELD(type, "Banana", "Apple", "Orange", "Peach", "Grape")'))
->select('code', 'type', 'name')
->get();
Beware of SQL injection of course... And to check what the final SQL is, you can use the Query Log after running the SQL:
$query_log = DB::getQueryLog();