Laravel join and left join together in a query - php

I have a sql query i want to convert it into query builder or Laravel ORM. It works fine. But i want it into query builder form or ORM. Is it possible to write in query builder or ORM ?.
I have four tables answers, questions, users and upvote_answers. Query has three 'joins' and one 'left join' just to check current logged in user upvoted(boolean) an answer or not along with other attributes
SELECT answers.answer_content as answer_content,
answers.id as answer_id,
answers.created_at as created_at,
answers.created_at as answer_upvote,
answers.created_at as answer_downvote,
questions.id as question_id,
questions.question_title as question_title,
questions.question_slug as question_slug,
users.id as user_id,
users.name as user_name,
users.user_slug as user_slug,
upvote_answers.upvote as upvote
FROM
answers
JOIN questions on questions.id = answers.question_id
JOIN users on users.id = answers.user_id
LEFT JOIN upvote_answers ON
upvote_answers.answer_id = answers.id AND
upvote_answers.user_id = '2'
WHERE
questions.question_active = 1 and
answers.answer_active = 1

Using query builder you could write your query as
DB::table('answers as a')
->join('questions as q', 'q.id', '=', 'a.question_id')
->join('users as u', 'u.id', '=', 'a.user_id')
->leftJoin('upvote_answers as ua', function ($join) {
$join->on('ua.answer_id', '=', 'a.id')
->where('ua.user_id', '=', 2);
})
->where('q.question_active', '=', 1)
->where('a.answer_active', '=', 1)
->select(DB::raw('a.answer_content as answer_content,a.id as answer_id,a.created_at as created_at,a.created_at as answer_upvote,a.created_at as answer_downvote,q.id as question_id, q.question_title as question_title, q.question_slug as question_slug, u.id as user_id, u.name as user_name, u.user_slug as user_slug,ua.upvote as upvote'))
->get();

