我无法使用 tinker 和 Carbon 大规模更新数据,因为它不会遍历值
输入
>>> $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
这是结果
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",
},
],
}
我只是想批量更新日期格式,但你可以看到它更新了所有错误