Laravel Query Builder: Using `Case` with `Joins` - laravel

I am trying to replicate the following psql query in laravel:
SELECT
DISTINCT
I.id,
T.id,
T.name,
I.first_name,
I.middle_name,
I.last_name,
I.dob,
CASE WHEN NOT E.email IS NULL THEN '<<' ELSE '' END
FROM individuals AS I
LEFT JOIN titles AS T ON I.title_id = T.ID
LEFT JOIN individuals_emails_map AS IEM ON IEM.individual_id = I.id
LEFT JOIN emails AS E ON E.id = IEM.email_id;
When I use the query builder and write:
DB::table('individuals AS I')
->join('titles AS T', 'T.id', '=', 'I.title_id')
->leftjoin('individuals_emails_map AS IEM', 'IEM.individual_id', '=', 'I.id')
->leftjoin('emails AS E', 'E.id', '=', 'IEM.email_id')
->distinct('I.title_id', 'T.name', 'I.first_name', 'I.middle_name', 'I.last_name', 'I.dob')
->select('T.id','T.name', 'I.first_name','I.middle_name','I.last_name','I.dob')
->get();
... it works fine, however when I try and insert the CASE line:
DB::table('individuals AS I')
->join('titles AS T', 'T.id', '=', 'I.title_id')
->leftjoin('individuals_emails_map AS IEM', 'IEM.individual_id', '=', 'I.id')
->leftjoin('emails AS E', 'E.id', '=', 'IEM.email_id')
->distinct('I.title_id', 'T.name', 'I.first_name', 'I.middle_name', 'I.last_name', 'I.dob')
->select('T.id','T.name', 'I.first_name','I.middle_name','I.last_name','I.dob',
DB::raw("CASE WHEN NOT E.email IS NULL THEN '<<' ELSE '' END"))
->get();
... I get the following error:
Undefined table: 7 ERROR: missing FROM-clause entry for table "e"
What am I doing wrong?

I think that the problem in your case statement:
DB::raw("CASE WHEN NOT E.email IS NULL THEN '<<' ELSE '' END"))
it should be:
DB::raw('case WHEN E.email is Not NULL THEN "<<" Else " " END resultEmail'))

Related

Laravel eloquent orwhere in leftjoin fails

My eloquent query is not giving me what I expects. When I use orWhere it is put on the end of the query not after the leftjoin.
$r = groupofpupil::where('group_id' , '=', '13')
->leftjoin('pupils' ,'pupil1_id' , '=', 'pu_id' )
->orWhere('pupil0_id', '=', 'pu_id')
->leftjoin('users', 'pupils.id', '=', 'users.id')
->select('*')
->get();
the sql query is:
SELECT * from groupofpupils
LEFT JOIN pupils on pupil1_id = pu_id
LEFT JOIN users on pupils.id = users.id
WHERE group_id = 13
OR pupil0_id = pu_id
but I expected this
SELECT * from groupofpupils
LEFT JOIN pupils on pu_id = pupil1_id OR pu_id = pupil0_id
LEFT JOIN users on pupils.id = users.id
WHERE group_id = 13
Please, what am I doing wrong?
Try something like this :
$r = groupofpupil::leftjoin('pupils', 'pupils.pupil1_id', '=', 'groupofpupils.pu_id')
->leftjoin('users', 'users.id', '=', 'pupils.id')
->where('groupofpupils.group_id', 13)
->orWhere('pupils.pupil0_id', '=', 'groupofpupils.pu_id')
->get();

subquery inside orWhere in laravel

I need assistance to build up the query like below in laravel:
SELECT *
FROM table t
WHERE t.a = 1
OR (t.a=0
AND t.id IN (
SELECT o.a_id
FROM other_table o
WHERE o.x > 3
)
);
You could try to build your exact current query, and in fact it might even be the most efficient to write it. But, if we rephrase your query using a left join, it becomes somewhat easier to express in Laravel code.
SELECT *
FROM your_table t
LEFT JOIN other_table o
ON t.id = o.a_id AND o.x > 3
WHERE
t.a = 1 OR
(t.a = 0 AND o.a_id IS NOT NULL);
This would translate to the following Laravel code:
$result = DB::table('your_table t')
->leftJoin('other_table o', function($join) {
$join->on('t.id', '=', 'o.a_id');
$join->on('o.x', '>', '3');
})
->where('t.a', '=', '1')
->orWhere(function($query) {
return $query->where('t.a', '=', '0')
->whereNotNull('o.a_id')
})
->get();

