2

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.

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Awesome Thanks! the StoreBooksRequest was already in App\Http\Requests but importing it did the trick.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.