PHP code example of tjslash / cto-set-image-trait
1. Go to this page and download the library: Download tjslash/cto-set-image-trait 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/ */
tjslash / cto-set-image-trait example snippets
namespace App\Models;
use Tjslash\CtoSetImageTrait\Traits\SetImageTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
...
class SimpleModel extends Model
{
use SetImageTrait;
/**
* Image options
*
* @return array
*/
protected $imageOptions = [
'image' => [
'destination_path' => 'category',
'disk' => 'public'
]
];
/**
* Get/set image attribute
*
* @return Attribute
*/
public function image() : Attribute
{
return Attribute::make(
get: fn($value) => $value,
set: fn($value) => self::setImage($value, 'image')
);
}
...
}