英文:
Displaying a default picture when the picture of the profile does not exist using file_exists php
问题
我有一个原生的 PHP 应用程序,我正在尝试升级并使用 Laravel 框架。在用户仪表板页面上,有一个根据用户ID(empid)从图片目录中获取的个人资料图片。如果用户没有个人资料图片,会显示默认图片。我以前在原生的 PHP 中使用 file_exists 方法来实现这个功能。但是我不确定为什么在 Laravel 中它现在不起作用了。
以下是我的代码:
@if (file_exists(URL::asset('assets/img/faces/'.Auth::user()->empid.'.jpeg')))
<img alt="user-img" class="avatar avatar-xl brround" src="{{ URL::asset('assets/img/faces/'.Auth::user()->empid.'.jpeg') }}">
@else
<img alt="user-img" class="avatar avatar-xl brround" src="{{ URL::asset('assets/img/faces/default.jpeg') }}">
@endif
这段代码没有进入 if 语句块,每次都执行 else 语句块。
英文:
I have an application in native php, and I am trying to upgrade it and use laravel framework. In the user dashboard page, there is a profile picture that is brought from the pictures directory based on the user ID (empid). If the user does not have a profile picture, there is a default picture that is displayed. I used to do it in native php using file_exists method. But I am not sure why it is not working now with laravel.
Here is my code
@if (file_exists(URL::asset('assets/img/faces/'.Auth::user()->empid.'.jpeg')))
<img alt="user-img" class="avatar avatar-xl brround" src="{{ URL::asset('assets/img/faces/'.Auth::user()->empid.'.jpeg') }}">
@else
<img alt="user-img" class="avatar avatar-xl brround" src="{{ URL::asset('assets/img/faces/default.jpeg') }}">
@endif
The code does not enter the if block; all the time the else block is evaluated.
答案1
得分: 3
你犯的错误是将URL
传递给file_exists
,因为URL::asset
返回的是完整的URL而不是文件路径,所以你需要稍微调整一下,像这样:
$path = 'assets/img/faces/' . Auth::user()->empid . '.jpeg';
file_exists(public_path($path))
如果你将逻辑提取到一个函数中并返回一个头像,你的Blade代码可以简化,我会将它放在你的User
模型中。
// User.php
/**
* 如果存在,获取用户的头像,否则获取默认头像。
* @return string
*/
public function getAvatar()
{
$path = 'assets/img/faces/' . Auth::user()->empid . '.jpeg';
if (file_exists(public_path($path))) {
return asset($path);
}
return asset('assets/img/faces/default.jpeg');
}
然后在你的Blade模板中,移除@if @else块,并用以下简化的代码替换:
<img alt="user-img" class="avatar avatar-xl brround" src="{{ Auth::user()->getAvatar() }}">
英文:
The mistake you're doing is passing a URL
to file_exists
because URL::asset
returns a full url not a file path so you need to tweak a little bit something like this
$path = 'assets/img/faces/'.Auth::user()->empid.'.jpeg'
file_exists(public_path($path))
Your blade code can be simplified if you extracted the logic to a function that returns an avatar, I would place it in your User
model.
// User.php
/**
* Get the user's avatar if it exists otherwise gets a default avatar.
* @return string
*/
public function getAvatar()
{
$path = 'assets/img/faces/' . Auth::user()->empid . '.jpeg';
if (file_exists(public_path($path))) {
return asset($path);
}
return asset('assets/img/faces/default.jpeg');
}
Then in your blade remove the @if @else block and replace it with this simplified
<img alt="user-img" class="avatar avatar-xl brround" src="{{ Auth::user()->getAvatar() }}">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论