Laravel如果用户没有头像,则显示默认头像。

huangapple go评论131阅读模式
英文:

Laravel display default avatar if user has none

问题

我允许用户上传他们的个人头像(个人资料图片)到他们的个人资料中。虽然上传头像完全是可选的,但如果用户没有上传头像,将会使用默认图片。然而,检查用户是否已经上传了头像或者没有上传头像是我的问题。只有当用户上传了头像时才会显示头像。

以下是我的视图文件中的代码:

@foreach($users as $user)  
    <div class="d-inline p-2">
        <div class="col"> 
            <div class="card" style="width: 18rem; background-color: #162238; color:white;">
                <br/>
                <img id="nature" src="{{asset('avatars/' . ($user->avatar ?? 'default1.jpg'))}}" class="imogi" style="width: 80px; border-radius: 20%" alt=""/>
                <div class="card-body" style="background-color: #162238; color:white;">
                    <h5 class="card-title" style="background-color: #162238; color:white; text-align:center;">{{$user->name}}</h5>
                </div>
                <ul class="list-group list-group-light list-group-small" style="background-color: #162238; color:white;">
                    <li class="list-group-item px-4" style="background-color: #162238; color:white; text-align:center;">
                        <i class="fa fa-building-o" style="color:#3490dc;"></i> {{$user->organization}}
                    </li>
                    <li class="list-group-item px-4" style="background-color: #162238; color:white; text-align:center;">
                        <i class="fa fa-map-marker" style="color:#3490dc;"></i> {{$user->location}}
                    </li>
                    <li class="list-group-item px-4" style="background-color: #162238; color:white; text-align:center; color:#3490dc;">
                        <small><i class="fa fa-clock-o" style="color:#3490dc;"></i> joined {{ \Carbon\Carbon::parse($user->created_at)->diffForHumans() }}</small>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <br/>
@endforeach
英文:

I am allowing user to upload an avatar (profile image) in their profile. While uploading avatar is entirely optional, if user does not have avatar, a default image will be used. However, checking whether a user has an avatar uploaded or not is my problem. It only display an avatar only if user has one.

