How to use orderby on element that was joined with Laravel Eloquent method WITH

后端 未结 1 1633
南方客
南方客 2021-01-28 15:11

The problem is that the query can\'t find the specific_method(specific_method, specific_model,SpecificModel,specificMethod etc...), that should been joined with the method WITH

相关标签:
1条回答
  • 2021-01-28 16:06

    This happens because the belongsTo relationship does not execute a join query as you expect it to (as you can see from the error you get). It executes another query to get the related model(s). As such you will not be able to order the original model by related models columns.

    Basically, 2 queries happen:

    1. Fetch the original model with SELECT * from originalModel ...*

    2. Fetch the related models with SELECT * from relatedModel where in id (originalModelForeignKeys)

    Then Laravel does some magic and attaches the models from the 2nd query to the correct models from the first query.

    You will need to perform an actual join to be able to order the way you want it to.

    0 讨论(0)
提交回复
热议问题