Home > OS >  Eloquent oderby using ( with )
Eloquent oderby using ( with )

Time:07-25

client Model

 protected $table = 'client';
    protected $fillable = [
        'client_name',
        'supplier_id'
    ];

 public function supplier()
    {
        return $this->belongsTo(Supplier::class, 'supplier_id', 'id');
    }

Supplier table

id    | supplier
1     | john 
2     | ace
3     | bravo

ClientController

$Client = new Client();
        $Client = $Client ->with(
            'supplier'
        );
 $Client = $Client->orderBy('supplier', 'DESC');
       

Error

Column not found: 1054 Unknown column 'supplier' in 'order clause' (SQL: select * from `client` order by `supplier` desc limit 20 offset 0)

i need to order by supplier from with relationship

CodePudding user response:

Depending on your laravel version you can try:

$Client = new Client();
return $Client ->with([
    'supplier' => function ($q){
    $q->orderBy('supplier');
    }
]);

CodePudding user response:

If you want to order the supplier inside the Client, see this.

If you want to order the Client based on its supplier, see this.

  • Related