Pass a raw query to order your results

You can pass a raw query to order your results.

For example, sorting tasks by how long before the due date they were completed.

// Sort tasks by the task was completed before the due date
$tasks = Task::query()
->whereNotNull('completed_at')
->orderByRaw('due_at - completed_at DESC')
->get();

Tip given by @cosmeescobedo

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 68 courses (1183 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses