DEBASHIS BAIDYA
DEBASHIS BAIDYA

Reputation: 310

How to get parameter from Resource route with $request or any other method?

Here is my Resource route:

     Route::resource('projects','ProjectsController');

This is the Url I am requesting for edit from a view the project with id

     projects/$project->id/edit/

Now how will I get the parameter

     $project-id 

form the Url into my ProjectsController edit() method with Request $request or other method ?

Upvotes: 1

Views: 254

Answers (3)

Malkhazi Dartsmelidze
Malkhazi Dartsmelidze

Reputation: 4992

You have to have Route Handler In Your Contttroller:

public function edit(Request $request, $id ){
    $project = Project::findOrFail($id)
    ...
    [DO Whatewer You Want]
}

Upvotes: 1

Anthony Aslangul
Anthony Aslangul

Reputation: 3847

You can also take advantage of the implicit model binding:

public function edit( Project $project ){
    //$project is your model instance
}

More on this topic here: https://laravel.com/docs/5.8/routing#implicit-binding

Upvotes: 0

namelivia
namelivia

Reputation: 2735

Try this:

public function edit( $id ){
    $project = Project::find($id)
    [...]
}

Upvotes: 0

Related Questions