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

pllaguno's avatar

Can't make API work

I'm trying to make a call to an API but for some reason i am getting an empty response.

inside my api.php i've got.

Route::group(['middleware' => 'auth:api'], function(){
    Route::apiResource('/KPI','PowerBiController')->parameters(['KPI' => 'ID'])->only(['show']);
});

My controller is the following, the resource is just default.

 public function show(PowerBi $powerBi) : PowerBiResource
    {
       
        return new PowerBiResource($powerBi);
    }

And my model is the following:

class PowerBi extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'PowerBiTable';
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'ID';
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
     /**
     * The connection name for the model.
     *
     * @var string
     */
    protected $connection = 'sqlsrv';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
}

if i do die dump on my controller i get a definition of the model, but no info back. my test call is /api/KPI/1.

if i do php artisan tinker

and i App\PowerBi::where('Date','<','2019-07-17')->first(); i do get the first response.

0 likes
6 replies
Snapey's avatar

so, something wrong with route model binding

Have you tried php artisan route:list to check how the resource parameter is being accepted?

pllaguno's avatar

@snapey yes, i do get the route, when i do the call api/KPI/1 i get back:

{
    "data": []
}

so i am getting a response but as if no data was retrieved.

if i do

public function show(PowerBi $powerBi) : PowerBiResource
    {
       dD($powerBi);
        return new PowerBiResource($powerBi);
    }

i get back

PowerBi {#1370
  #table: "PowerBiTable"
  #primaryKey: "ID"
  +timestamps: false
  #connection: "sqlsrv"
  #fillable: []
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
}
Snapey's avatar

Yes because route model binding is providing you with a new (empty) instance of the PowerBI model because it cannot match the parameter you are passing in the API with your model.

Please follow my suggestion and look what php artisan route:list shows

Snapey's avatar

What does your migration look like?

What do you get in tinker with

>>> App\PowerBi::find(1)
Snapey's avatar
Snapey
Best Answer
Level 122

Change this line

public function show(PowerBi $ID) : PowerBiResource
{
       dd($ID);
pllaguno's avatar
public function show(PowerBi $ID) : PowerBiResource  
    {
       
        return new PowerBiResource($ID);
    }

made the trick, thanks!

Please or to participate in this conversation.