英文:
Problem when accessing some routes from my LARAVEL 8 api
问题
Here is the translated code you provided:
UsuarioController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Usuario;
class UsuarioController extends Controller
{
public function index()
{
$usuarios = Usuario::all();
return response()->json($usuarios);
}
public function show($id)
{
$Usuario = Usuario::find($id);
if (!$Usuario) {
return response()->json(['error' => 'Usuario not found'], 404);
}
return response()->json($Usuario);
}
public function store(Request $request)
{
$usuario = new Usuario();
$usuario->nombre = $request->input('nombre');
$usuario->apellidos = $request->input('apellidos');
$usuario->ciudad = $request->input('ciudad');
$usuario->save();
return response()->json($usuario, 201);
}
public function update(Request $request, $id)
{
$usuario = Usuario::find($id);
if (!$usuario) {
return response()->json(['error' => 'Usuario not found'], 404);
}
$usuario->nombre = $request->input('nombre');
$usuario->apellidos = $request->input('apellidos');
$usuario->ciudad = $request->input('ciudad');
$usuario->save();
return response()->json($usuario);
}
public function destroy($id)
{
$Usuario = Usuario::find($id);
if (!$Usuario) {
return response()->json(['error' => 'Usuario not found'], 404);
}
$Usuario->delete();
return response()->json(['message' => 'Usuario deleted']);
}
}
Routes/Api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\Usuario;
use App\Http\Controllers\UsuarioController;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/usuarios', [UsuarioController::class, 'index']);
Route::post('/usuarios', [UsuarioController::class, 'store']);
Route::get('/usuarios/{id}', [UsuarioController::class, 'show']);
Route::put('/usuarios/{id}', [UsuarioController::class, 'update']);
Route::delete('/usuarios/{id}', [UsuarioController::class, 'destroy']);
Models/Usuario.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Usuario extends Model
{
use HasFactory;
protected $fillable = ['nombre', 'apellidos', 'ciudad'];
protected $table = 'usuarios';
}
Regarding your issue with the 404 error when accessing "http://localhost:8000/api/usuarios," it could be due to various reasons. Ensure that your Laravel application is running and that the URL is correct. Additionally, make sure you have data in your "usuarios" table and that your routes are properly set up in Laravel's route configuration.
英文:
I have created an api with Laravel 8, very simple, just to do some tests, it is a table with users where I store their name, last name and city.
Well my problem is to obtain all the users that are in the database and when making a POST and add a new one, i can make a GET to see a specific user, I can also update users and delete them, so I do not understand why I can not add them and see all that there is.
Here is the code
UsuarioController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Usuario;
class UsuarioController extends Controller
{
public function index()
{
$usuarios = Usuario::all();
return response()->json($usuarios);
}
public function show($id)
{
$Usuario = Usuario::find($id);
if (!$Usuario) {
return response()->json(['error' => 'Usuario not found'], 404);
}
return response()->json($Usuario);
}
public function store(Request $request)
{
$usuario = new Usuario();
$usuario->nombre = $request->input('nombre');
$usuario->apellidos = $request->input('apellidos');
$usuario->ciudad = $request->input('ciudad');
$usuario->save();
return response()->json($usuario, 201);
}
public function update(Request $request, $id)
{
$usuario = Usuario::find($id);
if (!$usuario) {
return response()->json(['error' => 'Usuario not found'], 404);
}
$usuario->nombre = $request->input('nombre');
$usuario->apellidos = $request->input('apellidos');
$usuario->ciudad = $request->input('ciudad');
$usuario->save();
return response()->json($usuario);
}
public function destroy($id)
{
$Usuario = Usuario::find($id);
if (!$Usuario) {
return response()->json(['error' => 'Usuario not found'], 404);
}
$Usuario->delete();
return response()->json(['message' => 'Usuario deleted']);
}
}
Routes/Api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\Usuario;
use App\Http\Controllers\UsuarioController;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/usuarios', [UsuarioController::class, 'index']);
Route::post('/usuarios', [UsuarioController::class, 'store']);
Route::get('/usuarios/{id}', [UsuarioController::class, 'show']);
Route::put('/usuarios/{id}', [UsuarioController::class, 'update']);
Route::delete('/usuarios/{id}', [UsuarioController::class, 'destroy']);
And Models/Usuario.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Usuario extends Model
{
use HasFactory;
protected $fillable = ['nombre', 'apellidos', 'ciudad'];
protected $table = 'usuarios';
}
I use this url "http://localhost:8000/api/usuarios" and i get 404 not found, but i dont understand why if others request like put works
答案1
得分: 1
我认为您在Postman中的URL之后添加了多余的空格/换行符,请将其修整。
英文:
I think you're adding an extra space/line right after URL in postman, please trim it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论