subquery inside select of Laravel with Eloquent ORM with - laravel

I want to execute following raw query with laravel eloquent ORM:
SELECT *,(SELECT country FROM tbl_sim_numbers WHERE highland_id= b.highland_id) as country1,(SELECT operator FROM tbl_sim_numbers WHERE highland_id= b.highland_id) AS operator1 FROM tbl_sim_distributions b ORDER BY b.created_at
on eloquent ORM of laravel. I tried:
$sub = DB::select(DB::raw("SELECT *,(SELECT country FROM tbl_sim_numbers WHERE highland_id= b.highland_id) as country1,(SELECT operator FROM tbl_sim_numbers WHERE highland_id= b.highland_id) AS operator1 FROM tbl_sim_distributions b
ORDER BY b.created_at"));
$pdfGenObjForList = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery());
However above code gives error.Someone,please help.

Related

how to write query with sub query distinct eloquent lumen laravel

so i had a query with 2 level nested sub query. but im confused how to convert it into eloquent lumen. any helper ?
my query
SELECT b.poid,
STRING_AGG( b.epurno, '#' ) as aks
FROM (SELECT DISTINCT *
FROM (SELECT hd.poid, so.epurno
FROM trpohd hd
JOIN trpodt dt ON hd.poid = dt.poid
JOIN trsalesorder so ON dt.sohdid = so.sono
ORDER BY so.sono
) as a
) as b
GROUP BY poid)

date method in eloquent querybuilder

i have this raw query and i want to use it in eloquent query builder
but seems i cant use date method in eloquent and gave me this error im new in eloquent. what is the problem:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date(prizes.created_at), user_id' in 'group statement' (SQL: select user_id,COUNT(user_id), date(created_at) from `prizes` group by `date(prizes`.`created_at), user_id` having `user_id` = 1 order by `date(created_at)` desc)
raw SQL:
SELECT
user_id,COUNT(user_id), DATE(created_at) FROM prizes
GROUP BY DATE(prizes.created_at), user_id
HAVING user_id = 1
ORDER BY DATE(created_at) DESC
limit 2
Eloquent:
$points = \App\Prize::selectRaw('user_id,COUNT(user_id), date(created_at)')
->groupBy("date(prizes.created_at), user_id")
->orderBy("date(created_at)","DESC")
->having("user_id","=",1)
->get();
what is the the cleanest and best form??
By default, Laravel tries to parse all strings as tables. This means it will add the string between `.
To avoid this, you can put the string in a DB:raw() function to let Laravel know not to parse this string and send it as-is to the database.
->orderBy(\DB::raw("date(created_at)"), "DESC")
Or use the raw method for ordering:
->orderByRaw('date(created_at) desc')

Complex SQL Query laravel Build

How would I use query builder in Laravel to generate the following SQL statement:
SELECT MAX(QTE) FROM (SELECT SUM(activity_sale_report.quantity_sold)
AS QTE FROM activity_sale_report
GROUP BY activity_sale_report.activity_id) AS T
If you are just using the query builder and not models you can use raw queries
$query = DB::select( DB::raw("SELECT MAX(QTE) FROM (SELECT SUM(activity_sale_report.quantity_sold) AS QTE FROM activity_sale_report GROUP BY activity_sale_report.activity_id) AS T);
I would recommend using the eloquent models, it's easier.

Laravel 5.4 Eloquent multiple table query

Hi I want to form a query in laravel 5.4 eloquent for the following SQL:
select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id
All the foreign keys are properly given and the models are also properly created.
My aim is to get all the master record name like category, client name, etc to come in the child table record set directly.
You can try this:
DB::table('payments')
->select('payments.*',DB::raw('CONCAT(clients.firstname," ",clients.lastname) as clientname'),categories.category)
->join('clients','clients.id','=','payments.client_id')
->join('categories','categories.id','=','payments.category_id')
->get();
Hope this help for you !!!
Paste your row query in DB::row() and print data like following
$data= DB::row("select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id")->get();
dd($data);
if your query is right and properly managed relation than its retrieve data
If you already made Models than Why you don't use ORM?
Use Models,
payment::leftJoin('clients ','payments.client_id','=','clients.id')
->leftJoin('categories ','payments.category_id ','=','categories .id')->select(['payment.*','categories.category',DB::raw("CONCAT(clients.firstname,' ',clients.lastname) as clientname")])->get();

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

Resources