0

I have the following code:

$invoices = \App\Finance_Invoices::query()
    ->with("file.property")
    ->where('period', "02")
    ->where('year', "2022")
    ->where('paid_to_landlord', 0)
    ->whereColumn('total_paid', '>=', 'amount')
    ->whereHas('file.property.landlord', function ($query) use ($id) {
        $query->where('id', $id);
    })
    ->orderBy("file.property.full_address")
    ->get();

The idea is getting the outstanding invoices, but sorted by address. The address is only available through the relation 'file->property'.

With this code I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'file.property.full_address' in 'order clause'
miken32
  • 42,008
  • 16
  • 111
  • 154
Erik180486
  • 1
  • 1
  • 5
  • Best bet is to sort the collection afterwards using [`sortBy()`](https://laravel.com/docs/8.x/collections#method-sortby) method. – miken32 Feb 02 '22 at 18:58

1 Answers1

0

With the syntax seen in the error message, you can't retrieve or sort a column from the file table. You can only retrieve or sort columns from the finance_invoices table. I suggest using a JOIN instead of the nested subqueries.

  • Without any code to back it up, this more of a comment than an answer. A lot of the attraction of Eloquent is avoiding database nonsense like joins. – miken32 Feb 02 '22 at 19:01