Simple and intuitive polymorphic file management service for Laravel.
This will support any of Laravel's disk drivers: "local", "ftp", "sftp", "s3", "rackspace"
composer require mwi/laravel-files
php artisan mwi:files:installTo use the facade add to your config/app.php aliases
'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
'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.
It should return the version of the service if successful.
Any model you would like to incorporate files just needs the relationship added
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
/**
* Relationships
*/
public function files()
{
return $this->morphMany(FileUpload::class, 'fileable');
}
}You can use any number of methods to upload your files.
NOTE Addition to any fields for CSRF or HTTP method the following fields ARE ALWAYS REQUIRED
fileobviously contains the file to be uploadedfileable_typeis the model namespace your saving the file toofileable_idis the id of the specific resource to attach it toofileable_relationshipreferences the name of the relationship you create in the Model
The most basic being a simple one off form field. You can have any other number of inputs for your needs.
<form action="{{ route('file-upload') }}" method="POST" enctype="multipart/form-data">
<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 two methods available for use
/**
* The following will look for the `file` input in the request and
* upload to the appropriate location while attaching it to the
* model and resource specified by the other fields.
*/
MWIFile::upload($request);
/**
* This method takes the file resource specified and removes it from
* the filesystem disk specified.
*/
MWIFile::remove($file);