PHP code example of williamoliveira / attachable

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

    

williamoliveira / attachable example snippets




namespace App\Models;

use App\Services\TenantContext;
use Intervention\Image\Image as InterventionImage;
use Williamoliveira\Attachable\Models\AttachableModel;

class Image extends AttachableModel
{
    // Set true if you want the model to accepts only images
    public $onlyImages = true;

    // Define the path where images will be stored, you can use id, filename, template and extension wildcards
    // (Fallsback to 'images_fallback_path' in your config file)
    public $imagesPath = 'images/{id}/{filename}--{template}.{extension}';
    
    // Define the path where files will be stored, you can use id, filename and extension wildcards
    // (Only for AttachableModel) 
    public $filesPath = 'files/{id}/{filename}.{extension}';
    
    // Define the default image template
    // (Optional, defaults to 'original') 
    protected $defaultTemplate = 'normal';
    
    // Define the Laravel Filesystem disk wich you be used to store the files
    // (Optional, fallsback to 'default_disk' on config file)
    public $disk = 'local_public';

    // Define your image modification, you can use anything from the Image Intervetion API
    // These image templates will be stored to disk, kinda like what Wordpress does, if you are familiar
    public function imageTemplates()
    {
        return [
            'original' => function (InterventionImage $image) {
                return $image;
            },
            'normal' => function (InterventionImage $image) {
                return $image->resize(800, null, function ($constraint) {
                    $constraint->aspectRatio();
                });
            },
            'thumbnail' => function (InterventionImage $image) {
                return $image->sharpen(10)->fit(200, 150);
            }
        ];
    }
}

//...
    public function image()
    {
        return $this->morphOne(\App\Models\Image::class, 'attachable');
    }
//...

$image = Image::create(['file' => $myUploadedFile]);

'local_public' => [
  'driver' => 'local',
  'root'   => public_path('storage'),
],

composer san vendor:publish
php artisan migrate