mastercut/database/seeders/UserSeeder.php

71 lines
2.2 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Attachment;
use App\Models\Biography;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::transaction(function () {
// Create a new user
$newUser = User::create([
'role_id' => 1,
'barbershop_id' => 1,
'username' => 'yoga',
'email' => 'yoga@developer.com',
'password' => Hash::make(env('PASSWORD_DEFAULT_USER')),
]);
// Create biography for the user
Biography::create([
'user_id' => $newUser->id,
'full_name' => 'Yoga Pangestu',
'pob' => 'Subang',
'dob' => '2000-01-01',
'contact_number' => '082121495806',
'address' => 'Jl.Babaru Nagri',
'gender' => 1,
]);
// Path to the image file
$imagePath = public_path('assets/admin/images/avatar/speed.jpeg');
// Check if the file exists
if (file_exists($imagePath)) {
// Create a Symfony file instance
$file = new \Symfony\Component\HttpFoundation\File\File($imagePath);
// Generate a unique file name
$fileName = Str::uuid().'.'.$file->getExtension();
// Create an attachment record
Attachment::create([
'attachmentable_id' => $newUser->id,
'attachmentable_type' => User::class,
'name' => $fileName,
'extension' => $file->getExtension(),
'size' => $file->getSize(),
]);
// Define the storage path for avatars
$path = 'avatars/'.now()->format('Y-m-d');
// Store the file in the defined path using Storage facade
Storage::disk('public')->putFileAs($path, $file, $fileName);
}
});
}
}