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.
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
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
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING and CONDUCT for details.
If you discover any related issues, please using the issue tracker.
- [AngryDeer][http://angrydeer.ru]
The MIT License (MIT). Please see License File for more information.