Skip to content
Advertisement

how to convert this SQL query to eloquent in Laravel

I am trying to convert this SQL query to Eloquent in Laravel

Convert SQL code to Eloquent

JavaScript

my code in Laravel but not working

JavaScript

Advertisement

Answer

There is documentation available for all the operations in your query.

  • For selected columns use select('column1', 'column2', ...)
  • For selected aggregate columns use selectRaw('sum(column) as column')
  • For WHERE column IN (...) use whereIn('column', $array)
  • For subquery tables, use Closures or Builder classes (DB::table(fn($q) => ... , alias) or DB::table($builder, alias))
  • For UNION ALL use unionAll() with the same syntax as subquery tables.

Option 1: Closures

JavaScript

Option 2: Builder (or translating the subqueries from the inside-out)

JavaScript

Pick whichever you prefer. Both evaluate to the same query you posted.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement