Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Laravel multiple WHERE clauses

I have a requirement to add multiple where clauses to a Laravel SQL query.

So far my PHP code has been:

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();

But above code returns:

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

I want to generate below SQl statement:

SELET * 
FROM task
WHERE rowstate <> 'Ready'
AND DATE_FORMAT(due_date, "%d-%m-%y") < $today
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You have 2 possible solutions.

Instead of:

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

you can use

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

or

you can use array syntax like this:

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

So to sum up you were missing enclosing your data into outer array, and you need to use DB::raw in case you don't use raw column names

about 4 years ago · Santiago Trujillo Relatório

0

You can't use sql function's directly in laravel where class. You need to use DB::raw() for same.

So your query will be like :

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

0

Try this

->where([
//      ^
    ['rowstate', '<>', 'Ready'],
    ['DATE_FORMAT(due_date, "%d-%m-%y")', '<', $today_]])
//                                                     ^
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda