Search code examples
phplaraveluploadmedia-librarylaravel-medialibrary

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


I have installed Spatie Media Library exactly how it is described on their docs. However, every time I try to save a media file with addMediaFromRequest('keyName') I get this error:

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

Every installation and getting started guide, such as this one on Laravel-News.com, is telling me to put this in my controller:

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

That's also what I have done. Nevertheless I still get this "... request does not have a file ..." error... which really annoys me. I follow exactly the instalttion guides but it is not working.

This is how my Models, Controllers and View looks like for the registration procedure:

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 (Note my // comments):

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>
// ...

The same also goes for other controllers where I want to give the possibility to upload avatars. For example my UserController leads to the same problem.

UserController.php

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

However, if I dump the $request->avatar it is not empty! I get this:

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
}

Solution

  • I finally found my error. My system is running on a Mac with Catalina. The problem is that I was hosting my project locally with php artisan serve. For some reason there where a problem with the permission to access the temp folder.

    I am now running on valet without any hassle or error... maybe it's gonna help someone!