Eloquent: How to Group with Where

29 views Asked by At

I have the following data

id - code - validity - created_at
1  - 1001 - 0        - 2019-01-29 09:00:00
2  - 1001 - 1        - 2019-01-29 09:30:00
3  - 1002 - 0        - 2019-01-29 09:40:00
4  - 1002 - 0        - 2019-01-29 10:00:00
5  - 1002 - 0        - 2019-01-29 10:10:00
6  - 1003 - 0        - 2019-01-29 10:15:00
7  - 1003 - 1        - 2019-01-29 10:15:00

My conditions are to get the record where validity of item (code) is all zero (or the fail record)

I wanted to get the

3  - 1002 - 0        - 2019-01-29 09:40:00
4  - 1002 - 0        - 2019-01-29 10:00:00
5  - 1002 - 0        - 2019-01-29 10:10:00

and finally get the latest record

5  - 1002 - 0        - 2019-01-29 10:10:00

what i tried.

$data = Table::groupBy('code')->where('validty',0)->get();
dd($data);

im getting all record with 0;

1

There are 1 answers

0
jeesoon On

Here is what worked for me.

$data = Table::groupBy('code')->havingRaw('SUM(validty) = 0')->pluck('code');