Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TzuSun67's avatar

Avatar Validation error when registering new user. "The avatar must be an image"

Hi Everyone,

I uploaded a question earlier however due to my late reply back haven't had an answer so made a new post for the updated reply.

I am trying to register a new user however I keep getting the error "The Avatar must be an image".

I have looked over my code countless times and cannot see any issues with it. Maybe I just need a fresh pair of eyes to have a look and it maybe obvious.

I have tried looking online however the solutions for other people that have worked in the past don't work for me as I have already set what they were talking about adding up.

My code is bellow. I have included the file system as I was working on another image upload for products. But thought I would include it just in case anyone mentions it.

Controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    use RegistersUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'userlevel' => ['required','string'],
            'avatar' => ['required', 'image', 'mimes:jpg,jpeg,bmp,svg,png', 'max:5000'],
        ]);
    }

    protected function create(array $data)
    {
        if(request()->has('avatar')){
            $profileimage = request()->file('avatar');
            $proimgname = time() .'.'. $profileimage -> getClientOriginalExtension();
            $proimgsave = public_path('/uploads/avatars/');
            $profileimage -> move($proimgsave, $proimgname);
            return User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
                'userlevel' => $data['userlevel'],
                'avatar' => '/uploads/avatars/'. $proimgname,
            ]);
        }

    }
}

View

@extends('layouts.app')

@section('content')
<div class="content">
    <div><h2 class="header">{{ __('Register') }}</h2></div>
    <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data ">
        @csrf
        <div class="form">
            <div class="name">
                <label for="name" class="left-form">{{ __('Name') }}</label>
                    <input id="name" type="text" class="right-form @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
                    @error('name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="email" class="left-form">{{ __('E-Mail Address') }}</label>
                    <input id="email" type="email" class="right-form @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
                    @error('email')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>

            <div class="holder">
                <label for="avatar" class="left-form">{{ __('Avatar') }}</label>
                <input id="avatar" class="right-form @error('avatar') is-invalid @enderror" type="file" name="avatar">
                @error('avatar')
                <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="password" class="left-form">{{ __('Password') }}</label>
                    <input id="password" type="password" class="right-form @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
                    @error('password')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                @enderror
            </div>
            <div class="confirm-pass">
                <label for="password-confirm" class="left-form">{{ __('Confirm Password') }}</label>
                    <input id="password-confirm" type="password" class="right-form" name="password_confirmation" required autocomplete="new-password">
            </div>
{{--            <div class="holder">--}}
{{--                <label for="bio" class="left-form">{{ __('Bio') }}</label>--}}
{{--                <textarea id="bio" type="text" class="right-form @error('bio') is-invalid @enderror" name="bio" value="{{ old('bio') }}" placeholder="Please enter your bio." autofocus></textarea>--}}
{{--                @error('bio')--}}
{{--                <span class="invalid-feedback" role="alert">--}}
{{--                        <strong>{{ $message }}</strong>--}}
{{--                    </span>--}}
{{--                @enderror--}}
{{--            </div>--}}
{{--            <div class="holder">--}}
{{--                <label for="experience" class="left-form">{{ __('Experience Level') }}</label>--}}
{{--                <input id="experience" type="text" class="right-form @error('experience') is-invalid @enderror" name="experience" value="{{ old('experience') }}" placeholder="Whats your experiene level?" autofocus>--}}
{{--                @error('experience')--}}
{{--                <span class="invalid-feedback" role="alert">--}}
{{--                        <strong>{{ $message }}</strong>--}}
{{--                    </span>--}}
{{--                @enderror--}}
{{--            </div>--}}
{{--            <div class="holder">--}}
{{--                <label for="years_played" class="left-form">{{ __('Years Played?') }}</label>--}}
{{--                <input id="years_played" type="text" class="right-form @error('years_played') is-invalid @enderror" name="years_played" value="{{ old('years_played') }}" placeholder="How many years have you been playing?" autofocus>--}}
{{--                @error('years_played')--}}
{{--                <span class="invalid-feedback" role="alert">--}}
{{--                        <strong>{{ $message }}</strong>--}}
{{--                    </span>--}}
{{--                @enderror--}}
{{--            </div>--}}
{{--            <div class="holder">--}}
{{--                <label for="gear" class="left-form">{{ __('Your Gear') }}</label>--}}
{{--                <input id="gear" type="text" class="right-form @error('gear') is-invalid @enderror" name="gear" value="{{ old('gear') }}" placeholder="What Gear do you use?" autofocus>--}}
{{--                @error('gear')--}}
{{--                <span class="invalid-feedback" role="alert">--}}
{{--                        <strong>{{ $message }}</strong>--}}
{{--                    </span>--}}
{{--                @enderror--}}
{{--            </div>--}}

                <input id="userlevel" name="userlevel" class="input" type="text" placeholder="" value="user" hidden>
            <div class="submit">
                <div class="reset-reg">
                    <button type="submit" class="btn btn-primary">
                        {{ __('Register') }}
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection

Model

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password','updated_at', 'userlevel','avatar',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    private $email;

    public function isAdmin()
    {
        return $this->userlevel === 'admin';
    }
}

Filesystem

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
    */

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "sftp", "s3"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => public_path('uploads/products/'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

    ],

];

Hopefully you'll be able to see something I can't, Ben :)

0 likes
23 replies
bobbybouwmann's avatar

It seems that the validation fails because the selected file for the avatar field is not a valid image. That's the only thing I can spot

TzuSun67's avatar

Hi Bobby,

That's what I don't understand as the image I am uploading is a jpg.

I think I am declaring it correctly but after doing the products area images it seems to have messed up for some reason. This was only last week.

Any Ideas?

TzuSun67's avatar

I have commented out the validation field for the image to see if it would pass anything through and instantly hit this error.

"Symfony\Component\Debug\Exception\FatalThrowableError Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, null given, called in C:\XAMPP\htdocs\Term_One_Assignment_Benjamin_Robinson_U1655703\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 35 "

Any idea how to fix this? I haven't even touched this file so have no idea why it is throwing up an error.

TzuSun67's avatar

Do you have any ideas how I could fix that error above so that it will let me upload an image into the database and fix my validation? I have tried removing everything to do with the avatar upload and it works fine. It also assigns the default image that I am using so the errors are definetly all coming from the image upload but I have no idea how as it exactly the same as my product image upload (which works).

Testing the code

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    use RegistersUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'userlevel' => ['required','string'],
//            'avatar' => 'required|image|max:5000',
        ]);
    }

//    const RULES = [
//        'avatar' => 'required|image|mimes:jpg,jpeg,bmp,svg,png|max:5000',
//    ];
//
//    const MESSAGES = [
//        'avatar.required' => 'You need to have an image for the avatar.',
//    ];

    protected function create(array $data)
    {
//        $data->validate(self::RULES, self::MESSAGES);
        if(request()->hasFile('avatar')){
            $avatar= request()->file('avatar');
            $avatarname = time() .'.'. $avatar -> getClientOriginalExtension();
            $avatarsave = public_path('/uploads/products/');
            $avatar->move($avatarsave, $avatarname);
            return User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
                'userlevel' => $data['userlevel'],
                'avatar' => '/uploads/avatars/'. $avatarname,
            ]);
        }
    }
}

Any ideas?

Snapey's avatar

If you dont have an image then you skip the entire create block. The login function is expecting the newly created user, but you did not create one.

protected function create(array $data)
    {
//        $data->validate(self::RULES, self::MESSAGES);

        $avatar =null;

        if(request()->hasFile('avatar')){
            $avatar= request()->file('avatar');
            $avatarname = time() .'.'. $avatar -> getClientOriginalExtension();
            $avatarsave = public_path('/uploads/products/');
            $avatar->move($avatarsave, $avatarname);
            $avatar =  '/uploads/avatars/'. $avatarname;
         }
         
         return User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
                'userlevel' => $data['userlevel'],
                'avatar' => $avatar,
            ]);
        }
    }
}
TzuSun67's avatar

I have implemented your fix however I am getting this error.

The file part of the form doesn't seem to be passing the value of the file through to the Controller as it just gives this error.

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'avatar' cannot be null (SQL: insert into users (name, email, password, userlevel, avatar, updated_at, created_at) values (benj, [email protected], $2y$10$lWn.ZY6qufilRIK4tjAvF.TFSXm5ryao4cKvi5kWWMq2ZWKGTMpRi, user, ?, 2020-04-09 21:21:17, 2020-04-09 21:21:17))

I have tried it with 0 and it passes through and saves the data so it looks like it isn't seeing a file come through from the form.

Any ideas as to why this is happening?

Snapey's avatar

If a user does not want to provide an image, then the avatar column needs to be nullable()

You have not said that it can be in your users table migration.

TzuSun67's avatar

