Wallogit.com
2017 © Pedro Peláez
MWI Polymorphic File Management
Simple and intuitive polymorphic file management service for Laravel., (*1)
This will support any of Laravel's disk drivers: "local", "ftp", "sftp", "s3", "rackspace", (*2)
composer require mwi/laravel-files php artisan mwi:files:install
If you would like to use the facade, add to your config/app.php aliases, (*3)
'aliases' => [
// ...
'MWIFile' => MWI\LaravelFiles\Facades\MWIFile::class,
// ...
],
If you're on laravel 5.5 or later the service provider will be automatically loaded and you can skip this step, if not, add to your config/app.php providers, (*4)
'providers' => [
// ...
MWI\LaravelFiles\ServiceProvider::class,
// ...
],
To verify the package was set up successfully you can use MWIFile and then call MWIFile::verify() in any method., (*5)
It should return the version of the service if successful., (*6)
Any model you would like to incorporate files just needs the relationship added, (*7)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
/**
* Relationships
*/
public function files()
{
return $this->morphMany(FileUpload::class, 'fileable');
}
// Specific type of relationship
public function photos()
{
// Where types value is equal to the value of `fileable_relationship` when saving
return $this->morphMany(FileUpload::class, 'fileable')->where('type', 'photos');
}
}
You can use any number of methods to upload your files., (*8)
NOTE Addition to any fields for CSRF or HTTP method the following fields available for use, (*9)
file REQUIRED Contains the file to be uploadedfileable_type is the model namespace your saving the file toofileable_id is the id of the specific resource to attach it toofileable_relationship references the name of the relationship you create in the ModelThe most basic being a simple one off form field. You can have any other number of inputs for your needs., (*10)
<form action="{{ route('file-upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file">
<input type="hidden" value="\App\User" name="fileable_type">
<input type="hidden" value="{{ $user->id }}" name="fileable_id">
<input type="hidden" value="files" name="fileable_relationship">
</form>
Once you have the package and your views set up there are three methods available for use, (*11)
/**
* @param \Illuminate\Http\UploadedFile $file The uploaded file
*
* @param string $disk The disk in which to upload the file too,
* defaults to local, meaning it will not be publicly accessible.
* Change to `public` for public files like profile photos.
* It's recommend to use `config('filesystems.default')` as a standard
* and then chagne as necessary for specific use cases
*
* @param Array $data An array requiring at least the following data, note that
* if this data is not all present it will simply upload the file
* and not be associtaed to a specific model:
* fileable_type
* fileable_id
* fileable_relationship
*/
MWIFile::upload($request->file('file'), 'local', $request->input());
/**
* @param \App\FileUpload $file The file resource
*
* Note that if the file is publicly accessiblethis
* will redirect to it rather than initialize a download
*/
MWIFile::download(FileUpload::latest()->first());
/**
* @param \App\FileUpload $file The file resource
*
* This just removed the relationship to the file,
* it does NOT delete the file from the filesystem
*/
MWIFile::remove(FileUpload::latest()->first());