Join ON AND with laravel eloquent query builder - mysql

I am trying to write the following sql query in laravel:
select
`workers`.`first_name`,
`workers`.`last_name`,
`occupations`.`approval_date`,
`occupations`.`certification_date`,
`occupations`.`expiration_date`,
`occupations`.`type`,
`workers`.`photo`
from `worker_print_queue`
inner join `workers` on `worker_print_queue`.`worker_id` = `workers`.`id`
inner join `occupations` on `worker_print_queue`.`certification` = `occupations`.`type` AND `workers`.`id` = `occupations`.`worker_id`
I have written it in eloquent in the following manner:
$records = \DB::table('worker_print_queue')
->join('workers', 'worker_print_queue.worker_id', '=', 'workers.id')
->join('occupations', function($join){
$join->on('workers.id', '=', 'occupations.worker_id')
->where('worker_print_queue.certification', '=', 'occupations.type');
})
->select('workers.first_name', 'workers.last_name', 'occupations.approval_date',
'occupations.certification_date', 'occupations.expiration_date', 'occupations.type', 'workers.photo')
->get();
But I am receiving no results. I'm sure there is something I'm missing, just not sure what it is.

The issue is that this line
->where('worker_print_queue.certification', '=', 'occupations.type');
is comparing the column 'worker_print_queue.certification' to the value 'occupations.type'.
If you change it to use the on clause you should be fine:
->on('worker_print_queue.certification', '=', 'occupations.type');

Related

Join Multiple Columns on Laravel Query Builder

I have no idea how to write it in Laravel Query Builder. Here's the SQL query:
select l.id, l.title, is1.similarity
FROM l
left join is1 on (l.id = is1.listing_id1 and is1.listing_id2 = 225678) or (l.id = is1.listing_id2 and is1.listing_id1 = 225678)
If I simply translate your SQL query to Laravel Query Builder query It would look like this.
DB::table('l')->select('l.id', 'l.title', 'is1.similarity')
->leftJoin('is1', function ($leftJoin) {
$leftJoin->on('l.id', '=', 'is1.listing_id1')->where('is1.listing_id2', '=', '225678')
->orOn('l.id','=', 'is1.listing_id2')->where('is1.listing_id1','=', '225678'); })
->get();
I guess, it would be something like this:
DB::table('l')
->('l.id', 'l.title', 'is1.similarity')
->join('is1', function ($join) {
$join->on('l.id', '=', 'is1.listing_id1') ->where('is1.listing_id2', '=', '225678')
->orOn('l.id','=', 'is1.listing_id2')->where('is1.listing_id1', '=', '225678');
})->get();
Don't forget to import this:
use Illuminate\Support\Facades\DB;
More information can be find here.

Convert Raw SQL query to Laravel DB Query

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 I Can Convert SQL query to Query Builder

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();

Laravel 5.2 - Odd Results When using Left Join on Subquery

I have the following query:
$products = Product::leftJoin(DB::Raw('(SELECT imageable_id, MIN(created_at) as min_created_at
FROM images WHERE imageable_type = "App\\\Product"
GROUP BY imageable_id) AS subquery'), function($join) {
$join->on('subquery.imageable_id', '=', 'products.id');
})
->leftJoin('images', function ($join) {
$join->on('images.imageable_id', '=', 'subquery.imageable_id')
->where('images.created_at', '=', 'subquery.min_created_at');})
->select('products.*', 'images.file_path')
->paginate(5);
When I die and dump the query log, the above gets translated as follows:
"query" => """
select `products`.*, `images`.`file_path` from `products`
left join (SELECT imageable_id, MIN(created_at) as min_created_at
FROM images WHERE imageable_type = "App\\Product"
GROUP BY imageable_id) AS subquery on `subquery`.`imageable_id` = `products`.`id`
left join `images` on `images`.`imageable_id` = `subquery`.`imageable_id` and `images`.`created_at` = ?
limit 5 offset 0
"""
"bindings" => array:1 [
0 => "subquery.min_created_at"
]
Which looks correct, though I'm unsure why a binding has been added for subquery.min_created_at
Now when I execute the above the query in laravel images.file_path is always null when clearly I know there are related images. When I test the above query by pasting it and running directly in MySQL command line I get the expected results i.e. for products which have images, file_path for image is not null. The only difference when I run in MySQL command line is that I'm not doing any binding for subquery.min_created_at - I simply replaced the ? with subquery.min_created_at
Any ideas why the query is behaving this way. If I remove the second left join it works correctly but then I can't select the first created image to load e.g doing the following give me the file_path:
$products = Product::leftJoin(DB::Raw('(SELECT imageable_id, file_path
FROM images WHERE imageable_type = "App\\\Product"
GROUP BY imageable_id) AS subquery'), function($join) {
$join->on('subquery.imageable_id', '=', 'products.id');
})
->select('products.*', 'subquery.file_path')
->paginate(5);
Ideally I want to get my original query working in - any help appreciated.
You are using ->where() for your second join condition:
->where('images.created_at', '=', 'subquery.min_created_at')
This generates
and `images`.`created_at` = ?
which the binding is for.
Instead you should use ->on();
$join->on('images.imageable_id', '=', 'subquery.imageable_id')
->on('images.created_at', '=', 'subquery.min_created_at');

Convert this SQL query to Laravel?

Hi i have troubles converting this query to Laravel, my query works fine, but idk how to convert to a laravel query:
select p.id, p.enunciado, p.created_at, c.categoria, s.categoria
from preguntas_seleccion p, categorias c,sub_categorias s
where p.categorias_id = c.id and p.sub_categorias_id = s.id and c.id = s.categorias_id
Do you really need the last condition? Anyway, it should work like this (untested code!):
$query = \DB::table('preguntas_seleccion as p')->select("p.id","p.enunciado","p.created_at", "c.categoria","s.categoria");
$query->join('categorias', function($join)
{
$join->on('p.categorias_id', '=', 'c.id');
$join->on('c.id', '=', "s.categorias_id");
});
$query->join('sub_categorias as s', 'p.sub_categorias_id', '=', 's.id');
return $query->get();