'cannot use my model on my controller " Undefined type 'App\Usuario' " Laravel
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.
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.
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Usuario extends Model
{
use Notifiable;
use HasFactory;
// ...
}
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;
// Validation ...
$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);
// Validation ...
$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");
}
}
Sorry if my English is not good. [1]: https://i.stack.imgur.com/MCKnl.png
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ali Safaei |
