MrMilkShake
MrMilkShake

Reputation: 51

Laravel 5.8 Custom validation error 'class does not exist'

I'm am new to laravel and i am trying to get my custom validation rules to work on my controller. It's showing that the class does not exist.

ReflectionException thrown with message "Class App\Http\Controllers\StoreBooksRequest does not exist"

I made the request file using the artisan command.

lando artisan make:request StoreBooksRequest

this is my request file :

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreBooksRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
            'title' => 'required|unique:books|max:150',
            'description' => 'required',
            'isbn' => 'required|max:20'
        ];
    }
}

and this is the controller where i am trying to get the custom request rules to work :

namespace App\Http\Controllers;

use App\Book;
use Illuminate\Http\Request;

class BooksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $books = Book::all();

        return view('books.index', compact('books'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('books.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreBooksRequest $request)
    {
        $book = new Book();

        $book->title = $request->title;
        $book->description = $request->description;
        $book->isbn = $request->isbn;

        $book->save();
    }

I think the problem is with the error saying that the request file is in the Controllers folder and not in the standard Requests folder.

Upvotes: 2

Views: 1286

Answers (3)

Gustavo Jordan
Gustavo Jordan

Reputation: 49

When you execute the php artisan make:request Myrequestname, Laravel create the file inside the App\Http\Request subdirectory, so you need to be careful to use the right namespace, another thing you always had to be carefull is about the name you use, is not the same Mycontroller than mycontroller and is worst if your server is a Linux server, because the file system make differentiation beewteen Caps.

Upvotes: 0

Tawhidul Islam Khan
Tawhidul Islam Khan

Reputation: 116

You have not included the namespace of your custom request's class. Add use App\Http\Requests\StoreBooksRequest; after use Illuminate\Http\Request;

Upvotes: 4

Aditya Thakur
Aditya Thakur

Reputation: 2610

You seem to be using wrong namespace for your

 Class App\Http\Controllers\StoreBooksRequest

Your namespace is set to namespace App\Http\Requests; while you are calling it from controller, If you move your Class to App\Http\Requests.

Also, don't forget to import the class in your controller

use StoreBooksRequest

Upvotes: 0

Related Questions