LARAVEL - Order by on relationship propriety -


i try sort topics date of creation of last post in each subject. have idea how this? recover want, not in right order.

one topic has many poste , 1 poste belongsto 1 topic.

*: topic = sujet in code. topics = lessujets.

$lessujets = sujet::where('jeu_id', $idjeu)->paginate(20);   @foreach ($lessujets $sujet)         <tr>              <td><a href="{{route('sujet.show', $sujet->id)}}">{{$sujet->titre}}</a></td>                 ...         </tr> 

screenshot: forum

i topics sorted last reply. order should be: "your best card", "test 1", "aaaaa ..!", "[firm] zzzz .."; can know last answer of topic $sujet->postes->last()->created_at (in view of screenshot).

you can use eager loading use orderby() relationship. i'm not entirely sure of database schema, being french speaking myself, i'll try , give example match , make sense gave ;-)

$sujets = sujet::where('jeu_id', $idjeu)->with('poste')->orderby('post.date', 'desc')->get(); 

this should "sujets", , eager load it's "poste", enabeling order of "poste"'s property.

edit: wrong, here correct syntax:

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

edit 2: think misunderstood looking @ first, how this? make sure check relationship names in code it's yours, on end, tested on dummy code , works.

$sujets = sujet::where('jeu_id', $idjeu) ->with(['poste'])->get() ->sortbydesc('poste.created_at'); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -