Home > front end >  Method Illuminate\Validation\Validator::validateRequired|image|mimes does not exist. register imag
Method Illuminate\Validation\Validator::validateRequired|image|mimes does not exist. register imag

Time:08-22

I'm trying to do so when a user registers that he puts an image, I tried to do that but I got the error that appears

RegisterController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Models\Role;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
 
class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'secretword' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'image' => ['required|image|mimes:jpeg,png,jpg,gif,svg|max:1000'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        $newUser = User::create([
            'name' => $data['name'],
            'secretword' => $data['secretword'],
            'email' => $data['email'],
            'image' => $data['image'],
            'password' => Hash::make($data['password']),
        ]);

        if(User::count() > 1){
            $adminRole = Role::where('name', 'like', 'ROLE_USER')->first();
            $newUser->roles()->attach($adminRole->id);
        }else{
            $adminRole = Role::where('name', 'like', 'ROLE_SUPERADMIN')->first();
            $newUser->roles()->attach($adminRole->id);
            $adminRole = Role::where('name', 'like', 'ROLE_USER')->first();
            $newUser->roles()->attach($adminRole->id);
        }

        return $newUser;

    }
}

User migration

    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('secretword');
            $table->string('image')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->integer('reactions')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    }

User model


namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Qirolab\Laravel\Reactions\Traits\Reacts;
use Qirolab\Laravel\Reactions\Contracts\ReactsInterface;

class User extends Authenticatable implements MustVerifyEmail, ReactsInterface
{
    use HasApiTokens, HasFactory, Notifiable, Reacts;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'image',
        'password',
        'secretword',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function roles()
    {
        return $this
            ->belongsToMany(Role::class)
            ->withTimestamps();
    }

    public function users()
    {
        return $this
            ->belongsToMany('App\User')
            ->withTimestamps();
    }

    public function authorizeRoles($roles)
    {
        if ($this->hasAnyRole($roles)) {
            return true;
        }
        abort(401, 'This action is unauthorized.');
    }

    public function hasAnyRole($roles)
    {
        if (is_array($roles)) {
            foreach ($roles as $role) {
                if ($this->hasRole($role)) {
                    return true;
                }
            }
        } else {
            if ($this->hasRole($roles)) {
                return true;
            }
        }
        return false;
    }

    public function hasRole($role)
    {
        if ($this->roles()->where('name', $role)->first()) {
            return true;
        }
        return false;
    }
};

View register

@extends('layouts.app')

@section('content')
<div >
    <div >
        <div >
            <div >
                <div >{{ __('Register') }}</div>

                <div >
                    <form method="POST" action="{{ route('register') }}">
                        @csrf

                        <div >
                            <label for="name" >{{ __('Name') }}</label>

                            <div >
                                <input id="name" type="text"  name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

                                @error('name')
                                <span  role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div >
                            <label for="email" >{{ __('Email Address') }}</label>

                            <div >
                                <input id="email" type="email"  name="email" value="{{ old('email') }}" required autocomplete="email">

                                @error('email')
                                <span  role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div >
                            <label for="image" >{{ __('Image') }}</label>

                            <div >
                                <input type="file"  id="Image" name="image" required>
                                <label  for="exampleInputFile">Choose file</label>

                                @error('image')
                                <span  role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>


                        <div >
                            <label for="secretword" >{{ __('secretword') }}</label>

                            <div >
                                <input id="secretword" type="text"  name="secretword" value="{{ old('secretword') }}" required autocomplete="secretword" autofocus>

                                @error('secretword')
                                <span  role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div >
                            <label for="password" >{{ __('Password') }}</label>

                            <div >
                                <input id="password" type="password"  name="password" required autocomplete="new-password">

                                @error('password')
                                <span  role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div >
                            <label for="password-confirm" >{{ __('Confirm Password') }}</label>

                            <div >
                                <input id="password-confirm" type="password"  name="password_confirmation" required autocomplete="new-password">
                            </div>
                        </div>

                        <div >
                            <div >
                                <button type="submit" >
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

How can I do so that when a user registers he must upload an image (the profile image) to be able to register, I have the error that appeared I do not know how to fix it

CodePudding user response:

In your validator function, this line seems wrong syntax

'image' => ['required|image|mimes:jpeg,png,jpg,gif,svg|max:1000'],

replace with this

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1000',

or this

'image' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:1000'],
  • Related