I have been trying to figure this out for some time and just can't get it to work. I have a table that looks like this.
Table: Problems
id yearly_issue year stock created_at updated_at magazine_id 1 10 2000 1 [timestamp] [timestamp] 3 2 12 1994 6 [timestamp] [timestamp] 10 3 36 2007 10 [timestamp] [timestamp] 102 4 6 2002 7 [timestamp] [timestamp] 7 5 6 2002 2 [timestamp] [timestamp] 5 6 12 2003 9 [timestamp] [timestamp] 10 7 11 2003 12 [timestamp] [timestamp] 10
My problem is that I need to sort it (just!), But I want to get only one journal (column magazine_id).
My current search request is:
$issues = Issue::where('stock', ($all ? '>=' : '>'), 0) ->orderBy('year', 'desc') ->orderBy('yearly_issue', 'desc') ->take($perpage) ->get();
I thought adding groupBy('magazine_id') would help, but it seems like it only partially helps me. The results are not in the correct order. So now my question is - is there any easy way around this?
I experimented with various answers to similar questions, but I was completely unable to implement it.
Getting the last record in each group
or
How to get the last record in each group using GROUP BY?
Additional help would be greatly appreciated.
EDIT: The closest I currently have is:
SELECT i1.*, c.name AS image, m.title AS title FROM issues i1 INNER JOIN covers c ON i1.id = c.issue_id INNER JOIN magazines m ON i1.magazine_id = m.id JOIN (SELECT magazine_id, MAX(year) year, MAX(yearly_issue) yearly_issue FROM issues GROUP BY magazine_id) i2 ON i1.magazine_id = i2.magazine_id AND i1.year = i2.year
However, he does not give me the desired result at all.
eloquent laravel
Andreas
source share