Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Laravel múltiples cláusulas WHERE

Tengo el requisito de agregar varias cláusulas where a una consulta SQL de Laravel.

Hasta ahora mi código PHP ha sido:

 date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.'-'.$month.'-'.$year; $result = DB::table('task') ->select('*') ->where( ['rowstate', '<>', 'Ready'], ['DATE_FORMAT(due_date, "%d-%m-%y")', '<', $today_]) ->get();

Pero el código anterior devuelve:

 Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `task_tab` where (`0` = rowstate and `1` = <> and `2` = Ready))

Quiero generar debajo de la declaración SQl:

 SELET * FROM task WHERE rowstate <> 'Ready' AND DATE_FORMAT(due_date, "%d-%m-%y") < $today
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Tienes 2 posibles soluciones.

En vez de:

 ->where(['rowstate', '<>', 'Ready'], ['DATE_FORMAT(due_date, "%d-%m-%y")', '<', $today_])

puedes usar

 ->where('rowstate','<>','Ready')->where(DB::raw('DATE_FORMAT(due_date, "%d-%m-%y"','<', $today_);

o

puede usar una sintaxis de matriz como esta:

 ->where([ ['rowstate', '<>', 'Ready'], [DB::raw('DATE_FORMAT(due_date, "%d-%m-%y")'), '<', $today_] ]);

Entonces, para resumir, le faltaba encerrar sus datos en una matriz externa, y necesita usar DB::raw en caso de que no use nombres de columna sin procesar

over 4 years ago · Santiago Trujillo Report

0

No puede usar la función sql directamente en laravel donde class. Necesita usar DB::raw() para lo mismo.

Entonces su consulta será como:

 $result = DB::table('task') ->select('*') ->where('rowstate', '<>', 'Ready') ->where(DB::raw('DATE_FORMAT(due_date, "%d-%m-%y")'), '<', $today_) ->get();
over 4 years ago · Santiago Trujillo Report

0

Prueba esto

 ->where([ // ^ ['rowstate', '<>', 'Ready'], ['DATE_FORMAT(due_date, "%d-%m-%y")', '<', $today_]]) // ^
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!