public static function boot() { parent::boot(); static::creating(function ($profile) { $string = preg_replace('/\\s+/', '', $profile->display_name); $filename = time() . '-' . $string . '.png'; $path = public_path('img/avatar/' . $filename); \Avatar::create($profile->display_name)->save($path, 100); $profile->profile_pic = asset('img/avatar/' . $filename); }); }
/** * Run the database seeds. * * @return void */ public function run() { DB::table('users')->insert([['name' => 'Tony', 'email' => '*****@*****.**', 'password' => Hash::make('Tony'), 'role' => 'administrator'], ['name' => 'Antoine', 'email' => '*****@*****.**', 'password' => Hash::make('Antoine'), 'role' => 'visitor'], ['name' => 'Romain', 'email' => '*****@*****.**', 'password' => Hash::make('Tony'), 'role' => 'visitor'], ['name' => 'yini', 'email' => '*****@*****.**', 'password' => Hash::make('Yini'), 'role' => 'visitor']]); factory(App\Customer::class, 4)->create(); // avatars DB::table('avatars')->delete(); DB::statement("ALTER TABLE avatars AUTO_INCREMENT=1"); $files = Storage::allFiles(env('UPLOADS_AVATARS', 'uploads')); foreach ($files as $file) { Storage::delete($file); } $users = User::all(); foreach ($users as $user) { $uri = str_random(12) . '_216x256.jpg'; $fileName = file_get_contents('http://lorempixel.com/216/256/animals'); $pathDirectory = env('UPLOADS_AVATARS', 'avatars') . DIRECTORY_SEPARATOR . $uri; Storage::put($pathDirectory, $fileName); $mime = mime_content_type(storage_path('app') . DIRECTORY_SEPARATOR . $pathDirectory); Avatar::create(['user_id' => $user->id, 'uri' => $uri, 'title' => $this->facker->name, 'mime' => $mime]); } }