示例#1
0
 /**
  * A model is saving, check for files being uploaded
  *
  * @param Illuminate\Database\Eloquent\Model $model 
  * @return void
  */
 public function onSaving(Model $model)
 {
     // Check that the model supports uploads through Upchuck
     if (!$this->supportsUploads($model) || !($map = $model->getUploadMap())) {
         return;
     }
     // Loop through the all of the upload attributes ...
     foreach ($map as $key => $attribute) {
         // If there is a file in the input, move the upload to the
         // config-ed disk and save the resulting URL on the model.
         if ($this->request->hasFile($key)) {
             $url = $this->storage->moveUpload($this->request->file($key));
             $model->setUploadAttribute($attribute, $url);
             // Remove the file from the request object after it's been processed.
             // This prevents other models that may be touched during the processing
             // of this request (like because of event handlers) from trying to act
             // on this upload.
             $this->request->files->remove($key);
         }
         // If the attribute field is dirty, delete the old image
         if ($model->isDirty($attribute) && ($old = $model->getOriginal($attribute))) {
             $this->storage->delete($old);
         }
     }
 }
示例#2
0
 /**
  * A model is saving, check for files being uploaded
  *
  * @param Illuminate\Database\Eloquent\Model $model
  * @return void
  */
 public function onSaving(Model $model)
 {
     // Check that the model supports uploads through Upchuck
     if (!$this->supportsUploads($model) || !($attributes = $model->getUploadAttributes())) {
         return;
     }
     // Loop through the all of the upload attributes ...
     foreach ($attributes as $attribute) {
         // Check if there is an uploaded file in the upload attribute
         if (($file = $model->getAttribute($attribute)) && is_a($file, UploadedFile::class)) {
             // Move the upload and get the new URL
             $url = $this->storage->moveUpload($file);
             $model->setUploadAttribute($attribute, $url);
         }
         // If the attribute field is dirty, delete the old image
         if ($model->isDirty($attribute) && ($old = $model->getOriginal($attribute))) {
             $this->storage->delete($old);
         }
     }
 }