dadub
3 months ago
532
10
Laravel

Add orderBy to a ::with

Posted 3 months ago by dadub

Hello and thank you in advance for your help.

I have this code :

$herb = Herb::with('hinteractions.herbs', 'hinteractions.effects', 'hinteractions.targets')->findOrFail($id);

I would like to add orderBy force_id

App\Herb {#1783
  #relations: array:1 [▼
    "hinteractions" => Illuminate\Database\Eloquent\Collection {#1822 ▼
      #items: array:35 [▼
        0 => App\Hinteraction {#1851 ▼
          #table: "hinteractions"
          #attributes: array:9 [▼
            "id" => 157
            "herb_id" => 8
            "target_id" => 2
            "user_id" => 2
            "force_id" => 4
            "validated" => 1
            "created_at" => "2020-07-25 09:24:08"
            "updated_at" => "2020-07-25 09:24:08"
          ]

I tried that :

$herb = Herb::with('hinteractions.herbs', 'hinteractions.effects', 'hinteractions.targets')->orderBy('force_id')->findOrFail($id);

I have this error :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'force_id' in 'order clause' (SQL: select * from herbs where herbs.id = 8 order by force_id asc limit 1)

Thank you in advance for your help.

Please sign in or create an account to participate in this conversation.