Below is the code in my view file

  @foreach($users as $user)  
        &lt;div class=&quot;d-inline p-2&quot;&gt;
            &lt;div class=&quot;col&quot;&gt; 
            &lt;div class=&quot;card&quot; style=&quot;width: 18rem; background-color: #162238; color:white;&quot;&gt;
            &lt;br/&gt;
    &lt;img id=&quot;nature&quot; src=&quot;{{asset(&#39;avatars/&#39; . ($user-&gt;avatar ?? &#39;default1.jpg&#39;))}}&quot; class=&quot;imogi&quot; style=&quot;width: 80px; border-radius: 20%&quot; alt=&quot;&quot;/&gt;

  &lt;div class=&quot;card-body&quot; style=&quot;background-color: #162238; color:white;&quot;&gt;
    &lt;h5 class=&quot;card-title&quot; style=&quot;background-color: #162238; color:white; text-align:center;&quot;&gt;{{$user-&gt;name}}&lt;/h5&gt;
  &lt;/div&gt;
  &lt;ul class=&quot;list-group list-group-light list-group-small&quot; style=&quot;background-color: #162238; color:white;&quot;&gt;
    &lt;li class=&quot;list-group-item px-4&quot; style=&quot;background-color: #162238; color:white; text-align:center;&quot;&gt;&lt;i class=&quot;fa fa-building-o&quot; style=&quot;color:#3490dc;&quot;&gt;&lt;/i&gt; {{$user-&gt;organization}}&lt;/li&gt;
    &lt;li class=&quot;list-group-item px-4&quot; style=&quot;background-color: #162238; color:white; text-align:center;&quot;&gt;&lt;i class=&quot;fa fa-map-marker&quot; style=&quot;color:#3490dc;&quot;&gt;&lt;/i&gt; {{$user-&gt;location}}&lt;/li&gt;
    &lt;li class=&quot;list-group-item px-4&quot; style=&quot;background-color: #162238; color:white; text-align:center; color:#3490dc;&quot;&gt;&lt;small&gt;&lt;i class=&quot;fa fa-clock-o&quot; style=&quot;color:#3490dc;&quot;&gt;&lt;/i&gt; joined {{ \Carbon\Carbon::parse($user-&gt;created_at)-&gt;diffForHumans() }}&lt;/li&gt;&lt;/small&gt;
  &lt;/ul&gt;

&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
@endforeach

答案1

得分: 1

如果default1.jpg位于avatars文件夹中,请像下面这样添加括号:

&lt;img id=&quot;nature&quot; src=&quot;{{asset(&#39;avatars/&#39; . ($user-&gt;avatar ?? &#39;default1.jpg&#39;))}}&quot; class=&quot;imogi&quot; style=&quot;width: 80px; border-radius: 20%&quot; alt=&quot;&quot;/&gt;

这是因为点字符串连接操作符的优先级高于null合并操作符,按照您的方式,结果是:

asset((&#39;avatars/&#39; . $user-&gt;avatar) ?? &#39;default1.jpg&#39;)

所以default1.jpg被引用的位置是错误的

<h2> 编辑 </h2>

如果这还不够:

  • 首先确保default1.jpg位于avatars文件夹中
  • 然后您可以测试*$user->avatar是否为NULL或其他空值,对于这种情况,您可以使用Elvis操作符?:* 而不是*??*,或者更安全地:
asset(&#39;avatars/&#39; . (($user-&gt;avatar ?? NULL) ?: &#39;default1.jpg&#39;))
  • 如果所有这些仍然无法解决问题,您需要在浏览器中检查页面源代码,查看在用户头像丢失时图像中设置了什么。完成此操作后,如果没有头像的用户具有错误的图像名称,并且始终相同,您可以将default1.jpg 重命名为在用户记录中列出的名称之一。

  • 如果某些用户缺少头像文件,另一个选项可能是:

@php
$avatarPath = &#39;avatars/&#39;;
$avatar = $avatarPath . ($user-&gt;avatar ?? &#39;&#39;);
$img = file_exists(public_path($avatar)) ? asset($avatar) ? asset($avatarPath . &#39;default1.jpg&#39;);
@endphp

&lt;img id=&quot;nature&quot; src=&quot;{{ $img }}&quot; class=&quot;imogi&quot; style=&quot;width: 80px; border-radius: 20%&quot; alt=&quot;&quot;/&gt;

(我现在无法测试)

更好的解决方案是运行一个批处理程序,更新具有不存在图像的记录。

英文:

If default1.jpg is in avatars folder, add brackets like so:

&lt;img id=&quot;nature&quot; src=&quot;{{asset(&#39;avatars/&#39; . ($user-&gt;avatar ?? &#39;default1.jpg&#39;))}}&quot; class=&quot;imogi&quot; style=&quot;width: 80px; border-radius: 20%&quot; alt=&quot;&quot;/&gt;

That is because dot string concatenation operator has precedence over the null coalescing operator and the result in your way is:

asset((&#39;avatars/&#39; . $user-&gt;avatar) ?? &#39;default1.jpg&#39;)

and so default1.jpg is referenced from a wrong location

<h2> EDIT </h2>

If that is not enough:

  • First make sure that default1.jpg is located in avatars folder
  • Then you can test if $user->avatar is NULL or other nullish value, you can use Elvis operator ?: insetad of ?? for this case, or more safely:
asset(&#39;avatars/&#39; . (($user-&gt;avatar ?? NULL) ?: &#39;default1.jpg&#39;))
  • If all of this still doesn't fix, you need to check the page source in the browser to see what is set in the image when the avatar for the user is missing.
    Once you have checked this, if the users with no avatar have a wrong image name and it's always the same, you can rename your default1.jpg like the one listed in the users record.

  • An other option, if certain users have a missing avatar file, can be something like:

@php
$avatarPath = &#39;avatars/&#39;;
$avatar = $avatarPath . ($user-&gt;avatar ?? &#39;&#39;);
$img = file_exists(public_path($avatar)) ? asset($avatar) ? asset($avatarPath . &#39;default1.jpg&#39;);
@endphp

&lt;img id=&quot;nature&quot; src=&quot;{{ $img }}&quot; class=&quot;imogi&quot; style=&quot;width: 80px; border-radius: 20%&quot; alt=&quot;&quot;/&gt;

(I can't test it now)

A better solution would be to run a batch that updates records that have a non-existent image

答案2

得分: 1

你可以在你的模型中创建一个方法来检查属性是否有值。如果没有值,那么赋予一个默认值。这样你就不需要编写语句来检查用户是否有头像了。例如,如果表中的列名是avatar。

public function getAvatarAttribute($value)
{
    if (!$value) {
        // 如果 'avatar' 为null,则返回默认图像的路径
        return asset('/public/images/profile-image.webp');
    }

    // 否则,返回实际的图像路径
    return $value;
}
英文:

You can create a method in your model to check if the attribute has value. If not then assign an default value. This way you don't have to write an statement to check if the user has an avatar. For example if the column name is avatar in your table.

public function getAvatarAttribute($value)
{
    if (!$value) {
        // If &#39;avatar&#39; is null, return the path to the default image
        return asset(&#39;/public/images/profile-image.webp&#39;);
    }

    // Otherwise, return the actual image path
    return $value;
}

huangapple
  • 本文由 发表于 2023年6月19日 15:58:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定