How to write sub query sql in laravel 4.2 - laravel

hy I'm new to Laravel 4.2 And I have Sub Query Sql Like this
Select *, Count(*) as total_rows From(
SELECT test_id,
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum,
SUM(IF(app.status='process',apt.result,0)) AS process_sum
FROM application_test AS apt
JOIN application AS app ON app.id=apt.application_id
GROUP BY apt.test_id)
my quetion is how I can write this sub query sql on laravel 4.2

You can make subqueries like this.
Test::where('id', function($query){
$query->select('other_test_id')
->whereIn('category_id', ['223', '15'])
->where('active', 1);
})->get()
If you're new on Laravel, consider to use the last version of it, the 5.2 ;)

Convert the MYSQL CASE INTO LARAVEL Query
$query = DB::raw("(CASE WHEN user_group='1' THEN 'Admin' WHEN user_group='2' THEN 'User' ELSE 'Superadmin' END) as name");
and simply execute this query in
DB::table('tablename')->select($query)->get();
or
YourModelClass::select($query)->get();
You will get the result.
Note: Syntax is based on Laravel 5.0

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

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.

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

Conver MySQL query with LEFT statment to Laravel Eloquent

Could anybody help to translate below query to Laravel Eloquent syntactics.
SELECT LEFT(subject , 10) FROM tbl
Thanks.
on the top of your Class, Call this
use DB;
then type this as your query
DB::table('tbl')->select('LEFT(subject , 10)')->get();

Resources