PHPackages                             0config/file-cabinet - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. 0config/file-cabinet

ActiveLibrary[File &amp; Storage](/categories/file-storage)

0config/file-cabinet
====================

file cabinet

v1.8.8201114(5y ago)052MITPHP

Since May 17Pushed 5y ago1 watchersCompare

[ Source](https://github.com/0config/file_cabinet)[ Packagist](https://packagist.org/packages/0config/file-cabinet)[ RSS](/packages/0config-file-cabinet/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (16)Used By (0)

Note: if you get `'Unable to create the directory' error in laravel (on the server)`

- please make sure you have `storage` folder in `public` folder
- with permission `chmod 777 storage/`
- please see how this file permission should look like below

```
ll -d storage/
drwxrwxrwx 2 username username 52 Nov 12 15:56 storage/
```

Enable logging
==============

[](#enable-logging)

add a log channel to `config/logging.php` like this inside `'channels' => ` node

```
'file_cabinet' => [
            'driver' => 'single',
            'path'   => storage_path('logs/file_cabinet.log'),
            'level'  => 'info',
],
```

// Try writing log as below , from you application

```
Log::channel('file_cabinet')->info('Hello world!!');
```

and see : `tail -f storage/logs/file_cabinet.log`

If config is not done properly, log may end up in standard log

Migrate :
=========

[](#migrate--)

move migration folder to local migration `mv vendor/0config/file-cabinet/src/database/migrations/*.php database/migrations`

migrate up : `php artisan migrate`

#### Note: if migrate up does not work, Please run `php artisan migrate --path=vendor/0config/file-cabinet/src/database/migrations/`

[](#note-if-migrate-up-does-not-work-please-run-php-artisan-migrate---pathvendor0configfile-cabinetsrcdatabasemigrations)

Route example from local / web.php :
------------------------------------

[](#route-example-from-local--webphp-)

```
// IMPORT BELOW two lines
//use Illuminate\Http\Request;
//use ZeroConfig\FileCabinet\App\Http\Controllers\UploadFileController;

// for file_cabinet starts

Route::get('/local_files/{mimetype}/{model_name}/{model_id}:{channel}::{id}/', function (Request $request , $mimetype,  $model_name, $model_id, $channel, $id ) {

    $info = UploadFileController::validateRecord($request );

    return view('local_upload', compact('info',  'mimetype', 'model_id', 'model_name', 'channel' ));

});
Route::post('/local_files/{mimetype}/{model_name}/{model_id}:{channel}::{id}/', function (Request $request , $mimetype,  $model_name, $model_id, $channel, $id ) {
    return UploadFileController::upsert($request, $mimetype);
});

Route::get('/local_files/{mimetype}/destroy/{model_name}/{model_id}:{channel}::{id}/', function (Request $request) {
    return UploadFileController::destroy($request);
});

// for file_cabinet ENDS
```

create view in appropriate local path
-------------------------------------

[](#create-view-in-appropriate-local-path)

- file name `local_upload.blade.php` location: `resources/views`

```

 File Cabinet : Universal File Manager

             File Manager

                Image File

                Name

           @php( $allFiles =  ( \ZeroConfig\FileCabinet\FileCabinet::select(['id', 'model_name', 'model_id', 'name', 'file_name', 'channel'])
                            ->where('user_id',  \Illuminate\Support\Facades\Auth::id()) )
                            ->where('model_name', $model_name)
                            ->where('model_id', $model_id)
                            ->where('channel', $channel)
                            ->get()
            )

            @foreach($allFiles as $fileArr)
                model_id-{{ $fileArr->model_id }}  ::
                id-{{ $fileArr->id }}  ::
                channel-{{ $fileArr->channel }}  ::
                file_name-{{ $fileArr->file_name }} ::

            @endforeach

```

make sure to load this template from route file :
-------------------------------------------------

[](#make-sure-to-load-this-template-from-route-file-)

```
/local_files/image/FileCabinet/3:1::0

/{static_path}/{mimetype}/{ModelName}/{modelId}:{channel}:{fileCabId}/
fileCabId = 0 ; // new file
fileCabId > 0 ; // will edit record based on this value

```

eg: /local\_files/image/FileCabinet/3:1::0 this will `CREATE` an entry for `FileCabinet` model's id= 2, set channel = 1 and `::0` will create a new record

e.g.: /local\_files/image/FileCabinet/1:5::12 this will `UPDATE` entry for `FileCabinet` model's id= 1, set channel = 5 and `::12` will update FileCabinet.id = 12

Relationships :
===============

[](#relationships-)

in `User` model : add below
---------------------------

[](#in-user-model--add-below)

```
// import on top
// use ZeroConfig\FileCabinet\FileCabinet;
    public function files()
    {
        return $this->hasMany(FileCabinet::class);
    }

    public function filesUser()
    {
        return $this->hasMany(FileCabinet::class)
            ->where('model_name', '=', 'User');
    }
    public function filesFileCabinet()
    {
        return $this->hasMany(FileCabinet::class)
            ->where('model_name', '=', 'FileCabinet');
    }
```

in your web.php // add routes
-----------------------------

[](#in-your-webphp--add-routes)

```
Route::get('users/{id?}', function ($id = 1 ) {
    return \App\User::with(['filesUser', 'filesFileCabinet', 'files'])->find($id);
});
```

now create some records and browse :
------------------------------------

[](#now-create-some-records-and-browse-)

: `/users/1``/users/2`

you should get desired response.

Destroy
=======

[](#destroy)

/local\_files/{mimetype}/destroy/{model\_name}/{model\_id}:{channel}::{id}/

see there is `destroy` between routes, rest is same

Restrict and relax ownership :
==============================

[](#restrict-and-relax-ownership-)

`UploadFileController::upsert($request, false); // default 2nd param is true `if you set it to false ownership is not checked BE VERY CAREFUL this will transfer ownership as well..

Fancy one with sorting, list , image etc...
===========================================

[](#fancy-one-with--sorting-list--image-etc)

```

 File Cabinet : Universal File Manager

    #sortable {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }

    #sortable li {
        margin: 0 3px 3px 3px;
        padding: 0.4em;
        padding-left: 1.5em;
        font-size: 1em;
        /*height: 18px;*/
    }

    #sortable li span {
        position: absolute;
        margin-left: -1.3em;
    }

    .drag-me-sort {
        cursor: all-scroll;
        border: lightyellow 1px solid;
        padding: 10px;
        opacity: .7;
    }

    .drag-me-sort:hover {
        border: lightgrey 1px solid;
        opacity: 1;
    }

    #sortable .loading {
        animation: loadingText 0.3s infinite;
        height: 2px;
        background: green;
        overflow: hidden;
    }

             File Manager
             ($_SERVER['REQUEST_URI']), 'files' => 'true', 'data-toggle' => "validator", 'role' => "form", 'required' => 'true', "novalidate" => "true"));
            ?>

                Image File

                 'true', 'class' => 'form-control', 'accept' => $info['validMimetype']]); // if $mimeType is not passed default will be image type
                $imagePath = $info['fileCabinet']->file_name ?? null;
                ?>

                Name
                name ?? '', ['required' => 'true', 'class' => 'form-control']);
                ?>

             'true', 'class' => 'form-control btn btn-primary']);
            ?>

            @php( $allFiles =  ( \ZeroConfig\FileCabinet\FileCabinet::select(['id', 'model_name', 'model_id', 'name', 'file_name', 'channel', 'user_id', 'sortorder'])
                             // ->where('user_id',  \Illuminate\Support\Facades\Auth::id()) // allowing everyone to upload
                              )
                             ->where('model_name', $model_name)
                             ->where('model_id', $model_id)
                             ->where('channel', $channel)
                             ->orderBy('sortorder', 'DESC')
                             ->get()
             )

                .

                @foreach($allFiles as $fileArr)

                             file_name-{{ $fileArr->file_name }}
                              model_id-{{ $fileArr->model_id }} ::
                                id-{{ $fileArr->id }} ::
                                channel-{{ $fileArr->channel }} ::

                                user :: {{ $fileArr->user_id }}
                                sortorder :: {{ $fileArr->sortorder }}

                @endforeach

    $(function () {
        $("#sortable").sortable();
        $("#sortable").disableSelection();
    });

    // Instantiate the widget
    $('ul').sortable({});
    $('ul').on('sortupdate', function () {

        // console.log('update called');

        var iterLenght = Object.entries($('#sortable li')).length - 5;

        // ?json={"1":"5","9":"5"}
        var ajaxLink = '?json={'; // ?json={"1":"5","9":"5"}

        for (var i = 0; i
