2017-09-16 63 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