Laravel Queries SQL Doubt?

Asked

Viewed 62 times

2

I’m starting with the SQL of Laravel I searched much more I did not get an answer on how to make a AND after a where, follows example code to implement a AND.

SQL:

UPDATE contas
SET valor_titulo = 0,
WHERE id_contrato = 2 
AND data_vencimento = '2017-05-05'

Like I’m doing in the Laravel:

DB::table('contas')
        ->where('id_contrato', $options['id_contrato'])
        ->update(['valor_titulo' => $attributes['valor_titulo']);

How to put a AND in this structure?

1 answer

2


You can do it like this:

DB::table('contas')
    ->where('id_contrato', $options['id_contrato'])
    ->where('data_vencimento', $options['data_vencimento'])
    ->update(['valor_titulo' => $attributes['valor_titulo']);

or so:

DB::table('contas')
    ->where([
        ['id_contrato','=', $options['id_contrato']],
        ['data_vencimento','=', $options['data_vencimento']],
     ])
    ->update(['valor_titulo' => $attributes['valor_titulo']);

or even so:

DB::table('contas')
    ->where(function($query) use ($options){
            $query->where('id_contrato',$options['id_contrato'])
                  ->where('data_vencimento',$options['data_vencimento']);
      })
    ->update(['valor_titulo' => $attributes['valor_titulo']);

References:

  • 1

    Virgilio Novic tried with the second option and worked perfectly, thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.