Hernawan
Hernawan

Reputation: 23

Laravel Mass Updates without touching timestamps

if single update we can use

$variable->timestamps = false;

but how if i want to mass updates without touching timestamps.

currently my code is

$allapps = App::select('id','name','parent_id','view')->where('published',true)->whereNotIn('id', $excludeappsid)->update(['timestamps' => false],['view' => 0]);

but i get error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'timestamps' in 'field list'

any solution?

thanks.

Upvotes: 1

Views: 3104

Answers (4)

Waska Chaduneli
Waska Chaduneli

Reputation: 362

You can achieve your goal with the query below:

App::select('id','name','parent_id','view')
    ->where('published',true)
    ->whereNotIn('id', $excludeappsid)
    ->update([
        'view' => 0,

        // Change updated_at column if its different from yours
        'updated_at' => \DB::raw('updated_at'),
    ]);

Actually it updates updated_at column, but the query will change it with the old value.

Upvotes: 1

yuan wang
yuan wang

Reputation: 41

Try this:

$model = new App();
$model->timestamps = false;
App::setModel($model)
    ->where('published',true)
    ->whereNotIn('id', $excludeappsid)
    ->update(['view' => 0]);

Upvotes: 1

Goms
Goms

Reputation: 2644

Since your model, you can juste mark timestamps to false

public $timestamps = false;

Or you can do nothing. :)

    $allapps = App::select('id','name','parent_id','view')
                   ->where('published',true)
                   ->whereNotIn('id', $excludeappsid)
                   ->update(['view' => 0])

Upvotes: 0

Jerodev
Jerodev

Reputation: 33186

As far as I know this is not possible. I don't think it should even be possible.

You can however use the query builder directly without Eloquent and this will not update the timestamps.

\DB::table(with(App::class)->getTable())
    ->where('published', true)
    ->whereNotIn('id', $excludeappsid)
    ->update(['view' => 0]);

Upvotes: 1

Related Questions