PHP code example of solbeg / laravel-files-manager

1. Go to this page and download the library: Download solbeg/laravel-files-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

solbeg / laravel-files-manager example snippets


    // ...
    Solbeg\FilesManager\FilesManagerServiceProvider::class,
    // ...

    // ...
    \Solbeg\FilesManager\StoreUploadedFilesMiddleware::class,
    // ...

    // ...
    Intervention\Image\ImageServiceProvider::class,
    // ...

    // ...
    'Image' => Intervention\Image\Facades\Image::class,
    // ...


use Solbeg\FilesManager\ModelFilesTrait;

class Product extends ...\Eloquent\Model
{
    use ModelFilesTrait;

    protected $fillable = [..., 'logo_photo', ...];

    protected function filesAttributes()
    {
        return [
            // attribute name => the name of context
            'logo_photo' => 'product-logo',

            // or context config right here
            //'logo_photo' => [
            //    'formats' => [
            //        'thumbnail' => 'image/thumb: width = 200, height = 300',
            //    ],
            //
            //    'validate' => [
            //        'types' => 'image/jpeg, image/png',
            //        'extensions' => ['jpg', 'jpeg', 'png'],
            //        // ...
            //    ],
            //],
        ];
    }
}

    $product = new Product;
    $product->fill($request->all());

    // File from input (if passed) will be saved automatically in `public/uploads/product-logo/hashed-subfolder-123/some-hashed-name.jpg`
    // Formatted 'thumbnail' version (for example) will be generated on fly and saved in `public/uploads/product-logo/hashed-subfolder-123/formats/some-hashed-name.jpg/thumbnail.jpg`
    // String 'hashed-subfolder-123/some-hashed-name.jpg' will be saved in database in `logo_photo` column.
    $product->save();

    $product = Product::find(1);
    $product->logo_photo = null; // or ''
    $product->save(); // file will be removed from database and from disk

    // returning url to origin file
    $product->logo_photo->url();
    $product->logo_photo->url;
    $product->logo_photo->href;
    (string) $product->logo_photo;

    // returning url of formatted as `thumbnail` versions of file
    $product->logo_photo->url('thumbnail');
    $product->logo_photo->asThumbnail;

    // Check whether file is empty or not
    $product->logo_photo->exists(); // whether file exists
    $product->logo_photo->exists('thumbnail'); // whether formatted file exists
    $product->logo_photo->isEmpty(/* null or 'thumbnail' */); // reverse of `exists()` method

    // Fetch other params
    $product->logo_photo->size(); // size of origin file in bytes
    $product->logo_photo->size('thumbnail'); // size of formatted file in bytes
    $product->logo_photo->mimeType(); // MIME type of origin file
    $product->logo_photo->mimeType('thumbnail'); // MIME type of formatted file
    $product->logo_photo->lastModified(); // the last modified timestamp of origin file
    $product->logo_photo->lastModified('thumbnail'); // the last modified timestamp formatted file
    $product->logo_photo->image()->width(); // origin image width in pixels
    $product->logo_photo->image('thumbnail')->width(); // formatted image width in pixels
    $product->logo_photo->image()->height(); // origin image height in pixels
    $product->logo_photo->image('thumbnail')->height(); // formatted image height in pixels

    return response()->json($product);
    /* Returns something like:
        {
            // ...
            "logo_photo": {
                "url": "http://...",
                "path" => "0001/...",
                "formats": {
                    "thumbnail": "http://...",
                    "small": "http://...",
                    ...
                }
            },
            // ...
        }

// ...
$this->validate($request, [
    // ...
    'logo_photo' => 'ribute',
    // ...
]);
// ...

$ php composer.phar 

$ php artisan filecontext:create-symlink

$ php composer.phar 

$ php ./artisan vendor:publish --provider="Solbeg\FilesManager\FilesManagerServiceProvider" --tag=config

$ php ./artisan make:filecontext {context-name}
twig
    {{-- Output <img ... /> tag --}}
    <img src="{{ $product->logo_photo }}" alt='' />
    <img src="{{ $product->logo_photo->asThumbnail }}" alt='' />
    <img src="{{ $product->logo_photo->url() }}" alt='' />
    <img src="{{ $product->logo_photo->url('thumbnail') }}" alt='' />

    {{ $product->logo_photo->img() }} <!-- outputs 'img' tag -->
    {{ $product->logo_photo->img('thumbnail') }} <!-- outputs 'img' tag -->
    {{ $product->logo_photo->img(null, ['id' => 'some-tag-id']) }} <!-- outputs 'img' tag -->
    {{ $product->logo_photo->link() }} <!-- outputs 'a' tag -->
    {{ $product->logo_photo->link('thumbnail') }} <!-- outputs 'a' tag -->
    {{ $product->logo_photo->link(null, 'Download Title', ['class' => 'some-css-class']) <!-- outputs 'a' tag -->
    {{ $product->logo_photo->link(null, $product->logo_photo->img('thumbnail')) <!-- outputs link with image -->

    {{-- Check if file is not empty --}}
    @if($product->logo_photo->exists())
    // ...
    @endif
    @if($product->logo_photo->isEmpty())
    // ...
    @endif

    {{-- Output other characteristics --}}
    Size: {{ $product->logo_photo->size() }}
    Mime Type: {{ $product->logo_photo->mimeType() }}
    ...