Query_while in Laravel - 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()

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

how to use between with laravel query builder

I am trying to use BETWEEN with laravel query builder. How do I do it. I have tried this below
Eventcalender::where(['between', 'acceptance_date', $final, $time])->count();
Check hist out: whereBetween()
The whereBetween method verifies that a column's value is between two values,
For Eloquent:
Eventcalender::whereBetween('acceptance_date', [$final, $time])->count();
For Query Builder:
DB::table('eventcalenders')->whereBetween('acceptance_date', [$final, $time])->count();

laravel where Clauses on related eloquent model

Hey why i cant use a where clause with 3 params on related eloquent models??
simple example (i want it for a <=)
$user->roles->where('active',1);
//is working
$user->roles->where('active','=',1);
//is not working
Can i use it with 3 params only in:
DB::table('users')->where('votes', '=', 100)->get();
and not in:
$xy->users->where('votes', '=', 100);
Thanks,
$user->roles is returning a Collection, which has a method where on it that accepts exactly two parameters:
$user->roles->where("key", "value");
As seen in the documentation: https://laravel.com/docs/5.4/collections#method-where
If you want to use three, you'll need to return a Query Builder instance:
$user->roles()->where("key", "operator", "value")->get();

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?

Group By Eloquent ORM

I was searching for making a GROUP BY name Eloquent ORM docs but I haven't find anything, neither on google.
Does anyone know if it's possible ? or should I use query builder ?
Eloquent uses the query builder internally, so you can do:
$users = User::orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 100)
->get();
Laravel 5
WARNING: As #Usama stated in comments section, this groups by AFTER fetching data from the database. The grouping is not done by the database server.
I wouldn't recommend this solution for large data set.
This is working for me (i use laravel 5.6).
$collection = MyModel::all()->groupBy('column');
If you want to convert the collection to plain php array, you can use toArray()
$array = MyModel::all()->groupBy('column')->toArray();
try: ->unique('column')
example:
$users = User::get()->unique('column');

Resources