Hello,
Why not to make a class that extends Eloquent with this functionnality and get inheritance for Models that need it ?
Users can upload images. Later on, I need to access the images in several views. To do so flexibly, I want to be able to access an image's URL as if it were a regular attribute fetched from the model's table.
this works:
class imageUpload extends \Eloquent {
protected $fillable = ["filename", "user_id"];
protected $table = 'imageUploads';
public $storageDirectory = '/uploads/userImages/';
// fuglify
protected $appends = ["fileUrl", "thumbUrl"];
public function getFileUrlAttribute()
{
return $this->storageDirectory.$this->filename;
}
public function getThumbUrlAttribute()
{
return $this->storageDirectory."thumb/thumb_".$this->filename;
}
}
However, there are different types of images (represented by different models), so I would like to add this functionality to all of those models. The logical way seemed to press the $appends and the two added functions into a trait, but when I do that and command the model to use that trait, I receive:
Illuminate\Database\Eloquent\Model and myTraitsName define the same property ($appends) in the composition of imageUpload. However, the definition differs and is considered incompatible. Class was composed
How can I define such attributes in a model?
Please or to participate in this conversation.