0

When trying to confirm by email I get this output:

PHP Fatal error: Trait "App\Http\Controllers\ValidateRequests" not found in app/Http/Controllers/CiudadanoController.php on line 16.

The line 16 is on:

class CiudadanoController extends Authenticatable implements MustVerifyEmail

this is the controller code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Ciudadano;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class CiudadanoController extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
    use ValidateRequests;


    public function store(Request $request)
    {

        $validated = $this->validate($request, [
            'cuil' => 'required',
            'nombre' => 'required',
            'apellido' => 'required',
            'email' => 'required',
            'password' => 'required',
        ]);
    
        $ciudadano = Ciudadano::create($validated);

        
        $ciudadano->save();
        event(new Registered($ciudadano));

        return("nice");

 }
4
  • The use declarations should be placed above the class. Commented Sep 28, 2022 at 18:32
  • 1
    It should be ValidatesRequests, note the s in the middle Commented Sep 28, 2022 at 18:37
  • @Innovin It's a trait, so the use statement is in the right place Commented Sep 28, 2022 at 18:38
  • Yeah, for traits in Laravel, it is very common to see use Path\To\MyTrait; before the class ... definition, then use MyTrait within the class. Since Laravel heavily uses namespaces, if you try to call use MyTrait, and it has a different namespace, it will fail with the message in the question. If they share the same namespace, then you don't have to; use MyTrait; within the class is all that's required. Spelling the trait correctly is another, separate issue though 😅 Commented Sep 28, 2022 at 19:04

1 Answer 1

1

You cannot use the trait name without a namespace prefix if they are not imported and not in the same namespace. You should import the trait of Illuminate\Foundation\Validation\ValidatesRequests or add a namespace prefix.

Alternative 1:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Ciudadano;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

############
use Illuminate\Foundation\Validation\ValidatesRequests;
############

class CiudadanoController extends Authenticatable implements MustVerifyEmail
{
    ###
    use ValidatesRequests;
    ###

    // ...
}

Alternative 2:


// ...

class CiudadanoController extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    ####
    use \Illuminate\Foundation\Validation\ValidatesRequests;
    ####


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

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.