Home > Enterprise >  cannot use my model on my controller " Undefined type 'App\Usuario' " Laravel
cannot use my model on my controller " Undefined type 'App\Usuario' " Laravel

Time:02-22

[CODE IMG AT THE END][1]

Hello I cannot use my model inside controller "Undefined type 'App\Usuario' " on Laravel.

When I try to, create, edit, or remove an user I am getting error " Undefined type 'App\Usuario' " appears. Sorry if my English is not good.

A few days ago the code worked for me but when I made another model called "paintings" they both stopped working. Maybe has some relation.

MY CODE:

Model:

<?php

use Illuminate\Database\Eloquent\Factories\HasFactory;`enter code here`
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;



class Usuario extends Model
{
    
    use Notifiable;
    use HasFactory;
    
    protected $primarykey = "id";
    protected $table = "usuarios";
    
    protected $fillable = ['id','nombre','contrasena1','email'];
    
    protected $hidden = ['contrasena1','remeber_token'];


    public $timestamps = false;

}


Controller: 

<?php

namespace App\Http\Controllers;
use App\Usuario;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;

class UsuariosController extends Controller

{
    public function index()
    {
        $usuarios = DB::table('usuarios')
                    -> select('usuarios.*')
                    -> orderBy('id', 'DESC')
                    ->get();
        return view('usuarios')->with('usuarios',$usuarios);
    }

    public function store(Request $request)
    {
        $id = 0;

        $validator= Validator::make($request->all(),[
            'nombre'=>'required|min:3|max:20',
            'email'=>'required|min:3|email',
            'contrasena1'=>'required|min:7|required_with::contrasena2|same:contrasena2',
            'contrasena2'=>'required|min:7'
        ]);
        if($validator->fails()){
            return back()
            ->withInput()
            ->with('ErrorInsert','Por favor rellene todos los campos')
            ->withErrors($validator)
            ;
        }else{
            $usuario = Usuario::create([
                'id' => $request->id,
                'nombre'=> $request->nombre,
                'email' => $request->email,
                'contrasena1'=>Hash::make ($request->contrasena1),
                'contrasena2'=> $request->contrasena2,
            ]);
            return back()->with('UsuarioAgregado','Usuario agregado con éxito');
        }
        
    }

    public function destroy($id){
        $usuario = Usuario::find($id);
        $usuario->delete();
        return back()->with("El ususario se elimino correctamente");
    }

    public function editarUsuario(Request $request){

        $usuario = Usuario::find($request->id);

        $validator= Validator::make($request->all(),[
            'nombre'=>'required|min:3|max:20',
            'email'=>'required|min:3|email',
        ]);
        if($validator->fails()){
            return back()
            ->withInput()
            ->with('ErrorInsert','Por favor rellene todos los campos')
            ->withErrors($validator)
            ;
        }else{
            
            $usuario->nombre = $request->nombre;
            $usuario->email = $request->email;
            $validator2= Validator::make($request->all(),[
                'contrasena1'=>'required|min:7|required_with::contrasena2|same:contrasena2',
                'contrasena2'=>'required|min:7'
            ]);
            if(!$validator2->fails()){
                $usuario->contrasena1 = Hash::make($request->contrasena1);
            }

            $usuario->save();
            return back()->with("El usuario se actualizo correctamente");

        }
    }
}

PLS HELP

  [1]: https://i.stack.imgur.com/MCKnl.png    <-- CODE IMG!

CodePudding user response:

Assuming that the class Usuario is located in the folder App this can be a Laravel 5 project.

the top of your model needs the namespace line

<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Usuario extends Model

CodePudding user response:

maybe you need to use this code on the header of your controller :

use App\Models\Usuario;

that's a common problem. it depends on the structure of your project's directory.

  • Related