Use Join Clause
https://laravel.com/docs/master/queries#joins
Simple example
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->where('users.id', '=', 100)
->get();
Also you can use Left Join Clause.
Update:
$yourQuery = \DB::table('answers')
->join('questions', 'questions.id', '=', 'answers.question_id')
->join('users', 'users.id', '=', 'answers.user_id')
->leftJoin('upvote_answers', 'upvote_answers.answer_id', '=', 'answers.id')
->where('questions.question_active', '=', '1')
->where('answers.answer_active', '=', '1')
->selectRaw('answers.answer_content as answer_content,
answers.id as answer_id,
answers.created_at as created_at,
answers.created_at as answer_upvote,
answers.created_at as answer_downvote,
questions.id as question_id,
questions.question_title as question_title,
questions.question_slug as question_slug,
users.id as user_id,
users.name as user_name,
users.user_slug as user_slug,
upvote_answers.upvote as upvote');
echo $yourQuery->toSql(); //generated sql query as string
//$yourQuery->get(); //for get data from db
Result:
select answers.answer_content as answer_content, answers.id as
answer_id, answers.created_at as created_at, answers.created_at as
answer_upvote, answers.created_at as answer_downvote, questions.id as
question_id, questions.question_title as question_title,
questions.question_slug as question_slug, users.id as user_id,
users.name as user_name, users.user_slug as user_slug,
upvote_answers.upvote as upvote from answers inner join questions on
questions.id = answers.question_id inner join users on users.id =
answers.user_id left join upvote_answers on upvote_answers.answer_id =
answers.id where questions.question_active = ? and
answers.answer_active = ?
If you need upvote_answers.user_id = '2' in leftJoin must use advance left join:
->leftJoin('upvote_answers', function($advancedLeftJoin){
$advancedLeftJoin->on('users.id', '=', 'contacts.user_id')
->where('upvote_answers.user_id', '=', 2);
})
Finally, The answer for your example is:
$yourQuery = \DB::table('answers')
->join('questions', 'questions.id', '=', 'answers.question_id')
->join('users', 'users.id', '=', 'answers.user_id')
->leftJoin('upvote_answers', function($advancedLeftJoin){
$advancedLeftJoin->on('users.id', '=', 'contacts.user_id')
->where('upvote_answers.user_id', '=', 2);
})
->where('questions.question_active', '=', '1')
->where('answers.answer_active', '=', '1')
->selectRaw('answers.answer_content as answer_content,
answers.id as answer_id,
answers.created_at as created_at,
answers.created_at as answer_upvote,
answers.created_at as answer_downvote,
questions.id as question_id,
questions.question_title as question_title,
questions.question_slug as question_slug,
users.id as user_id,
users.name as user_name,
users.user_slug as user_slug,
upvote_answers.upvote as upvote');
echo $yourQuery->toSql(); //generated sql query as string
//dd($yourQuery->get()); //result as collection
Result:
select answers.answer_content as answer_content, answers.id as
answer_id, answers.created_at as created_at, answers.created_at as
answer_upvote, answers.created_at as answer_downvote, questions.id as
question_id, questions.question_title as question_title,
questions.question_slug as question_slug, users.id as user_id,
users.name as user_name, users.user_slug as user_slug,
upvote_answers.upvote as upvote from answers inner join questions on
questions.id = answers.question_id inner join users on users.id =
answers.user_id left join upvote_answers on users.id =
contacts.user_id and upvote_answers.user_id = ? where
questions.question_active = ? and answers.answer_active = ?
? value bind after use ->get()

Related

Converting complex SQL select to Laravel Eloquent

I am new to Laravel and trying to support an existing application that is in Laravel 5. I am trying to convert the following SQL to eloquent structure
SELECT s.id,
CONCAT(u.first_name, ' ', u.last_name) AS user_name,
u.avatar_location AS user_img,
s.employee_photo,
d.name AS department,
seg.name AS segment,
s.survey_title,
s.before_action,
s.before_picture,
s.action,
s.action_date,
s.after_action,
s.after_picture,
s.nominated,
s.awarded,
s.created_at,
s.updated_at,
(SELECT COUNT(1) FROM likes l WHERE l.survey_id = s.id) AS likes,
(SELECT COUNT(1) FROM likes l WHERE l.survey_id = s.id AND l.user_id = 5) AS UserLikes,
(SELECT COUNT(1) FROM comments c WHERE c.survey_id = s.id ) AS comments
FROM surveys s
JOIN departments d
ON s.department = d.id
JOIN segments seg
ON s.segment_id = seg.id
JOIN users u
ON s.user_id = u.id
WHERE s.status = 'Approved'
ORDER BY s.action_date DESC
LIMIT 20 OFFSET 20
I know enough Laravel to know that my basic start would probably be
$surveys = DB::table('surveys')
->join('departments', 'surveys.department', '=', 'departments.id')
->join('segments', 'surveys.segment_id', '=', 'segments.id')
->join('users', 'surveys.user_id', '=', 'users.id')
->where('surveys.status', 'Approved')
->orderBy('surveys.action_date')
->skip(20)-take(20)
->select(...)->get();
However, I am not sure how to do the subqueries. Looking for any suggestions.
Thanks!
For select statements, you can use DB::raw().
$surveys = DB::table('surveys')
->join('departments', 'surveys.department', '=', 'departments.id')
->join('segments', 'surveys.segment_id', '=', 'segments.id')
->join('users', 'surveys.user_id', '=', 'users.id')
->where('surveys.status', 'Approved')
->orderBy('surveys.action_date')
->skip(20)-take(20)
->select([DB::raw('(SELECT COUNT(1) FROM likes l WHERE l.survey_id = s.id) AS likes'),
...
])
->get();
Similarly, select statements would go under the select array. Just so to be clear, the result will be a collection.

GROUP_CONCAT gives incorrect values

I have 3 tables: Users, Properties, Units
User table:
user_id user_name
Properties table
pty_id pty_name user pty_status
Units Table
unit_id unit_name pty_id unit_status
i want to show user details , number of properties and units and their details.
Here is my query:
DB::statement('SET SESSION group_concat_max_len = 10485760');
$ar = DB::table('users as u')
->leftjoin('properties as p', function($join) {
$join->on('p.user_id', '=', 'u.user_id')->where('p.pty_status', '!=' ,0 );
})
->leftJoin(
DB::raw("
(select COALESCE(count(unit_id),0) AS cntunits, pty_id as temp_pty
from property_units as pu3
left join properties as p2 on pu3.unit_pty_id = p2.pty_id
where pu3.unit_status!=0
group by p2.pty_id) as temp"), 'p.pty_id', '=', 'temp.temp_pty')
->select(
DB::raw("group_concat(DISTINCT CONCAT(ej_p.pty_id,'|',ej_p.pty_name,'|',cntunits)) as pty_details"),
DB::raw("group_concat(DISTINCT CONCAT(ej_p.pty_id,'|',ej_p.pty_name)) as pty_details_copy")
)->paginate(10);
When I group_concat the unit_count, only those properties and units are concated where unit exists.
For example the above query returns the following result:
pty_details pty_details_copy
7|I2|2 7|I2, 22|tR ,51|SG APARTMENT,54_||_GA APARTMENTS,
Why properties with units (where unit count=0) are not binding? Where have I gone wrong?
EDIT
Here is the raw query:
select group_concat(DISTINCT CONCAT(p.pty_id,'|',p.pty_name,'|',cntunits)) as pty_details,
group_concat(DISTINCT CONCAT(p.pty_id,'|',p.pty_name)) as pty_details_copy
from users as u
left join properties as p on p.user_id = u.user_id and p.pty_status !=0
left join
(select COALESCE(count(unit_id),0) AS cntunits, pty_id as temp_pty
from property_units as pu3
left join properties as p2 on pu3.unit_pty_id = p2.pty_id
where pu3.unit_status!=0
group by p2.pty_id) as temp on p.pty_id = temp.temp_pty
As Solarflare suggested, i got the required result when I changed the query like this:
$ar = DB::table('users as u')
->leftjoin('properties as p', function($join) {
$join->on('p.user_id', '=', 'u.user_id')->where('p.pty_status', '!=' ,0 );
})
->leftJoin(
DB::raw("
(select COALESCE(count(unit_id),0) AS cntunits, pty_id as temp_pty
from property_units as pu3
left join properties as p2 on pu3.unit_pty_id = p2.pty_id
where pu3.unit_status!=0
group by p2.pty_id) as temp"), 'p.pty_id', '=', 'temp.temp_pty')
->select(
DB::raw("group_concat(DISTINCT CONCAT(ej_p.pty_id,'|',ej_p.pty_name,'|',coalesce(cntunits, 0))) as pty_details"))->paginate(10);
The WHERE from your first join should be part of the ON.

Laravel 5.2 Advanced join with another join condition

so I'm building a query with the querybuilder where I need to count rows from a joined table that has a where condition from another table. I can't join that table in the end because it would result in the wrong rows being compared to this specific join. I thought the advanced join clausule would allow another join to be added in it's own scope but this seems to not be the case.
This is what I tried:
$profile = DB::table('users')
->select('users.username', 'users.profile_picture', 'users.screen_state',
'count(replies.id) as replies', 'count(interactions.id) as interactions',
'count(alerts.id) as alerts', 'count(user__followers.id) as followers',
'count(user__following.id) as following', 'count(following.id) as volgend')
->leftJoin('replies', function($join)
{
$join->on('users.id', '=', 'replies.user_id')
->leftJoin('alerts')
->on('replies.alert_id', '=', 'alerts.id')
->where('replies.user_id', '=', $user->id)
->where('alerts.hide', '=', 0);
})
->leftJoin('interactions', function($join)
{
$join->on('users.id', '=', 'interactions.user_id')
->leftJoin('alerts')
->on('interactions.alert_id', '=', 'alerts.id')
->where('interactions.user_id', '=', $user->id)
->where('alerts.hide', '=', 0);
})
->leftJoin('alerts', function($join)
{
$join->on('users.id', '=', 'alerts.user_id')
->where('alerts.user_id', '=', $user->id)
->where('alerts.hide', '=', 0);
})
->leftJoin('user__followers as user__following', function($join)
{
$join->on('users.id', '=', 'user__followers.user_id')
->where('user__followers.user_id', '=', $user->id)
->where('user__followers.follower_id', '=', $searchid);
})
->leftJoin('user__followers as following', function($join)
{
$join->on('users.id', '=', 'user__followers.user_id')
->where('user__followers.user_id', '=', $user->id);
})
->leftJoin('user__followers', function($join)
{
$join->on('users.id', '=', 'user__followers.user_id')
->where('user__followers.follower_id', '=', $user->id);
})
->where('users.id', '=', $user->id)
->get();
I also have this query in raw sql so you can see what I'm trying to achieve:
SELECT u.username, u.profile_picture, u.screen_state,
(SELECT count(DISTINCT r.id)
FROM `reply` r
LEFT JOIN alerts a
ON r.alert_id = a.alert_content_id
WHERE r.user_id = :userid AND a.hide=0)
AS replies,
(SELECT count(DISTINCT i.id)
FROM `interactions` i
LEFT JOIN alerts a
ON i.alert_id = a.alert_content_id
WHERE i.user_id = :userid AND a.hide=0)
AS interactions,
(SELECT count(DISTINCT a.alerts)
FROM `alerts` a
WHERE a.user_id = :userid AND a.hide=0)
AS alerts,
(SELECT count(DISTINCT uf.id)
FROM `user_followers` uf
WHERE uf.user_id = :userid
AND uf.follower_id = :searchid)
AS following,
(SELECT count(DISTINCT uf.id)
FROM `user_followers` uf
WHERE uf.user_id = :userid)
AS volgend,
(SELECT count(DISTINCT uf.id)
FROM `user_followers` uf
WHERE uf.follower_id = :userid)
AS followers
FROM users u
WHERE id = :userid GROUP BY id
But this gives me an error:
Call to undefined method Illuminate\Database\Query\JoinClause::leftJoin()
So my question is how can i join a table and check for a condition in yet another table?

Get all objects from database that are not in a relationship yet

I have the following query and it's doing it's job fine:
SELECT exams.id, exams.date FROM exams
WHERE exams.modul_id = (SELECT questions.modul_id FROM questions where questions.id = 5)
AND NOT EXISTS (
SELECT * FROM exam_question, questions
WHERE questions.id = 5
AND questions.id = exam_question.question_id
AND exam_question.exam_id = exams.id
)
It returns me all exams that belong to the same model as exam but not connteced to exam yet.
I want to use this query in Laravel but I always get an empty result back (it shouldn't be empty)
DB::table('exams')
->select(['id', 'date'])
->whereRaw('modul_id = '.$question->modul_id)
->whereNotExists(function ($query) use ($question) {
$query->select(DB::raw(1))
->from('questions as q')
->join('exam_question as eq', 'q.id', '=', 'eq.question_id')
->join('exams as e', 'eq.exam_id', '=', 'e.id')
->whereRaw('q.id = '.$question->id);
})
->get();
The output of the laravel expression is:
select `id`, `date` from `exams` where modul_id = 1 and not exists (select 1 from `questions` as `q` inner join `exam_question` as `eq` on `q`.`id` = `eq`.`question_id` inner join `exams` as `e` on `eq`.`exam_id` = `e`.`id` where q.id = 5)
I see problem in your sql query dump in section and not exists (... you select starts with select 1 from it will always select number 1 from your table according to selected rows count
select `id`, `date` from `exams`
where modul_id = 1
and not exists (
select 1 from `questions` as `q`
inner join `exam_question` as `eq` on `q`.`id` = `eq`.`question_id`
inner join `exams` as `e` on `eq`.`exam_id` = `e`.`id` where q.id = 5)
You have to use in query builder instead $query->select(DB::raw(1)) ... -> $query->select('*') ...
DB::table('exams')
->select(['id', 'date'])
->whereRaw('modul_id = '.$question->modul_id)
->whereNotExists(function ($query) use ($question) {
$query->select('*')
->from('questions as q')
->join('exam_question as eq', 'q.id', '=', 'eq.question_id')
->join('exams as e', 'eq.exam_id', '=', 'e.id')
->whereRaw('q.id = '.$question->id);
})
->get();
If the rest logic which you used to create your query is correct it will start to work

Laravel ORM query builder

I have the following interrogation that I want to translate into Laravel's Query Builder:
SELECT t.name, t.description, u.first_name as assignee_fname, u.last_name as assignee_lname,
us.first_name as reporter_fname, us.last_name as reporter_lname
FROM `tasks` AS t
LEFT JOIN `users` AS u
ON (t.asignee_id = u.id)
LEFT JOIN `users` AS us
ON (t.reporter_id = us.id);
Do you have any idea how can I do that?
Thank you
DB::table('tasks')->select('tasks.name', 'users.first_name AS assignee_fname', ...) //and the rest of your column
->leftJoin('users', 'taks.asignee_id', '=', 'users.id')
->leftJoin('users', 'tasks.reporter_id', '=', 'users.id')

Categories