uma uma - 27 days ago 15
PHP Question

how to pass multiple parameters in DELETE method in laravel 5?

I am doing project using

laravel 5
framework. I want to pass two
parameter
through
DELETE
method , for my
Controller
class.

ex.blade.php


<td align='center'>
{!! Form::open(['method' => 'DELETE', 'route'=>['commitee-page-member.destroy',$member->id ,$commitee->id]]) !!}
<button type="submit" class="btn btn-default btn-sm" onclick="return confirm('Are you sure?')"> <span class="glyphicon glyphicon-trash"></span> </button>
{!! Form::close() !!}
</td>


when i click button i can see
variable
through
url
:

URL:

../masterlaw.com/commitee-page-member/1?5


I try to different ways , take these
two parameters
1 and 5.

controller class code :

public function destroy(Request $request, $id)
{
//
echo $id;
echo $request['id'];
}


but still I couldn't
retrieve
data . please ,help me.

Answer

The problem here is that destroy() method of resource class only accepts one parameter, the ID. One possible solution is create a controller class (not a resource) and create a method accepting two arguments:

class PageController extends Contoller{

    public function destroy(Request $request, $memberId, $commiteeId){
        // echo memberId;
    }
}

Name a route fitting your needs and Form's format:

Route::get('delete-page/{memberId}/{commiteeId}', [
    'as' => 'destroy', 
    'uses' => 'PageController@destroy'
]);

Now your route is prepared to be used with two parameters:

{!! Form::open([
    'method' => 'POST', 
    'route' => ['destroy', $member->id, $commitee->id]]) !!}

As an additional note, you can read more about Nested resources, it could be useful for you if your project is made under an specific structure.

Edit. If you consider necessary keep code at resource class, you can add your custom method and declare it at routes.php file before resource declaration:

// using "delete" as a verb
Route::delete('memberPage/customDestroy/{memberId}/{commiteeId}', 'MyResource@customDestroy');
Route::resource('memberPage', 'MyResource');