0

im having trouble mass updating data with tinker and Carbon as it does not iterate through the values

input

>>> $d = Child::get('Birthday') //1st command

[!] Aliasing 'Child' to 'App\Child' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#4195
     all: [
       App\Child {#4196
         Birthday: "2015-03-26",
       },
       App\Child {#4197
         Birthday: "2014-08-16",
       },
       App\Child {#4198
         Birthday: "2018-02-05",
       },
       App\Child {#4199
         Birthday: "1976-08-26",
       },
       App\Child {#4200
         Birthday: "1978-02-20",
       },
       App\Child {#4201
         Birthday: "2011-06-09",
       },
     ],
   }

>>> foreach($d as $ad) Child::query()->update(['Birthday' => Carbon\Carbon::parse($ad->Birthday)->format('Y:m:d')]) //2nd command

heres the result

Illuminate\Database\Eloquent\Collection {#4204
     all: [
       App\Child {#4205
         Birthday: "2011:06:09",
       },
       App\Child {#4206
         Birthday: "2011:06:09",
       },
       App\Child {#4207
         Birthday: "2011:06:09",
       },
       App\Child {#4208
         Birthday: "2011:06:09",
       },
       App\Child {#4209
         Birthday: "2011:06:09",
       },
       App\Child {#4210
         Birthday: "2011:06:09",
       },
     ],
   }

I just want to mass update the date format but as u can see it updates everything wrong

Son
  • 3
  • 3
  • Looks like it updated all records based on the last Birthday in the loop. You should use `$ad->update()` instead of `Child::query()->update()` - because currently you are updating all records with every iteration. – acvi Oct 25 '20 at 16:06
  • @acvi something like this? `foreach($d as $ad) $ad->update(['Birthday' => Carbon\Carbon::parse( $ad->Birthday)->format('Y:m:d')])` unfotunately it does nothing – Son Oct 25 '20 at 16:18
  • check if `Birthday` is in `Child`'s `$fillable` attribute, it should work then. – acvi Oct 26 '20 at 07:12

1 Answers1

1

Child::query()->update(['Birthday' => Carbon\Carbon::parse($ad->Birthday)->format('Y:m:d')]);

this line in your code update every row in the table, so in the last round of for each loop this code update every row to the last birthdate.

since it is a string column you can reformat it like this way:

 $d = Child::get(['id','Birthday']);
foreach($d as $ad)
 Child::query()->where('id',$ad->id)->update(['Birthday' => Carbon\Carbon::parse($ad->Birthday)->format('Y:m:d')]);

there is a lot of format you may like in here

but I must say that this column should be a date column, not a string

OMR
  • 11,736
  • 5
  • 20
  • 35
  • yes, that much is clear from the result but I want a help or a solution to this – Son Oct 25 '20 at 16:19
  • ok, what is you want to do, do you want to change the format of Birthday column in db? – OMR Oct 25 '20 at 16:22
  • yes, every data in the Birthday column and thanks in advance – Son Oct 25 '20 at 16:25
  • is it string column? – OMR Oct 25 '20 at 16:25
  • `PHP Parse error: Syntax error, unexpected ')', expecting ']' on line 1` – Son Oct 25 '20 at 16:40
  • `Illuminate/Database/QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 (SQL: update `children` set `Birthday` = DATE_FORMAT("Birthday", "%Y-%m-%d", `children`.`updated_at` = 2020-10-25 16:50:35)'` – Son Oct 25 '20 at 16:51
  • are you using mysql dataBase? – OMR Oct 25 '20 at 16:53
  • anyway, I have updated the answer to have the simplest way, but not the faster – OMR Oct 25 '20 at 16:57
  • `Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.` sorry for this – Son Oct 25 '20 at 17:06