2017-09-16 62 views
0

我想在我的雄辯模型推額外的屬性上計算 ,我想用它在我的刀片作爲$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 

我該如何做到這一點?

+0

看看[這個問題](https://stackoverflow.com/questions/17232714/add-a-custom-attribute-to-a-laravel-eloquent -load-model),可能會對您有所幫助 –

回答

0

看起來像屬性user在您的模型中不存在。 如果這是用戶模式嘗試只

$this->put('avatarImg', $img); 
+0

感謝您的反饋意見。試過以前也不行'調用未定義的方法照亮\數據庫\查詢\生成器:: put()'' – deroccha

+0

試試也許$ this-> avatarImg = $ img; –

+0

嘗試不追加 – deroccha