我想在我的雄辯模型推額外的屬性上計算 ,我想用它在我的刀片作爲$user->avatarImg
我的模型方法看起來像增加新的屬性laravel雄辯收集
public function avatarImg()
{
if($this->hasMedia('avatar')) {
$image = $this->getMedia('avatar');
$img = \Intervention\Image\ImageManagerStatic::make($image[0]->getPath())->encode('data-url');
}
elseif ($this->blob->file) {
$img = 'data:image/jpeg;base64,'. base64_encode($this->blob->file);
} else {
$img = '';
}
$this->user->put('avatarImg', $img);
dd($this->user);
return $img;
}
但我嘗試不工作我得到:
Call to a member function put() on null
我該如何做到這一點?
看看[這個問題](https://stackoverflow.com/questions/17232714/add-a-custom-attribute-to-a-laravel-eloquent -load-model),可能會對您有所幫助 –