Initially I set it up by setting the default value to /uploads/avatars/default.jpg so that every user would get an image assigned.

How would I set this up and keep the column with a default value?

TzuSun67's avatar

This is my users migration

<?php

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->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
//            $table->string('bio');
//            $table->string('experience')->default('beginner');
//            $table->integer('years_played')->default('0');
//            $table->string('gear');
            $table->string('userlevel');
            $table->string('avatar')->default('/uploads/avatars/default.jpg');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
TzuSun67's avatar

Would this be how it is done?

$table->string('avatar')->default('/uploads/avatars/default.jpg')->nullable();
TzuSun67's avatar

just figured out a work around I've set the

$avatar = null;

to

$avatar = '/uploads/avatars/default.jpg';

Set the default value to null now. I imagine this would bypass the nullable() migration on the users area.

TzuSun67's avatar

Tried the updated code and the issue looks to be due to the

$avatar = '/uploads/avatars/'. $avatarname;

value not replacing the

$avatar = '/uploads/avatars/default.jpg';

Could this be due to the the value being called after the initial value?

New code looks like this for reference

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    use RegistersUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'userlevel' => ['required','string'],
//            'avatar' => 'required|image|max:5000',
        ]);
    }

    protected function create(array $data)
    {
//        $data->validate(self::RULES, self::MESSAGES);

        $avatar = '/uploads/avatars/default.jpg';

        if(request()->hasFile('avatar')){
            $avatar = request()->file('avatar');
            $avatarname = time() .'.'. $avatar -> getClientOriginalExtension();
            $avatarsave = public_path('/uploads/products/');
            $avatar -> move($avatarsave, $avatarname);
            $avatar = '/uploads/avatars/'. $avatarname;
        }

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'userlevel' => $data['userlevel'],
            'avatar' => $avatar,
        ]);
    }
}
Snapey's avatar

You can use the default in the migration, but in which case, do not pass the avatar to the create method.

Reorganise the controller;

    protected function create(array $data)
    {
//        $data->validate(self::RULES, self::MESSAGES);

	$user = new User;

        if(request()->hasFile('avatar')){
            $avatar = request()->file('avatar');
            $avatarname = time() .'.'. $avatar -> getClientOriginalExtension();
            $avatarsave = public_path('/uploads/products/');
            $avatar -> move($avatarsave, $avatarname);
            $user->avatar = '/uploads/avatars/'. $avatarname;
        }

        $user->fill([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'userlevel' => $data['userlevel'],
        ]);

        $user->save();

        return $user;
    }
TzuSun67's avatar

Hi,

I'm really sorry to keep bothering you like this but it still isn't passing a value through to the data base.

I've been trying to get it to work through new functions trying to pull the data out of the if and all sorts but it just keeps saying that the value is null.

Any ideas on this?

TzuSun67's avatar

Hi All,

Really sorry to keep this unsolved but I am still having issues adding an Auth image when registering a new user. Is any body still able to help that may fix the issue? My code had not adavanced since @snapey's last reply.

Kind Regards, Ben

TzuSun67's avatar

Hi Snapey,

Sorry for the late reply I had a few deadlines to hand in.

The other fields do get set I have added the extra info in that I wanted to add in and it works fine but the Avatar is still having issues. I have not used functions like this before where you call a variable in two different functions. I think the issue is that the $user variable doesn't get set in the first one so is null when called in the second.

Here is the error I recieve.

Method App\User::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException: Error encoding model [App\User] with ID [] to JSON: Recursion detected 

Here is my Controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    use RegistersUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'userlevel' => ['required','string'],
//            'avatar' => 'required|image|max:5000',
        ]);
    }

//    protected function create(array $data)
//    {
////        $data->validate(self::RULES, self::MESSAGES);
//
//        $avatar = '/uploads/avatars/default.jpg';
//
//        if(request()->hasFile('avatar')){
//            $avatar = request()->file('avatar');
//            $avatarname = time() .'.'. $avatar -> getClientOriginalExtension();
//            $avatarsave = public_path('/uploads/products/');
//            $avatar -> move($avatarsave, $avatarname);
//            $avatar = '/uploads/avatars/'. $avatarname;
//        }
//
//        return User::create([
//            'name' => $data['name'],
//            'email' => $data['email'],
//            'password' => Hash::make($data['password']),
//            'userlevel' => $data['userlevel'],
//            'avatar' => $avatar,
//        ]);
//    }

    protected function create(array $data)
    {
        $user = new User;

        if(request()->hasFile('avatar')){
            $avatar = request()->file('avatar');
            $avatarname = time() .'.'. $avatar -> getClientOriginalExtension();
            $avatarsave = public_path('/uploads/products/');
            $avatar -> move($avatarsave, $avatarname);
            $user->avatar = '/uploads/avatars/'. $avatarname;
        }

        $user->fill([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'userlevel' => $data['userlevel'],
            'bio' => $data['bio'],
            'gear' => $data['gear'],
            'experience' => $data['experience'],
            'years_played' => $data['years_played'],
            'avatar' => $user,
        ]);

        $user->save();

        return $user;
    }
}

