Laravel-medialibrary: The current request does not have a file in a key named `avatar`

Created on 27 Jul 2018  ·  17Comments  ·  Source: spatie/laravel-medialibrary

Hey guys, had encountered this problem... did not see anything wrong in my code to be honest... check it pls.

This is the form:
```

                    </div>
this is the user model:
```public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumb')
            ->width(60)
            ->height(60);
    }

this is the RegisterController:
```$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'type' => $type,
]);
if (isset($data['avatar'])) {
$user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
}

    return $user;

`` filesystems.php: 'default' => env('FILESYSTEM_DRIVER', 'public'),`

yes, the user model has:
```use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\AuthUser as Authenticatable;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class User extends \TCG\Voyager\ModelsUser
{
use Notifiable;
use HasMediaTrait;

Most helpful comment

Does your opening form tag contain the right enctype?

<form action="/your-url" method="post" enctype="multipart/form-data">

All 17 comments

Does your opening form tag contain the right enctype?

<form action="/your-url" method="post" enctype="multipart/form-data">

Hello, just checked... that's not the problem, the problem is another... the error code it gives me now is: Interface 'App\HasMediaConversions' not found

my user model has this:

<?php

namespace App;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
    use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
    use Illuminate\Database\Eloquent\User as EloquentUser;

class User extends \TCG\Voyager\Models\User implements HasMediaConversions
{
    use Notifiable, HasMediaTrait;

But i really don't know what is going on...

You need to import the correct namespace in order to use HasMediaConversions.

Put use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; at the top of your class.

More info: https://docs.spatie.be/laravel-medialibrary/v6/converting-images/defining-conversions

image

well... still not working :)

image

I've followed the documentation, but... Undefined namespace Interfaces and Undefined class HasMediaConversions

So basically, the issue has not been solved :P

@freekmurze

Can you install the latest version of the package in a vanilla Laravel app?

What do you mean about a vanilla laravel app?

I mean, i want to use the package on my project... and i don't have any idea why this is happening, basically this is not something i caused...

I think the problem is something specific to your app. All tests of the
package are passing. With over 500k installs we would get more reports if
the package was not installable.

Try following the install instructions to the letter. Sometimes it helps
installing the package in a new project to more easily figure out what is
going wrong in your old project.

On Sat, 28 Jul 2018 at 00:44, CriptoMex notifications@github.com wrote:

I mean, i want to use the package on my project... and i don't have any
idea why this is happening, basically this is not something i caused...


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/spatie/laravel-medialibrary/issues/1190#issuecomment-408584810,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAdiDYvCd8aBcuq819xfmrB_6RPFc6zBks5uK_o9gaJpZM4Vj_KS
.

>

Freek Van der Herten https://spatie.be +32 495 84 27 91

Ok, let my try to install it in a brand new project, let's see what is going on...

Just installed it in a brand new project, still getting the same problem:
image

made some changes to my user model:
```

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\User as EloquentUser;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class User extends \TCG\Voyager\ModelsUser implements HasMedia
{
use Notifiable, HasMediaTrait;

But then, what happens here is that the page now correctly loads, the problem is that when i sign up a new user and add an avatar, i get the following error: `The current request does not have a file in a key named "avatar"` the register form has the following in it: `<form method="POST" action="{{ route('register') }}" aria-label="{{ __('Register') }}" enctype=”multipart/form-data”>`
and 
```<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>

So it seems that my code is ok, then, why is it throwing me that exception?

in my registercontroller i have this:
```if (isset($data['avatar'])) {
$user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
}

    return $user;

```

Have you checked the contents of the request coming in to your app?

Hello freek! I've decided to make a use of the package out of the user model and... well, it works, but it only crashes when i try to make use of it in the user model...

It seems to me that your problem is more related to your app and not to our package.

For general questions around Laravel and how to structure your app, please use a public forum like laravel.io or the laracasts forum.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

swash13 picture swash13  ·  3Comments

denitsa-md picture denitsa-md  ·  3Comments

kickthemooon picture kickthemooon  ·  4Comments

aaronfullerton picture aaronfullerton  ·  4Comments

amrnn90 picture amrnn90  ·  3Comments