57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders\New;
|
|
|
|
use App\Models\User;
|
|
use Exception;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use JsonMachine\Items;
|
|
use JsonMachine\JsonDecoder\ExtJsonDecoder;
|
|
|
|
class AddressSeeder extends Seeder
|
|
{
|
|
public function run()
|
|
{
|
|
DB::transaction(function () {
|
|
$table = 'user_addresses';
|
|
|
|
$items = Items::fromFile(
|
|
database_path('data/addresses.json'),
|
|
['decoder' => new ExtJsonDecoder(true)]
|
|
);
|
|
|
|
// First we need to fill user_addresses table with json data, then find user id matching addressable_id and fill user options with {"address": "$data"}
|
|
|
|
foreach ($items as $data) {
|
|
try {
|
|
DB::table($table)->insert([
|
|
'id' => $data['id'],
|
|
'user_id' => $data['addressable_id'],
|
|
'street_address' => $data['address'],
|
|
'title' => $data['title'],
|
|
'building' => $data['building'],
|
|
'floor' => $data['floor'],
|
|
'door' => $data['door'],
|
|
'location' => $data['location'],
|
|
'type' => $data['type'],
|
|
'created_at' => $data['created_at'],
|
|
'updated_at' => $data['updated_at'],
|
|
'is_default' => false,
|
|
]);
|
|
} catch (Exception $e) {
|
|
continue;
|
|
}
|
|
|
|
$user = User::where('id', $data['addressable_id'])->first();
|
|
if ($user) {
|
|
info($user);
|
|
$user->options->set('address', $data['address']);
|
|
$user->save();
|
|
}
|
|
}
|
|
|
|
});
|
|
}
|
|
}
|