【发布时间】:2020-11-23 21:25:00
【问题描述】:
我的 Laravel 应用程序中的用户在他们的模型/数据库中都有一个 profile_image 列。如果他们是注册用户,则可以将其设置为他们喜欢的任何图像,但我遇到了没有为他们的个人资料图像设置图像的问题,因此如果您尝试注册新用户,则会引发错误。
SQLSTATE[HY000]:一般错误:1364 字段“profile_image”没有默认值(SQL:插入users(name、email、password、updated_at , created_at) 值
我尝试使用它来检查该字段是否为空,以便它返回要使用的默认图像的路径。否则,它应该返回用户的 profile_image 值。
public function profileImage()
{
if (is_null($this->profile_image)){
$imagePath = 'defaultavatar.png';
} else {
$imagePath = $this->profile_image;
}
return '/storage/avatars/' . $imagePath;
}
当然,这会在用户个人资料的刀片文件中使用。
<img src="{{ Auth::user()->profileImage() }}" class="img-fluid rounded-circle shadow-sm" height="150px" width="150px">
但似乎不起作用。它只是给出了上面的 sqlstate 错误。
【问题讨论】: