Convert query from Mysql to Eloquent Laravel - laravel-4

I need Help to Convert Mysql Query to Eloquent Laravel
SELECT SUM((id_user=7) * `commission_agent`) AS sum7,SUM((id_user=8) * `commission_agent`) as sum8 FROM agents_commission

if you have the agents commission model set up correctly, you can use something like:
AgentCommission::select(DB::raw("SUM((id_user=7) *commission_agent) AS sum7,SUM((id_user=8) *commission_agent) as sum8"))->from(agents_commission)->get();

Related

Laravel Eloquent 'WITH' statement query

How to convert this mysql query to Eloquent
with wmh as (select * from whatsapp_message_histories ORDER BY created_at LIMIT 1)
select DISTINCT whatsapp_histories.*, wmh.created_at from whatsapp_histories
left join wmh on wmh.whatsapp_history_id = whatsapp_histories.id
ORDER BY wmh.created_at
Please Help me, thanks a lot!
(Edited)
My Problem is, I have Eloquent Model whatsapp_histories, has a relation to whatsapp_message_histories (using hasMany). My Objective is need to display whatsapp_histories order by whatsapp_message_histories.created_at. (This is eloquent, because i need to display other whatsapp_histories's relation). I can solve it in native way, but not in eloquent, but i'm not allowed to change the previous eloquent code
Try this:-
whm::with('whatsapp_histories',function($join){
$join->on('wmh.whatsapp_history_id','=','whatsapp_histories.id');
$join->orderBy('whatsapp_histories.created_at','ASC');
$join->limit(1);
})->orderBy('wmh.created_at','ASC)->get();

Laravel Eloquent and SUM() - Do not consider the decimal

I run in a strange issue, I need to sum a column using eloquent but every function I use eloquent do not consider the decimal.
First of all I have the column set ad DECIMAL(8,2) and in my DB the data is stored in the correct way.
If I just use a simple query:
select sum(total_after_tax) from invoice_details where invoice_id = 1
I get the result: 312.93
Now if I use eloquent:
$invoice = Invoice::with(['details'])->get();
dd($invoice->first()->details->sum('total_after_tax'));
I get: 312.
How is this possibile? How can I get the correct value form DB?

how would you write complex query in laravel 5.3

I want to run following query in laravel using eloquent model
This is my raw query in mysql
SELECT `m`.`id` as `mid`,`m`.`code`,`oi`.`id` as `oi_id` FROM `member` `m`,`order` `o`, `order_item` `oi`
where
(`m`.`id`=`o`.`mid` OR `m`.`code`=`o`.`code`)
AND
`o`.`id`=`oi`.`order_id`
AND
(`oi`.`from_date` <= '{$selectdate}' AND `oi`.`to_date` >= '{$selectdate}' )
And this is what i have tried in laravel
$record = \App\Member::join('orders','members.id','=','order.mid')
->join('order_item','order.id','=','order_item.order_id')
->select('id','code','order.id','order_item.id')
->where([['order_item.fromdate','<=','{$selectdate}'],['order_item.todate','>=','{$selectdate}'])->get();
I am getting confused with the syntax of laravel for same query. please help me?

How do I create a query in Laravel 5.2 query builder, group join

I have the following sql PDO query.
("SELECT
actual_remains.quantity,
ingredients.name,
ingredients.unit,
ingredients.id
FROM
actual_remains
LEFT JOIN
ingredients ON ingredients.id = actual_remains.ingredient
WHERE
actual_remains.shop = ".$shopNumber);
How would I make this query in laravel query builder?
Here is the straight Query builder way to write this (See Laravel Docs for more information):
$actualRemains = DB::table('actual_remains')
->leftJoin('ingredients', 'actual_remains.ingredient', '=', 'ingredients.id')
->select('actual_remains.quantity', 'ingredients.name', 'ingredients.unit', 'ingredients.id')
->where('actual_remains.shop', '=', $shopNumber)
->get();
If you create models and set their relationships you will probably have something more like this:
(new App\ActualRemain)->find($shopNumber)->with('ingredients');
See Laravel Docs on relationships for more information on eloquent relationships.

How do I get a "select count(*) group by" using laravel eloquent

I would like to execute the follow sentence using laravel eloquent
SELECT *, count(*) FROM reserves group by day
The only solution occurs to me is to create a view in the DB, but I am pretty sure there is a way to do it in laravel way.
You could use this:
$reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day');
As you wish to do it with Laravel Eloquent I assume you have a model name Reserve. In this case you can use this
$reserve = Reserve::all()->groupBy('day')->count();
You could use:
#Laravel Raw Expressions
$reserves = DB::table('reserves')
->select(DB::raw('count(*) as reserves_count'))
->groupBy('day')
->get();
OR
$reserves = Reserve::select(['reserves.*'])
->groupBy('day')
->count();
Further read here

Resources