If you've been told to update a list of records with the new values in the DB, and it's hard to do it manually, you can run a script within Tinker.
The structure might look something like this:
- Go to
File
- Click
Share
and then selectPublish to Web
- In the
Link
tab, select the desired Sheet, and also selectComma-separated values (.csv)
- Copy the following link below
If you need to update records via QueryBuilder then you probably need this one:
(function(): void {
$url = 'https://docs.google.com/spreadsheets/d/e/SomeHash/pub?gid=XXX&single=true&output=csv';
$fp = fopen($url, 'r');
if ($fp === false) {
dump("Error happened on opening CSV file");
return;
}
$header = true;
while (($row = fgetcsv($fp)) !== false) {
if ($header) {
$header = false;
continue;
}
// here you split a row into variables
// you can also skip unnecessary columns by leaving them empty as following:
[$id,, $field1,, $field2] = $row;
SomeModel::where('id', $id)->update([
'field_1' => $field1,
'field_2' => $field2,
]);
dump(sprintf("%s [%d] updated", class_basename(SomeModel::class), $id));
}
fclose($fp);
})();
However, if you need update records via Models with its events then:
- you have to find the record first
- update the found model later
As you've mentioned, there're 2 queries per row.
Here's a slight adjustment to the previous script:
$someModel = SomeModel::find($id);
if (is_null($someModel)) {
dump(sprintf("There is no [%s] with ID [%d]", class_basename(SomeModel::class), $id));
continue;
}
$someModel->update([
'field_1' => $field1,
'field_2' => $field2,
]);
It is highly recommended to test such scripts locally, in order to be confident everything works as expected.
In order to run the script on the production server, you need to get there and in the terminal enter a Tinker:
php artisan tinker
Then copy and paste your script in the terminal, and hit Enter.
Hooray 🎉 You'll see the progress when records are being updated!