Convert sql query to query builder using laravel 5.4 - laravel

Hi I my try to convert SQL query to Query Builder. My Sql query below like this
SELECT exclusive_distributor_opens.distribution_code,exclusive_distributor_opens.distribution_name,exclusive_item_opens.item_code,exclusive_item_opens.item_name,SUM(exclusive_budget_items_details.dealer_contribution) as Total,exclusive_distributor_opens.date
FROM exclusive_budget_masters
JOIN exclusive_distributor_opens
ON exclusive_budget_masters.distributor_id=exclusive_distributor_opens.id
JOIN exclusive_budget_items_details
ON exclusive_budget_masters.distributor_id=exclusive_budget_items_details.budget_master_id
JOIN exclusive_item_opens
ON exclusive_budget_items_details.item_id=exclusive_item_opens.id
GROUP BY (exclusive_distributor_opens.distribution_code)
HAVING exclusive_distributor_opens.date BETWEEN '2018-12-19' AND '2018-12-19'
ORDER BY exclusive_distributor_opens.distribution_code DESC
2.My Query Builder Query like this
$results = DB::table('exclusive_budget_masters')
->join('exclusive_distributor_opens', 'exclusive_budget_masters.distributor_id', '=', 'exclusive_distributor_opens.id')
->join('exclusive_budget_items_details', 'exclusive_budget_masters.distributor_id' ,'=', 'exclusive_budget_items_details.budget_master_id')
->join('exclusive_item_opens', 'exclusive_budget_items_details.item_id', '=', 'exclusive_item_opens.id')
->select('exclusive_distributor_opens.distribution_code', 'exclusive_distributor_opens.distribution_name','exclusive_budget_items_details.customar_contribution', DB::raw('SUM(exclusive_budget_items_details.customar_contribution) As Total'))
->whereBetween('exclusive_distributor_opens.date', array('2018-12-19', '2018-12-19'))
->groupBy('exclusive_distributor_opens.distribution_code', 'exclusive_distributor_opens.distribution_name','exclusive_budget_items_details.customar_contribution')
->orderBy('exclusive_distributor_opens.date', 'DESC')
->get();
My Query Output Result Comes like this:
But My Expected Result looks like this:
Please anybody help me.

Why do you expect this to work?
Select part is missing or wrong:
exclusive_item_opens.item_code missing
exclusive_item_opens.item_name missing
exclusive_distributor_opens.date missing
SUM Total column is wrong:
SUM(exclusive_budget_items_details.dealer_contribution) as Total vs
DB::raw('SUM(exclusive_budget_items_details.customar_contribution) As Total')
Your builder query looks like this:
SELECT `exclusive_distributor_opens`.`distribution_code`,
`exclusive_distributor_opens`.`distribution_name`,
`exclusive_budget_items_details`.`customar_contribution`,
Sum(exclusive_budget_items_details.customar_contribution) AS Total
FROM `exclusive_budget_masters`
INNER JOIN `exclusive_distributor_opens`
ON `exclusive_budget_masters`.`distributor_id` =
`exclusive_distributor_opens`.`id`
INNER JOIN `exclusive_budget_items_details`
ON `exclusive_budget_masters`.`distributor_id` =
`exclusive_budget_items_details`.`budget_master_id`
INNER JOIN `exclusive_item_opens`
ON `exclusive_budget_items_details`.`item_id` =
`exclusive_item_opens`.`id`
WHERE `exclusive_distributor_opens`.`date` BETWEEN ? AND ?
GROUP BY `exclusive_distributor_opens`.`distribution_code`,
`exclusive_distributor_opens`.`distribution_name`,
`exclusive_budget_items_details`.`customar_contribution`
ORDER BY `exclusive_distributor_opens`.`date` DESC

Related

OrderBy fixed to asc on Eloquent query

I try to convert the following SQL request in an Eloquent query :
SELECT locals.*
FROM prises JOIN locals
ON prises.liaison_id = locals.id
GROUP BY locals.id
ORDER BY COUNT(liaison_id);
And I wrote this :
return $query->select('locals.*')
->from('prises')
->join('locals', function($join) {
$join->on('prises.liaison_id', '=', 'locals.id');
})
->orderBy(DB::raw('count(prises.liaison_id)', 'DESC'))
->groupBy('locals.id');
On my php page, it works, but this query is running :
select "locals".* from "prises"
inner join "locals" on "prises"."liaison_id" = "locals"."id"
group by "locals"."id"
order by count(prises.liaison_id) asc limit 100 offset 0
My Asc/Desc is fixed, and I don't know why. Is using DB::raw that block me ?
Thanks in advance.

