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/ */
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