PHP code example of mwi / laravel-files

1. Go to this page and download the library: Download mwi/laravel-files library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mwi / laravel-files example snippets


'aliases' => [
    // ...
    'MWIFile' => MWI\LaravelFiles\Facades\MWIFile::class,
    // ...
],

'providers' => [
    // ...
    MWI\LaravelFiles\ServiceProvider::class,
    // ...
],



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');
    }
}

/**
 * @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());