angrydeer/attachfiles

Add some attached files to Eloquent model and store it to storage/app/attached/{Y-m-d} dir.

dev-master / 1.0.x-dev 2016-02-18 11:31 UTC

This package is not auto-updated.

Last update: 2024-04-27 16:14:39 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Intro

Developed for easy work with the administration panel, such as Sleeping-Owl admin where all downloaded files are stored in a single folder and are not deleted if the record is not saved.

Attahfiles creates a link between a record of any model and a collection of downloaded files. He keeps them in the Storage assigning unique names, creates a new folder for each month, and day and deletes the originals from the shared folder upload.

When you remove attach, file is deleted from Storage.

You can add a file field Title, Alt, and Description. If the field Alt while maintaining empty, it is taken as the original file name.

Install

Add to composer.json in require section:

"angrydeer/attachfiles": "dev-master"

Make composer update

after that add to config/app.php:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
         Angrydeer\Attachfiles\AttachfilesServiceProvider::class,
         // ...
     ],

console:

$ php artisan vendor:publish
$ php artisan migrate

Usage

In model:

// beginning omitted
  
use Angrydeer\Attachfiles\AttachableTrait;
use Angrydeer\Attachfiles\AttachableInterface;

class SomeModel extends Model implements AttachableInterface
{

   use AttachableTrait;
   
   public function myfunc()
   {

       $model = SomeModel::find(1);
    
       $model->updateOrNewAttach($filename, $title, $alt, $desc);
    
       $model->removeAttach($filename);
       
       $all_attach_files = $model->attaches;
       
       $filearray = [ 'file1', 'file12', 'file3' ]
       
       /*
       * as example, in $all_attach_files now attaches with  'file2', 'file3', 'file4'    
       *
       */
       
       $model->keepOnly($filearray);
       $all_attach_files = $model->attaches;
       
       //  'file2', 'file3' included. file4 removed.
  
   }
  
}

in route.php you can write for images as example:

    Route::get('attaches/{date}/{filename}', function ($date,$filename) {
        return Storage::get('attaches/'.$date.'/'.$filename);
    });
    

in view:

        @foreach($model->attaches as $attach)
            <img src="{{URL::to($attach->filename)}}" alt="{{$attach->alt}}" title="{{$attach->title}}">
        @endforeach
    

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

issues

If you discover any related issues, please using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.