André Rodrigues
André Rodrigues

Reputation: 69

Laravel 5 - Change all values in a Column of a table

I have a table with column called "estado" and I want change all the column in one shot.

This is the right method? The faster one?

$vendedor = Vendedor::with('produtos')->find( $idVendedor );

    foreach ( $vendedor->produtos as $produto ) {
        $produto->pivot->estado = $novoEstado;
    };

The column what I want change is "estado". There's a way to do it without the foreach?

Upvotes: 2

Views: 2226

Answers (2)

Maizied Hasan Majumder
Maizied Hasan Majumder

Reputation: 1053

Update Specific column you want to upadate using Laravel eloquent.

Way -> 1

[Status-> Column you want to update]

Model::where('status', '=', 1)->update(['status' => 0]);

Way -> 2

[Status-> Column you want to update]

$allData = Model::where('status', 1)->get();
                foreach ($allData as $data){
                    $data->status = 0;
                    $data->update();
                }

Upvotes: 1

lukasgeiter
lukasgeiter

Reputation: 152900

The simplest would be to just use the query builder for this (instead of your model)

DB::table('table_name')->where('vendedor_id', $idVendedor)->update('estado', $novoEstado);

Upvotes: 0

Related Questions