0

I'm using Laravel 9. To clarify my question I will explain the models also the relations between them.

The first model is LicenseSubmission which has a one to many relationship with LicenseForm model:

class LicenseSubmission extends Model
{

    public function licenseForm(): BelongsTo
    {
        return $this->belongsTo(LicenseForm::class);
    }
}

Also, LicenseForm model has a one to many relationship with Department model:

class LicenseForm extends Model
{

    public function department(): BelongsTo
    {
        return $this->belongsTo(Department::class);
    }
  
}

Now I want to get license_submissions order by its related department's title.

How to do that?

If you can answer this question, there is no difference between Eloquent code and Raw SQL.

Rayan
  • 115
  • 8
  • Does this answer your question? [Order by relationship column](https://stackoverflow.com/questions/38261546/order-by-relationship-column) – miken32 Apr 05 '23 at 16:09
  • Seems like a duplicate, but might be helpful to show the relationships between these three models as well as how the relationships are being accessed. Lazy vs eager loading, etc. Show your actual query code. – miken32 Apr 05 '23 at 16:11

1 Answers1

0

try this:

LicenseSubmission::with('licenseForm.department')
    ->orderBy('licenseForm.department.title')
    ->get();
Mahdi Rashidi
  • 411
  • 3
  • 8