how to create select join multiple condition in laravel 5.3

how to create select join multiple condition in laravel 5.3
SQL SELECT Statement.
SELECT table_1.column_1
,table_2.column_1
,table_3.column_1
FROM table_1
LEFT JOIN table_2
ON table_1.column_1 = table_2.column_1
LEFT JOIN table_3
ON table_1.column_2 = table_3.column_2
AND table_3.column_3 <= NOW()
AND ( table_3.column_4 >= NOW()
OR table_3.column_4 = 0
)
WHERE table_1.column_1 = '0000000001'
I want to convert SQL Statement to laravel select.
I try.
$result = DB::table('table_1')
->select('table_1.column_1', 'table_2.column_1', 'table_3.column_1')
->leftJoin('table_1', 'table_1.column_1', '=', 'table_2.column_1')
->leftJoin('table_3', 'table_1.column_2', '=', 'table_3.column_2')
->where('table_1', $_POST['id'])
->get();
DB::table('table_1')
->select('table_1.column_1', 'table_2.column_1', 'table_3.column_1')
->leftJoin('table_1', 'table_1.column_1', '=', 'table_2.column_1')
->leftJoin('table_3', 'table_1.column_2', '=', 'table_3.column_2')
->where('table_1.column_1', $_POST['id'])
->where('table_3.column_3','>=', NOW())
->where(function($query) {
$query->where('table_3.column_4', '>=', NOW())
->orWhere('table_3.column_4','0');
})
->get();

Lumen eloquent left join

I want to make some migration from Laravel to Lumen, before that every code on my Laravel working and has no error and running well. Then i found this line and eloquent in Lumen translate this different than Laravel.
DB::table('products_categories')
->select('products.*', 'brands.name as brand_name', 'categories.link_rewrite as category_link_rewrite',
'product_medias.id as id_image', 'product_stocks.quantity as product_stock', 'product_medias.path','product_medias.filename', 'specific_prices.reduction_type', 'specific_prices.reduction as reduction',
'specific_prices.reduction', 'products_categories.position')
->leftJoin('products', 'products.id', '=', 'products_categories.product_id')
->leftJoin('product_stocks', 'products.id', '=', 'product_stocks.product_id')
->leftJoin('brands', 'brands.id', '=', 'products.brand_id')
->leftjoin('product_medias', function($join){
$join->on('product_medias.product_id', '=', 'products.id')
->whereIn('product_medias.cover', [1, 0]);
})
->leftJoin('categories', 'products_categories.category_id', '=', 'categories.id')
->leftJoin('specific_prices', function($join){
$join->on('specific_prices.product_id', '=', 'products.id')
->on(DB::raw("(`specific_prices`.`from` = '0000-00-00 00:00:00' OR '".date('Y-m-d H:i:s')."' >= `specific_prices`.`from`)"), DB::raw(''), DB::raw(''));
})
->where('categories.name','=', "Special Price" )
->where('products.active','=', 1)
->whereNull('products.deleted_at')
->groupBy('products_categories.product_id')
->orderBy('products_categories.id', 'desc');
Once again it's working on my Laravel but i get 'Error' on Lumen, is there any solution to solve this?
If i dump this line the query will look like this :
select `products`.*, `brands`.`name` as `brand_name`, `categories`.`link_rewrite` as `category_link_rewrite`, `product_medias`.`id` as `id_image`, `product_stocks`.`quantity` as `product_stock`, `product_medias`.`path`, `product_medias`.`filename`, `specific_prices`.`reduction_type`, `specific_prices`.`reduction` as `reduction`, `specific_prices`.`reduction`, `products_categories`.`position` from `products_categories` left join `products` on `products`.`id` = `products_categories`.`product_id` left join `product_stocks` on `products`.`id` = `product_stocks`.`product_id` left join `brands` on `brands`.`id` = `products`.`brand_id` left join `product_medias` on `product_medias`.`product_id` = `products`.`id` and `product_medias`.`cover` in (1, 0) left join `categories` on `products_categories`.`category_id` = `categories`.`id` left join `specific_prices` on `specific_prices`.`product_id` = `products`.`id` and (`specific_prices`.`from` = '0000-00-00 00:00:00' OR '2016-11-14 09:12:44' >= `specific_prices`.`from`) = where `categories`.`name` = Special Price and `products`.`active` = 1 and `products`.`deleted_at` is null group by `products_categories`.`product_id` order by `products_categories`.`id` desc limit 10
There is equal symbol after left join specific_prices and also didn't track 'Special Price' as character