Converting a raw query to Laravel query builder

I have the following MySQL query which fetches a list of the last 9 authors to write a post and lists them in order of the date of the last post they wrote.
It's working properly but I'd like to re-write it using the Laravel Query Builder. Here is the query at the moment:
$authors = DB::select("
SELECT
`a`.`id`,
`a`.`name`,
`a`.`avatar`,
`a`.`slug` AS `author_slug`,
`p`.`subheading`,
`p`.`title`,
`p`.`slug` AS `post_slug`,
`p`.`summary`,
`p`.`published_at`
FROM
`authors` AS `a`
JOIN
`posts` AS `p`
ON `p`.`id` =
(
SELECT `p2`.`id`
FROM `posts` AS `p2`
WHERE `p2`.`author_id` = `a`.`id`
ORDER BY `p2`.`published_at` DESC
LIMIT 1
)
WHERE
`a`.`online` = 1
ORDER BY
`published_at` DESC
LIMIT 9
");
I understand the basics of using the query builder, but there doesn't appear to be anything in the Laravel docs that allows for me to JOIN a table ON a SELECT.
Can anyone suggest a way that I can write this query using the Laravel Query builder, or perhaps suggest a way that I can rewrite this query to make it easier to structure with the query builder?
Try to do like this
$data = DB::table('authors')
->select(
'a.id',
'a.name',
'a.avatar',
'a.slug AS author_slug',
'p.subheading',
'p.title',
'p.slug AS post_slug',
'p.summary',
p.published_at')
->from('authors AS a')
->join('posts AS p', 'p.id', '=', DB::raw("
(
SELECT p2.id FROM posts AS p2
WHERE p2.author_id = b.id
ORDER BY p2.published_at
DESC LIMIT 1
)"))
->where('a.online', 1)
->limit(9)
->orderBy('p.published_at', 'desc')
->get();

Need to convert raw sql to eloquent query builder

I need to convert the following sql query to eloquent query builder.
select * from projects
inner join assigned_projects on projects.id = assigned_projects.project_id
where assigned_projects.user_id=9;
where projects and assigned_projects are in many to many relation. Please help me figure it out.
$project = DB::table('projects')
->join('assigned_projects' , 'projects.id' , '=', 'assigned_projects.project_id')
->where('assigned_projects.user_id', '=', 9)
->get();

eloquent where not in query?

I'm trying to build the following sql query with eloquent. The query gives me all records from table_a which are in the list of ids and do not appear in table_b.
select * from table_a
where id in (1,2,3)
and id not in
(select tablea_id from table_b
where tablea_id in (1,2,3))
So how do I do it in eloquent ? I want to avoid using a raw query.
//does not work
TableA::whereIn('id',$ids)
->whereNotIn('id', TableB::select('tabla_id')->whereIn($ids));
To run a subquery you have to pass a closure:
TableA::whereIn('id',$ids)
->whereNotIn('id', function($q){
$q->select('tabla_id')
->from('tableb');
// more where conditions
})
->get();

Laravel ordering results of a left join

I am trying to replicate the below SQL in Laravels Eloquent query builder.
select a.name, b.note from projects a
left join (select note, project_id from projectnotes order by created_at desc) as b on (b.project_id = a.id)
where projectphase_id = 10 group by a.id;
So far I have:
$projects = Project::leftjoin('projectnotes', function($join)
{
$join->on('projectnotes.project_id', '=', 'projects.id');
})
->where('projectphase_id', '=', '10')
->select(array('projects.*', 'projectnotes.note as note'))
->groupBy('projects.id')
->get()
which works for everything except getting the most recent projectnotes, it just returns the first one entered into the projectnotes table for each project.
I need to get the order by 'created_at' desc into the left join but I don't know how to achieve this.
Any help would be much appreciated.
Your subquery is unnecessary and is just making the entire thing inefficient. To be sure though, make sure this query returns the same results...
SELECT
projects.name,
notes.note
FROM
projects
LEFT JOIN
projectnotes notes on projects.id = notes.project_id
WHERE
projects.projectphase_id = 10
ORDER BY
notes.created_at desc
If it does, that query translated to the query builder looks like this...
$projects = DB::table('projects')
->select('projects.name', 'notes.note')
->join('projectnotes as notes', 'projects.id', '=', 'notes.project_id', 'left')
->where('projects.projectphase_id', '=', '10')
->orderBy('notes.created_at', 'desc')
->get();

Resources