Hello everyone, I've several models that shares thumbnail functionality, so extracted it to a trait and used it like this, I think it's kinda clear and can easily be documented
namespace App\Traits;
/**
* Add Thumnial functionality to models
*/
trait ThumbnailTrait
{
private function setThumbnail(?string $value, string $directory, array $size) : void
{
// ...
}
private function getThumbnail(?string $value, string $default) : string
{
// ...
}
}
namespace App\Models;
class Tool extends Model
{
use \App\Traits\ThumbnailTrait;
public function setThumbnailAttribute($value)
{
$this->setThumbnail($value, 'tools', [256, 256]);
}
public function getThumbnailAttribute($value)
{
return $this->getThumbnail($value, 'images/[email protected]');
}
}
but I was thinking that it may be used with properties to alter traits like the following
class Tool extends Model
{
use \App\Traits\ThumbnailTrait;
protected $thumbnail = [
'default' => 'images/[email protected]',
'directory' => 'tools',
'size' => [256, 256]
];
}
then rename the trait functions to setThumbnailAttribute and getThumbnailAttribute and
make then access the property directly with $this keyword without additional calls in the model
I'm just wondering what is the better way to do it, or if there is a best practice I'm not aware of?