Home > Mobile >  How do I pass a value in my Route to the Controller to be used in the View in Laravel?
How do I pass a value in my Route to the Controller to be used in the View in Laravel?

Time:01-14

I have 2 entities called Match and Roster. My Match routes are like this

http://localhost:8888/app/public/matches (index) http://localhost:8888/app/public/matches/14 (show)

In order to view/create the teams for each specific match I added the routes for the match roster like this:

Route::get('/matches/'.'{id}'.'/roster/', [App\Http\Controllers\RosterController::class, 'index']);

Now I need that {id} i have in my URL to pass it to the Controller here:

public function index()
    {
        return view('roster.index');
    }

I need that for a couple of things. First I need to do a search on the Roster table filtering by a column with that value, so I can display only the players that belong to that match.

Second, I need to pass it on to the view so I can use it on my store and update forms. I want to add or remove players from the roster from that same index view.

How can I do that?

CodePudding user response:

How are you going to pass the id to the route? do you have a view form or create form?

CodePudding user response:

You can get the route parameter via request()->route().

public function index()
{
    // get {id} from the route (/matches/{id}/roaster)
    $id = request()->route('id');
}
  •  Tags:  
  • Related