how would you write complex query in laravel 5.3 - laravel

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?

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 Query Result Issue: query is returning empty array even there is data exist into database

I am working on laravel 5.4. I am trying to get data from database using below query:
$obj = \Illuminate\Support\Facades\DB::table('A')
->join('B', 'A.Intake', '=', 'B.Id')
->join('C', 'B.StreamId', '=', 'C.StreamId')
->select(\Illuminate\Support\Facades\DB::raw('DISTINCT(C.StreamId) As StreamId'))
->where('A.YearId', $yearId);
$streamIds = $obj->get()->toArray();
The above query is returning empty results. I have also debug the query generated by laravel. Below is the raw query generating by laravel on the basis of above conditions:
select DISTINCT(C.StreamId) As StreamId from A
inner join B on A.Intake = B.CentreStreamId
inner join C on B.StreamId = C.StreamId
where A.YearId = '12'
When I run the above raw query directly in my database, then it returns me some records. But when I try to get records in laravel then it returns me empty results.
I am not able to get the issue with the above query. Can someone please tell me why it is returning empty results set? If there is any issue with above query builder syntax then please correct my query.
Thanks in Advance.

Query_while in Laravel

How would I do such a similar query with laravel?
$GLOBALS['db']->query_while("SELECT * FROM games
WHERE id_gioco='".$id_gioco."' AND id_utente='".$USER['id']."'");
In the documentation of laravel and Eloquent, I found nothing on query_while.
Not sure what a while query is , but the following should work. If you chain 2 where clauses it will make it an AND where()
$games = Game::where('id_gioco', $id_gioco)->where('id_utente', $user->id)->get()

proper usage of queries (Aggregates) in laravel 4.2

im trying to get the total count of records that are less than a value in my table so im using this query in laravel 4.2
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft','<','ModuleCriticalLevel')
->count();
and passes it like this
return View::make('ssims.board' ,compact('title','mTotal','critical'));
//please don't mind the others
then receives it in the view page like this
<div>Modules at critical level <span><strong><?= $critical ?></strong></span></div>
unfortunately, im getting zero whereas in my database, i have 2 records where ModuleCountLeft is less than ModuleCriticalLevel
any ideas?
thanks
Hi turns out that i needed to pluck out the value of ModuleCriticalLevel first here is the code
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' , '<' , DB::table('dbo_modules')->pluck('ModuleCriticalLevel') )
->count();
-thumbs up here-

Convert query from Mysql to Eloquent Laravel

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

Resources