How to Use custom request (make:request)? (laravel) Method App\Http\Requests\Custom::doesExistI does not exist

可紊 提交于 2019-12-11 22:03:27

问题


I created a custom request to make my own validation. As i follow these article.

I created ProfileRequest

php artisan make:request ProfileRequest

Inside my ProfileRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProfileRequest 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 [
            'name' => 'required|min:10',
            'age' => 'required|numeric'
        ]; 
    }
}


My problem is when I use the ProfileRequest inside the controller, like below:
namespace App\Http\Controllers;

use Illuminate\Http\Request;
class ProfileController extends Controller
{
    public function update(ProfileRequest $request){
        return "123";
    }
}

It returns an error like these below:

Class App\Http\Controllers\ProfileRequest does not exist

I need your help Sirs. Somebody know how to use the custom request?


回答1:


In ProfileRequest change extends FormRequest to Request. And add use Illuminate\Http\Request; above the class. Code would like below.

<?php

namespace App\Http\Requests;

use Illuminate\Http\Request;

class ProfileRequest 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 [
            'name' => 'required|min:10',
            'age' => 'required|numeric'
        ]; 
    }
}

And put this App\Http\Requests\ProfileRequest; in above controller.




回答2:


You have imported use Illuminate\Http\Request;

You have to import ProfileRequest instead of Request on your controller,

just this:

use App\Http\Requests\ProfileRequest;



回答3:


Try with this, First of all You have to import request in your controller,

use App\Http\Requests\ProfileRequest;


 $validated = $request->validated();
        if ($validated) {
             // ...
         }

Hope this helps :)




回答4:


You need to import App\Http\Requests\ProfileRequest in your controller

like: use App\Http\Requests\ProfileRequest

then try: composer dumpautoload, php artisan cache:clear




回答5:


In your ProfileRequest.php change

use Illuminate\Foundation\Http\FormRequest;

to

use Illuminate\Http\FormRequest;

And then just

php artisan config:cache 
composer dumpautoload


来源:https://stackoverflow.com/questions/55631139/how-to-use-custom-request-makerequest-laravel-method-app-http-requests-cus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!