Update fields of several tables in Laravel

-1

I have a question, how can I make this query in laravel using Eloquent ?

UPDATE Tabla1, Tabla2, Tabla
SET
Tabla1.estado = 1,
Tabla2.estado = 1,
Tabla3.estado = 1, 
WHERE 
Tabla1.estado = 0 
or 
Tabla2.estado = 0 
or 
Tabla3.estado = 0

I'm not sure about the query, but what I'm trying to do is update 3 tables with the estado = 0 by estado = 1 .

    
asked by Jhosselin Giménez 27.04.2017 в 14:42
source

1 answer

1

Good morning. I honestly do not think you can make this query using Eloquent. What I'm sure of is that if you divide it you can use an expression like this:

// Tabla1, Tabla2 y Tabla3 son los modelos
Tabla1::where('estado', 0)->update(['estado' => 1]);
Tabla2::where('estado', 0)->update(['estado' => 1]);
Tabla3::where('estado', 0)->update(['estado' => 1]);

This is in the documentation of Mass Update

Now there are more options, which by the way are not as elegant:

Using Query Builder

DB::connection('mi_conexion')
    ->table(DB::raw('Tabla1, Tabla2, Tabla3'))
    ->where('Tabla1.estado', 0)
    ->orWhere('Tabla2.estado', 0)
    ->orWhere('Tabla3.estado', 0)
    ->update(['Tabla1.estado' => 1, 
              'Tabla2.estado' => 1, 
              'Tabla3.estado' => 1 ]);

Using Raw Queries

DB::connection('mi_conexion')->statement('UPDATE Tabla1, Tabla2, Tabla3 SET Tabla1.estado = 1, Tabla2.estado = 1, Tabla3.estado = 1 WHERE Tabla1.estado = 0 or Tabla2.estado = 0 or Tabla3.estado = 0;');

By the way, it seems very strange to me what you are trying to do, but good ...

    
answered by 27.04.2017 / 15:51
source