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

TuffRivers's avatar

Route Model Binding with Resource Routes?

How can i inject the model data into my routes with Route::resource routes? Is it automatically available because the resource creates /route/{id} ? I tried type hinting in my controller but that doesnt seem to work, it returns an empty model.

My Route

Route::group(['middleware' => 'jwt.auth'], function() {

    Route::resources([
    
        'cards' => 'api\Card\CardController'

        ]);
});

My controller

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Card $id)
    {     

	//how to access? everything i try returns blank
	// print_r($id)         
       
   
    }
0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

look at the php artisan route:list

You will see the routes described as /cards/{card}

You must accept the model in your controller as $card and not $id

2 likes
TuffRivers's avatar

I have multiple i just removed them from the code i posted here :P

TuffRivers's avatar

snapey this works when i put Card $card in the controller fuction, but when i put it in the construct of the controller it returns a blank model

protected $card;
    

    public function __construct(Card $card){

        $this->card = $card;
      

    }

Show

   public function show()
    {     

  print_r($this->card);
}

Blank model, any idea?

Snapey's avatar

yes, don't put it in the constructor.

The controllers are constructed before the router knows what you are actually requesting.

2 likes

Please or to participate in this conversation.