Laravel Eloquent Multiple Select - laravel

I'm just wondering if multiple select queries work in eloquent such as the code shown below
$sql = DB::('table')
->select('column1')
->selectRaw('raw select')->get();

Related

How can I write the below pure SQL statement in laravel without using DB:raw

I'm trying to show the total price according to month. I know how to retrieve the data with pure SQL statements but I don't know a way to apply it inside Laravel. And also I don't want to use DB::raw(). I need help!! Below is the pure SQL statement.
SELECT month(dt.created_at) as Month,SUM(dp.price) as Total_Price
FROM datapack_transactions dt
INNER JOIN datapack_packages dp ON dt.package_id=dp.id
GROUP BY month(dt.created_at);
Below is the result of the above pure SQL statement.
I want to use the Laravel Eloquent instead of using DB::raw().
Try this query:
$orders = datapack_transactions::select(
DB::raw('month(datapack_transactions.created_at) as Month'),
DB::raw("SUM(datapack_packages.price) as Total_Price")
)
->join('datapack_packages','datapack_packages.id','=','datapack_transactions.package_id')
->groupBy('datapack_transactions.created_at')
->get();
//Change model and columns as per yours

How to do this laravel subquery

How can I make this query in Laravel:
SELECT * FROM posts
LEFT OUTER JOIN (SELECT * from subscribers where subscribers.post_id=2)
as sub ON sub.university_id = universities.id
try this, i get that from sql to laravel query builder converter online
DB::table('posts')
->leftJoin('(SELECT * from subscribers where subscribers.post_id=2) as
sub','sub.university_id','=','universities.id')
->get();

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.

Rewrite SQL Query for Use With Laravel Query Builder

$query = "SELECT *, (id.number_of_candidates - t.counted) as available
FROM interview_dates id
LEFT JOIN (SELECT interview_dates_id,COUNT(interview_dates_id) as counted
FROM date_timeslot GROUP BY interview_dates_id) as t
ON t.interview_dates_id=id.id
WHERE id.number_of_candidates > t.counted";
Please help me to rewrite this using Laravel Query Builder.

subquery inside select of Laravel with Eloquent ORM with

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.

Resources