Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pavlen's avatar

Update all column fileds

Hi,

I am trying to update all fields in same time in table column. First, i check are there diferent values(0 or 1) if are all 1, then update all values to 0. I try with this part of code,but get error:

[Symfony\Component\Debug\Exception\FatalErrorException]                   
  Call to undefined method Illuminate\Database\Eloquent\Collection::save()

Here is my code:

  $users = DB::table('mails')->distinct('mark')->count('mark');
             
             if ($users == 1){    
                 $mark = Mails::all();
                 $mark->mark = '0';
                 $mark->save();
                 
                 echo 'reset values to 0';
             }else{
                 
                 echo 'DIferent values';
             }

Tnx,

0 likes
6 replies
phildawson's avatar
Level 26

Updated

DB::table('mails')->update(['mark' => 0]);

You can't call save() on a Collection.

Edit: Just seen you figured it out :)

pavlen's avatar

Hi,

don't work,

but this work :

DB::table('mails')->update(['mark' => 0]);

Thanks!

phildawson's avatar

No probs @pavlen :) btw if you wanted to do it from the model like in the OP.

(new Mails)->update(['mark' => 0]);
waadm97's avatar
DB::table('mails')->query()->update( [ 'mark' => 0] );
Sinnbeck's avatar

@waadm97 Uhm 7 years later and with a solution that does the exact same as the best answer?

Please or to participate in this conversation.