Sorting data with Eloquent

后端 未结 5 609
傲寒
傲寒 2021-01-11 12:49

I\'m new to laravel (switched from CI) and eloquent ORM is still a bit of a mystery at some point!

Here is my problem :

I\'d like to sort data from my db usi

5条回答
  •  有刺的猬
    2021-01-11 13:13

    I had the same problem before but it was a one-to-one relationship. This post helped me. I ended up using a join. My code was like this:

    $data = Posts::join('comments', 'comments.post_id', '=', 'posts.id')
            ->order_by('comments.created_at')
            ->get(array('comments.field1 as field1', 'posts.field2 as field2'));
    

提交回复
热议问题