mardi 7 mai 2019

Laravel - Order eloquent query by a belongsTo sub relation (relation of a relation of the model)

I have this query:

$challenges = $platform->challenges()->with(['reward', 'brand'])->skip($input['start'])->take($input['length'])->get();

I am trying to get all the platform challenges ordered by brand name. Those are the relations:

  • Platform hasMany Challenge
  • Challenge belongsTo Brand

I am trying with this code:

$challenges = $platform->challenges()->with(['reward'])->skip($input['start'])->take($input['length']);
// ... some more code here
$challenges = $challenges->with(
    [
        "brand" => function ($query) use ($orderDirection) {
            return $query->orderBy("name", $orderDirection);
        }
    ]
)->get();

Anything works. If I try to do orderBy('brand.name')it throws an Exception.

How can I get platform challenges ordered by brand name?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire