Spatie 媒体库上传总是导致 "request does not have a file in a key named [...]"

Spatie Media Library upload ALWAYS leads to "request does not have a file in a key named [...]"

我已经按照 docs 上的描述安装了 Spatie Media Library。但是,每次我尝试使用 addMediaFromRequest('keyName') 保存媒体文件时,我都会收到此错误:

The current request does not have a file in a key named keyName

每个安装和入门指南,例如 Laravel-News.com 上的这个指南,都告诉我将其放入我的控制器中:

if (isset($data['avatar'])) {
    $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
}

我也是这么做的。尽管如此,我仍然收到这个“......请求没有文件......”错误......这真的让我很烦。我完全按照安装指南进行操作,但它不起作用。

这是我的模型、控制器和视图在注册过程中的样子:

User.php:

use Glorand\Model\Settings\Traits\HasSettingsField;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Models\Role;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\Models\Media;

class User extends Authenticatable implements HasMedia
{
    use Notifiable;
    use HasRoles;
    use HasSettingsField;
    use HasMediaTrait;

    // ...
}

RegisterController.php(注意我的 // 评论):

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    //If I check, if the avatar is set, I create a user without avatar
    if (isset($data['avatar'])) {
        $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
    }

    //If I don't check it, it leads to "... request does not have a file ..." error
    $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');

    return $user;
}

register.blade.php:

// ...
<div class="form-group row">
    <label for="avatar" class="col-md-4 col-form-label text-md-right">{{ __('Avatar (optional)') }}</label>

    <div class="col-md-6">
       <input id="avatar" type="file" class="form-control" name="avatar">
    </div>
</div>
// ...

我想提供上传头像的其他控制器也是如此。例如我的 UserController 导致同样的问题。

UserController.php

// ...
public function update(Request $request, User $user)
{
    if (isset($request->avatar)) {
        $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
    }
}
// ...

但是,如果我转储 $request->avatar 它不是空的!我明白了:

Illuminate\Http\UploadedFile {#473 ▼
  -test: false
  -originalName: "23133.png"
  -mimeType: "application/octet-stream"
  -error: 1
  #hashName: null
  path: ""
  filename: ""
  basename: ""
  pathname: ""
  extension: ""
  realPath: "/Users/myname/Desktop/Coding/MyProject/public"
  aTime: 1970-01-01 00:00:00
  mTime: 1970-01-01 00:00:00
  cTime: 1970-01-01 00:00:00
  inode: false
  size: false
  perms: 00
  owner: false
  group: false
  type: false
  writable: false
  readable: false
  executable: false
  file: false
  dir: false
  link: false
}

我终于找到了我的错误。我的系统是 运行 在 Mac 上与 Catalina。问题是我使用 php artisan serve 在本地托管我的项目。出于某种原因,访问临时文件夹的权限存在问题。

我现在 运行 在 valet 上,没有任何麻烦或错误...也许它会对某人有所帮助!