View

@extends('layouts.app')

@section('content')
<div class="content">
    <div><h1     class="header">{{ __('Register') }}</h1></div>
    <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data ">
        @csrf
        <div class="form">
            <div class="name">
                <label for="name" class="left-form">{{ __('Name') }}</label>
                    <input id="name" type="text" class="right-form @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
                    @error('name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="email" class="left-form">{{ __('E-Mail Address') }}</label>
                    <input id="email" type="email" class="right-form @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
                    @error('email')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>

            <div class="holder">
                <label for="avatar" class="left-form">{{ __('Avatar') }}</label>
                <input id="avatar" class="right-form @error('avatar') is-invalid @enderror" type="file" name="avatar" placeholder="">
                @error('avatar')
                <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="password" class="left-form">{{ __('Password') }}</label>
                    <input id="password" type="password" class="right-form @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
                    @error('password')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                @enderror
            </div>
            <div class="confirm-pass">
                <label for="password-confirm" class="left-form">{{ __('Confirm Password') }}</label>
                    <input id="password-confirm" type="password" class="right-form" name="password_confirmation" required autocomplete="new-password">
            </div>
            <div class="holder">
                <label for="bio" class="left-form">{{ __('Bio') }}</label>
                <textarea id="bio" type="text" class="right-form @error('bio') is-invalid @enderror" name="bio" value="{{ old('bio') }}" placeholder="Please enter your bio." autofocus></textarea>
                @error('bio')
                <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="experience" class="left-form">{{ __('Experience Level') }}</label>
                <input id="experience" type="text" class="right-form @error('experience') is-invalid @enderror" name="experience" value="{{ old('experience') }}" placeholder="Whats your experiene level?" autofocus>
                @error('experience')
                <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="years_played" class="left-form">{{ __('Years Played?') }}</label>
                <input id="years_played" type="text" class="right-form @error('years_played') is-invalid @enderror" name="years_played" value="{{ old('years_played') }}" placeholder="How many years have you been playing?" autofocus>
                @error('years_played')
                <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="holder">
                <label for="gear" class="left-form">{{ __('Your Gear') }}</label>
                <input id="gear" type="text" class="right-form @error('gear') is-invalid @enderror" name="gear" value="{{ old('gear') }}" placeholder="What Gear do you use?" autofocus>
                @error('gear')
                <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>

                <input id="userlevel" name="userlevel" class="input" type="text" placeholder="" value="user" hidden>
            <div class="submit">
                <div class="reset-reg">
                    <button type="submit" class="btn btn-primary">
                        {{ __('Register') }}
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection

Model

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password','updated_at','userlevel','bio','gear','experience','years_played','avatar'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    private $email;

    public function isAdmin()
    {
        return $this->userlevel === 'admin';
    }
}

Everything else works fine but the Avatar area isn't.

I have done these types of functions in C++ and they work how I have put it in Laravel so I imagine it is a completely different in this syntax.

Any Ideas anyone? :)

bobbybouwmann's avatar
Level 88

Your problem is in below code

$user = new User;

if (request()->hasFile('avatar')) {
    // Create avatar

    $user->avatar = '/uploads/avatars/'. $avatarname;
}

 $user->fill([
    // Other fields
    
    'avatar' => $user,      <------ HERE IS THE PROBLEM ----->
]);

You assign the user model to the avatar key, but that variable is then pointing to itself. You simply have to remove avatar => $user, here and it should work as expected!

TzuSun67's avatar

Thank you Bobby,

This has fixed the code!! :D

I'll make sure not to call it like that in laravel in future!

Thanks again, Ben

thebuybest's avatar

When I tried creating new functions to access the data from the conditional, it always said the value was null.

Could you shed any light on this?

Please or to participate in this conversation.