Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel create request not working

Tags:

php

laravel

You can probably see Im very new to laravel. I have ran into an issue where it cant seem to see the new class I've made...

Firstly I ran....

php artisan make:request CreateSongRequest

which in turn generated a CreateSongRequest.php file in app/Http/Requests/

The contents...

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateSongRequest extends Request {

    /**
     * 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 [

            //
        ];
    }

}

In my controller I have the form post to the following method...

public function store(CreateSongRequest $request, Song $song) {


    $song->create($request->all());

    return redirect()->route('songs_path');
}

When I submit the form, Im getting the following error...

ReflectionException in RouteDependencyResolverTrait.php line 53: Class App\Http\Controllers\CreateSongRequest does not exist

like image 640
Adrian Avatar asked Mar 17 '15 14:03

Adrian


People also ask

What happens when a validation error occurs in Laravel?

When Laravel generates a redirect response due to a validation error, the framework will automatically flash all of the request's input to the session. This is done so that you may conveniently access the input during the next request and repopulate the form that the user attempted to submit.

What is the difference between authorize and rules in Laravel?

Each form request generated by Laravel has two methods: authorize and rules. As you might have guessed, the authorize method is responsible for determining if the currently authenticated user can perform the action represented by the request, while the rules method returns the validation rules that should apply to the request's data:

What is the use of productupdaterequest in Laravel?

Since ProductUpdateRequest inherits from FormRequest, Laravel will know to inject the class as the $request parameter as well as run the authorize and validation logic before hand. If the validation fails, Laravel will return the user to the form with the errors.

Why should I use Laravel in my project?

Laravel does an incredible job helping developers break their code into smaller parts, which reduces code clutter and makes the code easier to read. I recently discovered how to create a custom form request for handling validation during a form submission. Let’s pretend we have created a Product model and want to update the name or price fields.


1 Answers

You need to add this at the top of your controller:

use App\Http\Requests\CreateSongRequest;
like image 72
JoeCoder Avatar answered Sep 29 '22 20:09

JoeCoder