Conver MySQL query with LEFT statment to Laravel Eloquent - laravel-5

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

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

Select in select in laravel

I recently started learning Laravel and cannot solve my problem with queries.
SELECT * FROM films WHERE id IN (SELECT film_id FROMfavorites WHERE user_id = 2)
This is the correct code, but how to make it for Laravel
I tried to look in documentation, but it's all not that. Maybe someone knows how to solve this problem?
You can use Eloquent model with whereIn() just like below
Films::whereIn('id',Favorites::where('user_id', 2)->pluck('film_id'))->get();
and also if you use Eloquent model with relation, then it'll be more simple as below
$user->favorites()->films
To build it as a query you can use whereIn
\DB::table('films')->whereIn('id', function($query) {
$query->select('id')->from('favorites')->where('user_id',2);
})->toSql();
this will generate
select * from `films` where `id` in (select `id` from `favorites` where `user_id` = ?)

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.

Sub-queries in Laravel

I have a query in MySQL
SELECT * FROM group_recordings WHERE id IN ( SELECT MAX(id) FROM group_recordings GROUP BY time_span )
there is a sub-query in "IN" operator, I want to convert this query in laravel eloquent, I have GroupRecording Model. Anyone help me with that
I think the Laravel style of such query will be,
GroupRecording::whereIn('id', function($query) {
$query->selectRaw('MAX(id)')
->from('group_recordings')
->groupBy('time_span');
})
->where('time_span', '>', $recording->time_span)
->get();

How to write sub query sql in laravel 4.2

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

Resources