'array','images' => 'array']; protected $table = 'venues'; // protected $primaryKey = 'id'; public $timestamps = false; // protected $guarded = ['id']; protected $fillable = [ 'venue_name', 'venue_name_ru', 'venue_name_tk', 'venue_name_full', 'description_ru', 'description_tk', 'location_address', 'location_address_line_1', 'location_address_line_2', 'location_country', 'location_country_code', 'location_state', 'location_post_code', 'location_street_number', 'location_lat', 'location_long', 'location_google_place_id', 'seats_image', 'active', 'address', 'images', ]; // protected $hidden = []; // protected $dates = []; /* |-------------------------------------------------------------------------- | FUNCTIONS |-------------------------------------------------------------------------- */ public static function boot() { parent::boot(); static::deleting(function($obj) { $disk = config('filesystems.default'); \Storage::disk($disk)->delete($obj->seats_image); if (count((array)$obj->images)) { foreach ($obj->images as $file_path) { \Storage::disk('uploads')->delete($file_path); } } }); } /* |-------------------------------------------------------------------------- | RELATIONS |-------------------------------------------------------------------------- */ public function events(){ return $this->hasMany(Event::class); } public function sections(){ return $this->hasMany(Section::class); } /* |-------------------------------------------------------------------------- | SCOPES |-------------------------------------------------------------------------- */ /* |-------------------------------------------------------------------------- | ACCESORS |-------------------------------------------------------------------------- */ public function getVenueNameAttribute(){ return $this->{'venue_name_'.Config::get('app.locale')}; } /* |-------------------------------------------------------------------------- | MUTATORS |-------------------------------------------------------------------------- */ public function setSeatsImageAttribute($value){ $attribute_name = "seats_image"; $disk = config('filesystems.default'); // or use your own disk, defined in config/filesystems.php $destination_path = "venues"; // path relative to the disk above // if the image was erased if ($value==null) { // delete the image from disk \Storage::disk($disk)->delete($this->{$attribute_name}); // set null in the database column $this->attributes[$attribute_name] = null; } // if a base64 was sent, store it in the db if (starts_with($value, 'data:image')) { // 0. Make the image $image = \Image::make($value)->encode('jpg', 90); // 1. Generate a filename. $filename = md5($value.time()).'.jpg'; // 2. Store the image on disk. \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream()); // 3. Save the public path to the database // but first, remove "public/" from the path, since we're pointing to it from the root folder // that way, what gets saved in the database is the user-accesible URL $public_destination_path = Str::replaceFirst('public/', '', $destination_path); $this->attributes[$attribute_name] = $public_destination_path.'/'.$filename; } } public function uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path) { $request = \Request::instance(); $attribute_value = (array) $this->{$attribute_name}; $files_to_clear = $request->get('clear_'.$attribute_name); // if a file has been marked for removal, // delete it from the disk and from the db if ($files_to_clear) { foreach ($files_to_clear as $key => $filename) { \Storage::disk($disk)->delete($filename); $attribute_value = array_where($attribute_value, function ($value, $key) use ($filename) { return $value != $filename; }); } } // if a new file is uploaded, store it on disk and its filename in the database if ($request->hasFile($attribute_name)) { foreach ($request->file($attribute_name) as $file) { if ($file->isValid()) { // 1. Generate a new file name $new_file_name = md5($file.time()).'.jpg'; // 2. Move the new file to the correct path $file_path = $file->storeAs($destination_path, $new_file_name, $disk); // 3. Add the public path to the database $attribute_value[] = $file_path; } } } $this->attributes[$attribute_name] = json_encode($attribute_value); } public function setImagesAttribute($value) { $attribute_name = "images"; $disk = config('filesystems.default'); $destination_path = "venues/Images"; $this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path); } }