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.
You can install the package via composer:
composer require guimcaballero/laravel-folders
Then run the migrations:
php artisan migrate
You can publish the migrations with:
php artisan vendor:publish --provider="Guimcaballero\LaravelFolders\LaravelFoldersServiceProvider"
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());
});
To add a folder to a model, add the following to the model class:
public function importantFiles()
{
return $this->morphOne(Folder::class, 'folderable');
}
To create a folder run:
$folder = Folder::createNewRandomFolder();
$user->importantFiles()->save($folder);
$folder->getListOfFiles();
This will return an array with the files inside the folder.
You can upload multiple files at once with:
$folder->uploadFiles($request->file('files'));
You can upload a single file with:
$folder->uploadSingleFile($request->file('file'));
To remove multiple files, pass an array containing the names of the files:
$folder->removeFiles(['importantFile1.pdf', 'importantFile2.pdf']);
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);
Please see CHANGELOG for more information what has changed recently.
Feel free to Contribute to this project!
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.