在我的對象User中,我有一個可選的配置文件圖片。 我想顯示它,如果它爲空,則PIC設置爲非標準avatar.pngLaravel 5.1中的getPictureAttribute模型
因此,我認爲好的地方是在模型中,有:
public function getPictureAttribute($picture){
return is_null($picture) ? 'avatar.png' : $picture;
}
在我看來: 的代碼,我想避免
@if (!is_null($user->picture))
<img src="{{ $user->picture) }}" />
@else
<img src="{{ "avatar.png") }}" />
@endif
我想要的代碼,但失敗,因爲它沒有返回avatar.png但空
<img src="{{ $user->picture) }}" />
圖片爲空,我不知道爲什麼?
有人解釋?
嘗試與setPictureAttribute功能 –