【问题标题】:Display a default profile picture in Laravel在 Laravel 中显示默认的个人资料图片
【发布时间】: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 错误。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你也许可以使用空对象模式

    https://laravel.com/docs/8.x/eloquent-relationships#default-models

    所以,你会做这样的事情

    public function profile() {
     return $this->belongsTo(Profile::class)
        ->withDefault([
             'image_path' => 'path to default image'
        ]);
    }
    

    所以这样你是安全的并且总是返回配置文件对象。

    【讨论】:

      【解决方案2】:

      您可以使数据库中的profile_image 列可以为空以消除错误,或者您可以为profile_image 列填充默认值。有两个选项可以填充个人资料图像的默认值。

      选项 1:(在数据库级别处理)

      将迁移文件中profile_image列的默认值设置为/storage/avatars/defaultavatar.png

      
      
      use Illuminate\Database\Migrations\Migration;
      use Illuminate\Database\Schema\Blueprint;
      use Illuminate\Support\Facades\Schema;
      
      class CreateUsersTable extends Migration
      {
          /**
           * Run the migrations.
           *
           * @return void
           */
          public function up()
          {
              Schema::create('users', function (Blueprint $table) {
                  $table->id();
                  $table->string('name');
                  $table->string('email')->unique();
                  $table->string('password');
                  $table->timestamp('email_verified_at')->nullable();
                  $table->string('profile_image')->default('/storage/avatars/defaultavatar.png')
                  $table->rememberToken();
                  $table->timestamps();
              });
          }
      
          /**
           * Reverse the migrations.
           *
           * @return void
           */
          public function down()
          {
              Schema::dropIfExists('users');
          }
      }
      

      选项2:(控制器中的句柄)

      Controller中收到注册新用户数据的POST请求时,检查profile_image是否有值,如果没有则填写默认值

          protected funtion profileImagePath()
          {
              $path = '/storage/avatars/defaultavatar.png';
              if(request()->hasFile('profile_image') && request()->file('profile_image')->isValid()) {
                  $path = request()->profile_image->store('avatars');
              }
              
              return $path;
          }
      
      //$data is validated Request data received via registration form
      
      /**
           * Create a new user instance after a valid registration.
           *
           * @param  array  $data
           * @return \App\User
           */
          protected function create(array $data)
          {
              return User::create([
                  'name' => $data['name'],
                  'email' => $data['email'],
                  'password' => Hash::make($data['password']),
                  'profile_image' => $this->profileImagePath(),
              ]);
          }
      

      【讨论】:

      • 我继续在数据库级别进行了操作,但感谢您让我知道如何检查列是否为空。这行得通!
      猜你喜欢
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2020-02-13
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      相关资源
      最近更新 更多