0

I try to sort topics from the date of creation of the last post in each subject. Does anyone have any idea how to do this? Actually I recover everything well what I want, but not in the right order.

One topic has many poste and one poste belongsto one topic.

*: topic = sujet in the code. topics = lesSujets.

$lesSujets = Sujet::where('jeu_id', $idJeu)->paginate(20); 

@foreach ($lesSujets as $sujet)
        <tr>

            <td><a href="{{route('sujet.show', $sujet->id)}}">{{$sujet->titre}}</a></td>
                ...
        </tr>

Screenshot: forum

I would like the topics to be sorted by last reply. The order should be: "your best card", "test 1", "aaaaa ..!", "[Firm] zzzz .."; We can know the last answer of a topic by $sujet->postes->last()->created_at (in the view of screenshot).

emeliku
  • 173
  • 1
  • 13

1 Answers1

0

You can use eager loading to use orderBy() with a relationship. I'm not entirely sure of you database schema, but being french speaking myself, I'll just try and give you an example that match and make sense with what you gave us ;-)

$sujets = Sujet::where('jeu_id', $idJeu)->with('poste')->orderBy('post.date', 'desc')->get();

This should get all your "sujets", and will eager load it's "poste", enabeling you order by any of the "poste"'s property.

EDIT: I was wrong, here is the correct syntax:

$sujets = Sujet::where('jeu_id', $idJeu)->with(['poste' => function ($query) {
    $query->orderBy('created_at', 'desc');
}])->get();

EDIT 2: I think I misunderstood what you where looking for at first, how about this? Just make sure you check the relationship names in my code so it's really like yours, but on my end, I tested it on some dummy code and it works.

$sujets = Sujet::where('jeu_id', $idJeu)
->with(['poste'])->get()
->sortByDesc('poste.created_at');
Jean-Philippe Murray
  • 1,208
  • 2
  • 12
  • 34
  • Thx, but I've already try this and it's not work $lesSujets = Sujet::where('jeu_id', $idJeu)->with('postes')->orderBy('poste.created_at', 'desc')->paginate(20); // Error: SQLSTATE[42S22]: Column not found: 1054 Champ 'poste.created_at' inconnu dans order clause (SQL: select * from `sujets` where `jeu_id` = 1 order by `poste`.`created_at` desc limit 20 offset 0) – emeliku Apr 07 '17 at 13:10
  • @emeliku I corrected my answer, turns out my syntax was wrong, see the edit. – Jean-Philippe Murray Apr 07 '17 at 13:38
  • Nothing are changed. I'go home – emeliku Apr 07 '17 at 13:56
  • i've edit 1st post for screenshot and more explication – emeliku Apr 08 '17 at 17:12
  • it's sorted by first post of topics and he would have to take the last post for sorted. And the method paginate() not work with this – emeliku Apr 08 '17 at 19:05
  • To change the order or sorting, you just have to change `sortByDesc` with `sortBy`, this reverse the order. As for pagination, I didn't take it into account because the question was on ordering more than anything to me. It won't work because paginate it's a query builder method, but a quick search has shown me that there is a [way to paginate collections](https://gist.github.com/vluzrmos/3ce756322702331fdf2bf414fea27bcb), you could use that. – Jean-Philippe Murray Apr 08 '17 at 20:00