0

I have some relations in my DB and heres the relatin i'm working with:

$category = Category::with('article')
    ->where('slug', '=', $slug)
    ->latest()
    ->paginate(12); 

the articles fetch and everything are ok, but latest method and paginate won't work! it means the collection is ascending & not paginated.

any ideas? thanks.

Ar.Bazargani
  • 13
  • 1
  • 6
  • By default `latest` uses `created_at` column name, if you want to use different column, pass it as 2nd parameter. – Harven Jan 22 '20 at 09:40
  • 1
    Define "won't work". is the sorting incorrect? Are you getting the wrong page? ... – Jerodev Jan 22 '20 at 09:44
  • @Jerodev means the collection isn't paginated and all rows will fetch in ascending mode, so latest and paginate won't return what i want. – Ar.Bazargani Jan 22 '20 at 09:57
  • Do you want to order by articale create date or by category create date? Becounse in your case `latest()` and `paginate()` relates to `Category model`. – Harven Jan 22 '20 at 10:01
  • @Jerodev om, that's the point i think. collection should order by the article model. – Ar.Bazargani Jan 22 '20 at 10:07
  • @Ar.Bazargani check this answer (very similar case): https://stackoverflow.com/a/18882219/5252403 – Harven Jan 22 '20 at 10:10

1 Answers1

0

Your code is working fine for me as i have change acording to my models.

$category = Doctor::with('detail')
            ->where('name', 'like', '%a%')
            ->latest()
            ->paginate(12);
            dd($category);

It's output is hereenter image description here

Waqas Altaf
  • 392
  • 3
  • 16