3

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?

schutte
  • 1,949
  • 7
  • 25
  • 45

6 Answers6

4

In ProfileRequest change extends FormRequest to extends Request. Then add use Illuminate\Http\Request; above the class. Code should look 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'
        ]; 
    }
}

Then put App\Http\Requests\ProfileRequest; in your controller.

Shuvojit
  • 1,390
  • 8
  • 16
1

create ProfileRequest

php artisan make:request Profile/ProfileRequest

inside ProfileRequest

<?php

namespace App\Http\Requests\Profile;

use Illuminate\Foundation\Http\FormRequest;

class ProfileUpdate 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'
        ]; 
    }
}

Then put use App\Http\Requests\Profile\ProfileRequest in the controller.

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Anil Stha
  • 515
  • 7
  • 12
0

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

Johhn
  • 979
  • 17
  • 24
  • Sir it returns an error `Class App\Http\Controllers\ProfileRequest does not exist`. And bdw the `ProfileRequest` I made is a request stored in `App/Http/Requests/` – schutte Apr 11 '19 at 11:20
  • I tried Sir but returns an error like this `Method App\Http\Requests\ProfileRequest::doesExistI does not exist.` can you check the codes of my `ProfileRequest` Sir for me. thank you for your response – schutte Apr 11 '19 at 11:29
0

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;
Sethu
  • 1,299
  • 10
  • 13
  • Sir it returns an error `Method App\Http\Requests\ProfileRequest::doesExistI does not exist.` can you check the code of my `ProfileRequest` Sir, I think the problem is in there, I'm not sure – schutte Apr 11 '19 at 11:21
  • can you please add your full `ProfileController` class and `ProfileRequest` class here? – Sethu Apr 11 '19 at 11:50
  • that is my full `ProfileController` Sir – schutte Apr 11 '19 at 11:59
0

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 :)

Maulik Shah
  • 1,040
  • 1
  • 7
  • 17
  • Sir it returns an error `Method App\Http\Requests\ProfileRequest::doesExistI does not exist.`. I think the problem is in the `ProfileRequest` codes Sir – schutte Apr 11 '19 at 11:24
  • I observe Sir in the article **Good**, the custom request extends a request. Can you check it for me Sir. thanks for response – schutte Apr 11 '19 at 11:26
  • Actually I implemented it with FormRequest, at that time i dont know it with request, No problem, thank you for that tutorial. You can check it with `composer dump-autoload`. – Maulik Shah Apr 11 '19 at 11:30
  • I tried `composer dump-autoload` but still returns this error `Method App\Http\Requests\ProfileRequest::doesExistI does not exist.` I really don't know what's wrong with this. – schutte Apr 11 '19 at 11:32
  • Tried `php artisan cache:clear` but still returns the same error :( – schutte Apr 11 '19 at 11:44
  • try with `new ProfileRequest` in method parameter – Maulik Shah Apr 11 '19 at 11:49
  • error Sir `syntax error, unexpected 'new' (T_NEW), expecting variable (T_VARIABLE)` – schutte Apr 11 '19 at 11:54
  • Something freaky – Maulik Shah Apr 11 '19 at 12:30
0

In your ProfileRequest.php change

use Illuminate\Foundation\Http\FormRequest;

to

use Illuminate\Http\FormRequest;

And then just

php artisan config:cache 
composer dumpautoload