Problem when accessing some routes from my LARAVEL 8 api

huangapple go评论57阅读模式
英文:

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

&lt;?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()-&gt;json($usuarios);
}
public function show($id)
{
$Usuario = Usuario::find($id);
if (!$Usuario) {
return response()-&gt;json([&#39;error&#39; =&gt; &#39;Usuario not found&#39;], 404);
}
return response()-&gt;json($Usuario);
}
public function store(Request $request)
{
$usuario = new Usuario();
$usuario-&gt;nombre = $request-&gt;input(&#39;nombre&#39;);
$usuario-&gt;apellidos = $request-&gt;input(&#39;apellidos&#39;);
$usuario-&gt;ciudad = $request-&gt;input(&#39;ciudad&#39;);
$usuario-&gt;save();
return response()-&gt;json($usuario, 201);
}
public function update(Request $request, $id)
{
$usuario = Usuario::find($id);
if (!$usuario) {
return response()-&gt;json([&#39;error&#39; =&gt; &#39;Usuario not found&#39;], 404);
}
$usuario-&gt;nombre = $request-&gt;input(&#39;nombre&#39;);
$usuario-&gt;apellidos = $request-&gt;input(&#39;apellidos&#39;);
$usuario-&gt;ciudad = $request-&gt;input(&#39;ciudad&#39;);
$usuario-&gt;save();
return response()-&gt;json($usuario);
}
public function destroy($id)
{
$Usuario = Usuario::find($id);
if (!$Usuario) {
return response()-&gt;json([&#39;error&#39; =&gt; &#39;Usuario not found&#39;], 404);
}
$Usuario-&gt;delete();
return response()-&gt;json([&#39;message&#39; =&gt; &#39;Usuario deleted&#39;]);
}
}

Routes/Api.php

&lt;?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\Usuario;
use App\Http\Controllers\UsuarioController;
Route::middleware(&#39;auth:sanctum&#39;)-&gt;get(&#39;/user&#39;, function (Request $request) {
return $request-&gt;user();
});
Route::get(&#39;/usuarios&#39;, [UsuarioController::class, &#39;index&#39;]);
Route::post(&#39;/usuarios&#39;, [UsuarioController::class, &#39;store&#39;]);
Route::get(&#39;/usuarios/{id}&#39;, [UsuarioController::class, &#39;show&#39;]);
Route::put(&#39;/usuarios/{id}&#39;, [UsuarioController::class, &#39;update&#39;]);
Route::delete(&#39;/usuarios/{id}&#39;, [UsuarioController::class, &#39;destroy&#39;]);

And Models/Usuario.php

&lt;?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Usuario extends Model
{
use HasFactory;
protected $fillable = [&#39;nombre&#39;, &#39;apellidos&#39;, &#39;ciudad&#39;];
protected $table =  &#39;usuarios&#39;;
}

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
Problem when accessing some routes from my LARAVEL 8 api

答案1

得分: 1

我认为您在Postman中的URL之后添加了多余的空格/换行符,请将其修整。

英文:

I think you're adding an extra space/line right after URL in postman, please trim it

huangapple
  • 本文由 发表于 2023年5月11日 19:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定