guimcaballero/laravel-folders

Folders to upload multiple number of files dynamically

dev-master 2020-05-05 10:15 UTC

This package is auto-updated.

Last update: 2025-06-05 21:50:16 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

This is a very simple package that implements Folders, where you can save as many files as you want to a model, while only having one column.

Folders behave just like a normal folder on you OS would, you can add, remove and list files. Adding files doesn't remove existing files.

Installation

You can install the package via composer:

composer require guimcaballero/laravel-folders

Then run the migrations:

php artisan migrate

Configuration

You can publish the migrations with:

php artisan vendor:publish --provider="Guimcaballero\LaravelFolders\LaravelFoldersServiceProvider"

Example

This is a simple use case where we want to be able to upload a list of important files in a User:

In App\User:

<?php

namespace App;

...
use Guimcaballero\LaravelFolders;

class User extends Authenticatable
{
    ...

    public function importantDocuments()
    {
        return $this->morphOne(Folder::class, 'folderable');
    }
}

Then in web.php:

    Route::post('user/uploadImportantFiles', function(Request $request) {
        $folder = auth()->user()->importantDocuments;
        $folder->uploadFiles($request->file('files'));

        dd($folder->getListOfFiles());
    });

Usage

Add a folder to a model

To add a folder to a model, add the following to the model class:

    public function importantFiles()
    {
        return $this->morphOne(Folder::class, 'folderable');
    }

Create a folder

To create a folder run:

    $folder = Folder::createNewRandomFolder();
    $user->importantFiles()->save($folder);

List all files in a folder

    $folder->getListOfFiles();

This will return an array with the files inside the folder.

Uploading multiple files

You can upload multiple files at once with:

    $folder->uploadFiles($request->file('files'));

Uploading a single file

You can upload a single file with:

    $folder->uploadSingleFile($request->file('file'));

Removing files

To remove multiple files, pass an array containing the names of the files:

    $folder->removeFiles(['importantFile1.pdf', 'importantFile2.pdf']);

Removing a single file

To remove a single file, pass the name of the file:

    $folder->removeSingleFile('importantFile1.pdf');

## Multiple folders

You can have more than one folder in a class, like for example:

    public function importantDocuments()
    {
        return $this->morphOne(Folder::class, 'folderable')->where('folderable_group', 'important');
    }

    public function lessImportantDocuments()
    {
        return $this->morphOne(Folder::class, 'folderable')->where('folderable_group', 'less_important');
    }

You then have to pass the corresponding group when creating the folder:

    $folder = Folder::createNewRandomFolder('important');
    $user->importantFiles()->save($folder);

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Feel free to Contribute to this project!

Please see CONTRIBUTING for details.

Credits

License

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