skip to Main Content

I am using Laravel to create records using APIin api.php than using the form, but I am having difficulties with the methods, for example the store() method is not inserting the record I want in Postman when I enter the URL http://localhost:8000/api/store of type POST. My database is enable and Apache Laravel is also enable.

The error is this:

POST http://localhost:8000/api/store: {
  "Error": "connect ECONNREFUSED 127.0.0.1:8000",
  "Request Headers": {
    "description": "A+",
    "user-agent": "PostmanRuntime/7.35.0",
    "accept": "*/*",
    "cache-control": "no-cache",
    "postman-token": "7adc02d9-f53e-4aa0-8552-04fe9fdff882",
    "host": "localhost:8000",
    "accept-encoding": "gzip, deflate, br",
    "connection": "keep-alive"
  },
  "Request Body": {}
}
  • api.php

    <?php
    
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesRoute;
    use AppHttpControllersTypeBloodController;
    
    Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
        return $request->user();
    });
    
    Route::apiResources([
        'blood_type' => TypeBloodController::class,
    ]);

  • TypeBloodController.php
<?php

namespace AppHttpControllers;

use AppHttpRequestsTypeBloodRequest;
use AppModelsTypeBloodModel;
use IlluminateHttpRequest;

class TypeBloodController extends Controller
{
    public function index()
    {
        $type_blood = TypeBloodModel::all();
        return response()->json($type_blood, 200);
    }

    public function store(TypeBloodRequest $request)
    {
        $validations = $request->validated();

        $treated_datas = [
            'description' => trim((string) $validations['description']),
        ];

        $type_blood = TypeBloodModel::create($treated_datas);

        return response()->json($tipo_hunter, 201);
    }

    public function show($id)
    {
        $type_blood = TypeBloodModel::find($id);

        if (!$type_blood) {
            return response()->json(['message' => 'Resource not found'], 404);
        }

        return response()->json($type_blood, 200);
    }

    public function update(TypeBloodRequest $request, $id)
    {
        $validations = $request->validated();

        $treated_datas = [
            'description' => trim((string) $validations['description']),
        ];

        $type_blood = TypeBloodModelModel::where('id', $id)->update($treated_datas);

        return response()->json($tipo_hunter, 201);
    }

    public function destroy($id)
    {
        TypeBloodModel::where('id', $id)->delete();
        return response()->json(null, 204);
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    The way that gave right to me was this:

    <?php
    
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesRoute;
    use AppHttpControllersTypeBloodController;
    
    Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
        return $request->user();
    });
    
    Route::get('/type-blood', [TypeBloodController::class, 'index']); // GET: localhost:8000/api/type-blood
    Route::post('/type-blood', [TypeBloodController::class, 'store']); // POST: localhost:8000/api/type-blood
    Route::get('/type-blood/{id}', [TypeBloodController::class, 'show']); // GET: localhost:8000/api/type-blood/{id}
    Route::patch('/type-blood/{id}', [TypeBloodController::class, 'update']); // PATCH: localhost:8000/api/type-blood/{id}
    Route::delete('/type-blood/{id}', [TypeBloodController::class, 'destroy']); // DELETE: localhost:8000/api/type-blood/{id}
    
    

  2. You understand that store is just the name of the function that handles POST on the endpoint /api/blood_type right? You typically do not append the names of handler/controller functions to the end of the route.

    try a POST to /api/blood_type

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search