PHP code example of sabbir268 / laravel-filecaster

1. Go to this page and download the library: Download sabbir268/laravel-filecaster 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/ */

    

sabbir268 / laravel-filecaster example snippets


'aliases' => Facade::defaultAliases()->merge([
    // ...
    'filecast' => Sabbir268\LaravelFileCaster\FileCaster::class,
    // ...
])->toArray(),

use Sabbir268\LaravelFileCaster\FileCaster;

use App\Models;
use Sabbir268\LaravelFileCaster\FileCaster;

class Blog extends Model
{
    // ...
    protected $casts = [
        'image' => FileCaster::class,
    ];
    // ...
}


use App\Models;

class Blog extends Model
{
    // ...
    protected $casts = [
        'image' => 'filecast',
    ];
    // ...
}

$blog = new Blog();
$blog->image = $request->file('image');
$blog->save();

$blog = Blog::find(1);
echo $blog->image; // output: /storage/blog/1/image.jpg

// get file name
$blog->image->name; // output: image.jpg

// get file extension
$blog->image->extension; // output: jpg

// get file size
$blog->image->size; // output: 1024

// get image width
$blog->image->width; // output: 200

// get image height

$blog->image->height; // output: 200

// get file mime type
$blog->image->mime; // output: image/jpeg

// get file http url
$blog->image->url; // output: http://example.com/storage/blog/1/image.jpg

// get file full path
$blog->image->path; // output: /var/www/html/storage/app/public/blog/1/image.jpg

// get storage directory
$blog->image->dir; // output: /var/www/html/storage/app/public/blog/1

// check if file exists
$blog->image->exists; // output: true

$blog->image->url('200x200'); // output: http://example.com/storage/cache/200x200/blog-2-image1.jpg

$blog->image->remove(); // output: true
sh
php artisan vendor:publish --provider="Sabbir268\LaravelFileCaster\FileCasterServiceProvider"