Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Linc's avatar
Level 1

Find id from API created in Laravel, with Postman

Hi guys!

I have a little problem: I have created an API from my DB with Laravel; i used this API in my Vue.js with AXIOS and it'all ok (with stamp and with console.log).

I have "readed" my API with postman, and i tryed to select a single value with id (in this case number 1), but it doesn't work. Like this method:

http://127.0.0.1:8000/api/players/1

With other API it's works, but in this case, it doesn't return the id value.

0 likes
8 replies
frankielee's avatar

Perhaps you can show us the codes at api.php and PlayersController.php

Linc's avatar
Level 1

Yup @frankielee , sorry:

ApiAuth.php

<?php

namespace App\Http\Middleware;

use Closure;

class ApiAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $tokenHeader = $request->header('Authorization');


        if(empty($tokenHeader)) {
            return response()->json(
                [
                    'success' => false,
                    'error' => 'authorization assente'
                ]
            );
        }

        $apiKey = 'Bearer ' . config('app.api_key');

        if($tokenHeader != $apiKey) {
            return response()->json(
                [
                    'success' => false,
                    'error' => 'authorization errata'
                ]
            );
        }
        return $next($request);
    }
}


And this is my PlayersController.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Player;

class PlayerController extends Controller
{
       public function getAll()
    {
        $players = Player::all();

        return response()->json($players);

    }

}

And this is my Api.route:

Route::middleware('api.auth')->any('/players', 'Api\PlayerController@getAll');
frankielee's avatar
Level 29

Try this, for more reference =>https://laravel.com/docs/7.x/routing#parameters-optional-parameters

Api.route

Route::middleware('api.auth')->any('/players/{player?}', 'Api\PlayerController@getAll');

PlayersControllers

  public function getAll(Player $player)
    {
	if($player){
	return response()->json($player);
}	
        $players = Player::all();

        return response()->json($players);

    }

1 like
nathalie's avatar

Hello! Sorry, I'm not familiar with Vue.js or AXIOS so maybe I'm lost here. But shouldn't you specify a route and controller method to show a single player?

Linc's avatar
Level 1

@frankielee it's works! Now i will read the link at the reference.

It's possible to delete or add another value inside my db with this method in postman?

(I will try to delete ora add a value with Vue.js and axios, but it return me "success= false". )

frankielee's avatar

hey @linc, maybe we can open a new discussion so that you can provide more information on that.

1 like

Please or to participate in this conversation.