El conde Lucanor's avatar

Custom Form Request Validation = "Name is already in use"

I was trying to do a custom form request validation with simples rules, but for some reason when I submit the form the next error appear:

Cannot declare class App\Http\Requests\StoreArticuloRequest, because the name is already in use

The problem is in the ArticulosController.php due to I call the custom request StoreArticuloRequest but the function store is telling me the next error:

Undefined type 'App\Http\Requests\Articulos\StoreArticuloRequest'. intelephense(1009)

What is the problem? I call a lot of times other files with use, why it's appearing right now this problem with this particular custom request validation?

I tried to write App\Http\... or app\Http\... and that's not the reason of the error.

Any idea?

ArticulosController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
// This is the correct route because I'm directly copying his relative route from files sidebar
use App\Http\Requests\Articulos\StoreArticuloRequest;

class ArticulosController extends Controller
{
    /* 
    This "StoreArticuloRequest" is underlined with the
    "Undefined type 'App\Http\Requests\Articulos\StoreArticuloRequest'. intelephense(1009)"
    */
    public function store(StoreArticuloRequest $request)
    {
		//
    }
}

StoreArticuloRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreArticuloRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required',
            'text' => 'required',
        ];
    }
}

the controller show me the next error Undefined type 'App\Http\Requests\Articulos\StoreArticuloRequest'.intelephense(1009)

0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message suggests that the name "StoreArticuloRequest" is already in use somewhere in your application. One possible solution is to rename your custom form request class to something else, such as "CreateArticuloRequest".

To do this, you need to update the class name and the filename of your form request class. Here's an example:

  1. Rename the file StoreArticuloRequest.php to CreateArticuloRequest.php.
  2. Update the namespace and class name in CreateArticuloRequest.php:
namespace App\Http\Requests\Articulos;

use Illuminate\Foundation\Http\FormRequest;

class CreateArticuloRequest extends FormRequest
{
    // ...
}
  1. Update the controller to use the new class name:
use App\Http\Requests\Articulos\CreateArticuloRequest;

class ArticulosController extends Controller
{
    public function store(CreateArticuloRequest $request)
    {
        // ...
    }
}

Make sure to update any other references to StoreArticuloRequest in your application to use the new class name.

1 like
El conde Lucanor's avatar

@LaryAI Oh my god, an AI solve my problem hahaha.

In StoreArticuloRequest I should change the namespace App\Http\Requests; to namespace App\Http\Requests\Articulos; because I created a specific folder to save all these custom request validation.

My gosh, thanks future job stealer!

tykus's avatar

Which one is it:

App\Http\Requests\Articulos\StoreArticuloRequest

or

App\Http\Requests\StoreArticuloRequest

You need to ensure that you are accurate with your namespaces and that the underlying directory structure is consistent with that namespace

1 like
El conde Lucanor's avatar

@tykus Problem solved!

Question:

If I created the StoreArticuloRequest.php in the App\Http\Requests route instead of App\Http\Requests\Articulos, the namespace of the StoreArticuloRequest.php should be namespace App\Http\Requests;?

Edit: Ok, I tried and yes, that's the point.

Thanks m8 ;)

Please or to participate in this conversation.