Laravel Eloquent 'WITH' statement query - laravel

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

Related

Laravel query use parent column inside whereRelation

I have problem with my eloquent query, i need to use data of my base model into whereRelation.
I tried this query bottom, but results was not what i except. The query return me all users who have one city relation, not only user who have city updated between my last user sync.
$users = People::whereRaw('TIMESTAMPDIFF(SECOND, people.latest_sync, people.updated_at) > 20')
->orWhereRelation('city', 'updated_at', '>', 'people.updated_at')
->get();
I'v tried people.updated_at and latest_sync in value of my Where Relation
Do i need to make pure SQL Raw query with classic join ?
PS: the first whereRaw is ok, and work (i really need)

HasManyThrough Relations includes a Pivot

I have a Module, Question and Category model.
Module hasMany Questions. (1 to Many)
Question belongsToMany Categories. (Many to Many)
For a given Module, I would like to access only the Questions where category_id = X .
I'm not sure what the most efficient way of doing this is. Can I do it through a HasManyThrough relation between Module and Category? Or do I have to create a loop? Or do it through a raw SQL query?
Update:
This SQL query seems to work. However, I'm sure there must be a more elegant solution?
SELECT id
FROM questions
INNER JOIN category_question ON questions.id = category_question.question_id
WHERE category_question.category_id = X and module_id = Y;
You can achieve that using this
Question::with(['module','categories'])
->join('category_question','category_question.question_id','=','question.id')
->where('category_question.category_id','=','X')
->where('questions.module_id','=','module_id')->get();
Question::with(['model','categories'=>function($query){
return $query->where('category_question.category_id',$category_id);
}])->get();
hope this works.

Laravel Eloquent duplicate distinct row

Let's consider the image above. I would like to show duplicated entries as one entry and also I want to show the sum of the "stock" column. In this case it should be 5722.
Is it possible to do it using Eloquent? Or what are the best ways to do it?
Not sure how your database / query is built but you could maybe use something like that:
Item::groupBy('item_name')
->selectRaw('*, sum(stock) as sum')
->get();
It will return a collection of Item with an additional “sum” field
This will group the result with same medicine and supplier name and sum up the stock.
$result = Model_Name::groupBy('medicine_name','supplier_name')
->selectRaw('*, sum(stock) as sum')
->get();
Try this. Hope it might help you.

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

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