dimanche 19 avril 2015

laravel eloquent select first row of each group by

I have subjects table and tutorials table in sqlite database. I am trying to select current tutorial. There are my tables



subjects table
-------------------
| id | name |
-------------------
| 1 | Chemistry |
| 2 | Physic |
-------------------
tutorials table
-----------------------------------------
| id | name | subj_id | completed |
-----------------------------------------
| 1 | chapter 1 | 1 | 1 |
| 2 | chapter 2 | 1 | 0 |
| 3 | chapter 3 | 1 | 0 |
| 4 | chapter 1 | 2 | 1 |
| 5 | chapter 2 | 2 | 1 |
| 6 | chapter 3 | 2 | 0 |
| 7 | chapter 4 | 2 | 0 |
-----------------------------------------


My current eloquent is



$query->where('completed', false)->groupBy('subj_id')->get();


It return



-----------------------------------------
| id | name | subj_id | completed |
-----------------------------------------
| 3 | chapter 3 | 1 | 0 |
| 7 | chapter 4 | 2 | 0 |
-----------------------------------------


what i wish to select is id 2 and 6.



-----------------------------------------
| id | name | subj_id | completed |
-----------------------------------------
| 2 | chapter 2 | 1 | 0 |
| 6 | chapter 3 | 2 | 0 |
-----------------------------------------


How can I get id 2 and 6. I am using sqlite.


Aucun commentaire:

Enregistrer un commentaire