Laravel Pagination group by year and month only [duplicate] - php

This question already has answers here:
Laravel pagination not working with group by clause
(5 answers)
Closed 3 years ago.
I want to make paginate from laravel, group by year and month only
when i used get , it work well
$data = DB::table('gallery')
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah'),'gallery.*')
->where('type','Foto')
->groupBy('tanggal')
->orderBy('tanggal','desc')
->get();
but when i use paginate , there is an error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tanggal' in 'group statement' (SQL: select count(*) as aggregate from gallery where type = Foto group by tanggal)
$data = DB::table('gallery')
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah'),'gallery.*')
->where('type','Foto')
->groupBy('tanggal')
->orderBy('tanggal','desc')
->paginate(2);
Or any idea for group by just year and month laravel 5.4?

From https://laravel.com/docs/5.4/pagination
Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.
Here's an alternative solution provided on Jen's Segers's blog

Related

Using DB::select in Laravel 9, but result not same in table database [duplicate]

This question already has an answer here:
Having issue with matching rows in the database
(1 answer)
Closed 7 months ago.
I have tried to make some query in laravel, sometimes work, but sometime not work,
the problem is, result of query i made, its not same with actual value on table database, i have no idea to fix that, and iam have a trying to searching this problem in google or stackoverflow, but cannot find issue like that, can anyone tell me about what this problem.
Query SQL
SELECT id, parent_id, status, name FROM works WHERE (status != 'CANCELED' AND deleted_at IS NULL) AND (parent_id = 20 AND type = 'TASK')
image result of sql query
Query Laravel
DB::select("SELECT id, parent_id, status, name FROM works WHERE (status != 'CANCELED' AND deleted_at IS NULL) AND (parent_id = 20 AND type = 'TASK')")
image result of dd in laravel
result of end data with id 66 its cannot founded in table, so iam realy confused, in table iam search data with id 66 its not found, but in result query of laravel made the data its show.
i feel like watching horor movie .
if u have Work model..try this
$works = Work::where('status','!=','CANCELLED')->whereNull('deleted_at')->where('parent_id',20)->where('type','TASK')->get(['id', 'parent_id', 'status', 'name']);

How can I convert this SQL query to Laravel Eloquent?

One of our teams asked this question earlier today, and there aren't any right answers still. Perhaps the way he worded the question wasn't correct—all in all, this is the SQL query I want to convert to Laravel Eloquent:
SELECT * FROM drivers
where driver_number
NOT IN (SELECT driver_number FROM buses)
AND station_id = 2 OR driver_number = 'Dr_02'
Try this:
Driver::whereNotIn('driver_number', Buse::select('driver_number'))
->where('station_id', 2)
->orWhere('driver_number', 'Dr_02');
If the Buse select gives you an error, add the get method at the end of it:
Buse::select('driver_number')->get()

(Laravel) can't use group by with order by [duplicate]

This question already has answers here:
Group by not working - Laravel
(8 answers)
Closed 3 years ago.
I'm trying to use order by without adding the order column in groupby, it only works if I execute it directly from the database but from laravel I get database error
I made this eloquent code
Comment::select('product_id')->where('shop_name', $shop)->groupby('product_id')->distinct()->orderBy('created_at')->paginate(12)
it will product the following query
select distinct DISTINCT(product_id) from comments where shop_name
= 'shopname' group by product_id order by created_at asc limit 12 offset 0
if I rub the above query directly in database it works
but if I use Laravel eloquent code it fires this error
SQLSTATE[42000]: Syntax error or access violation: 1055
'areviews_areviewzappz.comments.created_at' isn't in GROUP BY (SQL:
select distinct DISTINCT(product_id) from comments where shop_name
= 'shopname' group by product_id order by created_at asc limit 12 offset 0)
how can I solve this issue ?
The issues is that you SHOULD really include the ORDER BY in the GROUP BY list as this is best practise.
The reason it works when you are on the the sql mode set to ''. However, Laravel by default (I think) has Strict as TRUE,
You have 2 options:
Add the created_at to the GROUP BY clause (Recommended)
Change the strict mode to false
A bit more info on the 2nd option:
How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?
You can group it after you have retrieved the results rather than in the MySQL query - so you group the collection after it has been retrieved, not the table entries in the query itself.
Comment::select('product_id')
->where('shop_name', $shop)
->distinct()
->orderBy('created_at')
->paginate(12)
->groupBy('product_id')
Laravel collection groupBy method: https://laravel.com/docs/5.7/collections#method-groupby
Related post: Laravel Query Buider Group By Not Getting All The Records

Laravel groupBy violation [duplicate]

This question already has answers here:
Error related to only_full_group_by when executing a query in MySql
(18 answers)
Closed 4 years ago.
I want to get all messages for auth user in his inbox with name of people who send him messages.
I have query
$user_id=Auth::id();
$messages=Chat::with('user')
->where('user_id',$user_id)
->groupBy('friend_id')
->limit(10)
->get();
But Laravel won't me to get that and said:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mojaodeca.chats.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from chats where user_id = 1 group by friend_id limit 10)
use this
$user_id=Auth::id();
$messages=Chat::with('user',function($q){
$q->groupBy('friend_id')
})
->where('user_id',$user_id)
->limit(10)
->get();
Please check MySQL Handling of GROUP BY - Your select list must be in your aggregate functions.
$messages = Chat::select('user_id', DB::raw('COUNT(user_id) as count'))
->with('user')
->groupBy('friend_id')
->limit(10)
->get();

#1093 - You can't specify target table 'installments' for update in FROM clause [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 6 years ago.
I Tried
DELETE FROM installments
WHERE EXISTS (SELECT * FROM `installments`
JOIN student
ON installments.s_id=student.s_id
WHERE installments.curr_sem=3
AND student.bat_id=10 AND student.p_id=170 AND student.DeleteStatus=0);
but show error
#1093 - You can't specify target table 'installments' for update in FROM clause
Please help me
Try this solution using DELETE JOIN:
DELETE
i
FROM
installments i JOIN student s USING (s_id)
WHERE
i.curr_sem=3 AND
s.bat_id=10 AND
s.p_id=170 AND
s.DeleteStatus=0
The correct form for your statement is the following:
DELETE installments.*
FROM installments
JOIN student ON installments.s_id=student.s_id
WHERE installments.curr_sem=3
AND student.bat_id=10
AND student.p_id=170
AND student.DeleteStatus=0;
If I understand it right, you want to delete only records in installments that match the join condition. You can delete or update records in one table with conditions on a join and there is no need for the nested select.
If you are working with Workbench you will have to disable safe mode or you can get an error in case you do not specify a key column in where.
Let me have your feedback
Regards

Categories