Laravel 5.3 Join on Field Value

This Laravel Model Query
PostModel::select('posts.*', 'users.name', 'users.user_fullname', 'users.user_gender', 'users.user_picture', 'users.user_picture_id', 'users.user_cover','users.user_cover_id', 'users.user_verified', 'users.user_pinned_post', 'pages.*', 'groups.group_name', 'groups.group_picture_id', 'groups.group_cover_id', 'groups.group_title', 'groups.group_admin', 'groups.group_pinned_post')
->leftjoin('users',function($flj){
$flj->on('posts.user_id', '=' ,'users.id')
->where('posts.user_type','=','user');
})
->leftjoin('pages',function ($slj){
$slj->on('posts.user_id', '=', 'pages.page_id')
->where('posts.user_type','=','page');
})
->leftjoin('groups',function ($tlj){
$tlj->on('posts.in_group','=',1)
->whereColumn('posts.group_id', '=', 'groups.group_id');
})
->whereRaw('NOT (users.name <=> NULL AND pages.page_name <=> NULL)')
->where('posts.post_id','=',$id)->get();
When Query Run Then Give me this Error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select `posts`.*, `users`.`name`, `users`.`user_fullname`, `users`.`user_gender`, `users`.`user_picture`, `users`.`user_picture_id`, `users`.`user_cover`, `users`.`user_cover_id`, `users`.`user_verified`, `users`.`user_pinned_post`, `pages`.*, `groups`.`group_name`, `groups`.`group_picture_id`, `groups`.`group_cover_id`, `groups`.`group_title`, `groups`.`group_admin`, `groups`.`group_pinned_post` from `posts` left join `users` on `posts`.`user_id` = `users`.`id` and `posts`.`user_type` = user left join `pages` on `posts`.`user_id` = `pages`.`page_id` and `posts`.`user_type` = page left join `groups` on `posts`.`in_group` = `1` and `posts`.`group_id` = `groups`.`group_id` where NOT (users.name <=> NULL AND pages.page_name <=> NULL) and `posts`.`post_id` = 360)
If i run this query direct on database no error
Query is
select `posts`.*, `users`.`name`, `users`.`user_fullname`, `users`.`user_gender`, `users`.`user_picture`, `users`.`user_picture_id`, `users`.`user_cover`, `users`.`user_cover_id`, `users`.`user_verified`, `users`.`user_pinned_post`, `pages`.*, `groups`.`group_name`, `groups`.`group_picture_id`, `groups`.`group_cover_id`, `groups`.`group_title`, `groups`.`group_admin`, `groups`.`group_pinned_post` from `posts` left join `users` on `posts`.`user_id` = `users`.`id` and `posts`.`user_type` = 'user' left join `pages` on `posts`.`user_id` = `pages`.`page_id` and `posts`.`user_type` = 'page' left join `groups` on `posts`.`in_group` = 1 and `posts`.`group_id` = `groups`.`group_id` where NOT (users.name <=> NULL AND pages.page_name <=> NULL) and `posts`.`post_id` = 360
And how to use where not in laravel??
Thanks's
You can replace
->whereRaw('NOT (users.name <=> NULL AND pages.page_name <=> NULL)')
with
->whereNotNull('users.name')
->whereNotNull('pages.page_name')
Edit:
Replace this
->leftjoin('groups',function ($tlj){
$tlj->on('posts.in_group','=',1)
->whereColumn('posts.group_id', '=', 'groups.group_id');
with
->leftjoin('groups',function ($tlj){
$tlj->on('posts.group_id','=', 'groups.group_id');
And put the in_group before your ->whereNotNull()
->where('posts.in_group', '=', 1)

Resources