Giter VIP home page Giter VIP logo

laravel-folders's Introduction

Description

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.

laravel-folders's People

Contributors

guimcaballero avatar

Stargazers

 avatar

Watchers

 avatar

laravel-folders's Issues

great repo

does the package make money like money printer go brrr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.