B
B
BonBon Slick2017-06-06 18:55:20
ORM
BonBon Slick, 2017-06-06 18:55:20

Laravel fetching models with sorting by relation models?

I want to sort current post models by author's name.

$posts= Post::with(['author' => function ($query) {
                $query->orderBy('name', 'desc');
            }])->get();

Returns 1, 5, 11. Standard ASC selection by creation date.
QueryLog
0 => array:3 [▼
    "query" => "select * from "posts" where "posts"."deleted_at" is null limit 3"
    "bindings" => []
    "time" => 1.13
  ]
  1 => array:3 [▼
    "query" => "select * from "authors" where "authors"."id" in (?, ?, ?) order by "name" desc"
 "bindings" => array:3 [▼
      0 => 1
      1 => 5
      2 => 11]
    "time" => 3.99
  ]
]

The first should be a post with id 11, then 1 and the last 5.
How to sort by author names in this case, please tell me?
ps without joins and division into small subqueries, in one request via ORM.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2017-06-07
@BonBonSlick

Without JOIN in any way .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question