Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 261
uma Avatar asked Sep 16 '15 04:09

uma


3 Answers

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');
like image 55
manix Avatar answered Oct 18 '22 18:10

manix


Your code was almost ok, just have to make few edit

./masterlaw.com/commitee-page-member/1?val=5&&val2=4

and in the controller

public function destroy(Request $request, $id)
{      
 echo $id;
 echo $request->val;
 echo $request->val2;     
}

when you are going to add new get parameters inside the resource controller's function

you just have to add Request $request parameter in the function and then you can catch all the values. like for edit function

manage/pages/1/edit?language=en

public function edit(Request $request,$id){
 $getvalue=$request->language;
}
like image 1
MD ABDULLAH AL KAFI Avatar answered Oct 18 '22 20:10

MD ABDULLAH AL KAFI


you must define all parameters in your method of your controller but you are free to use all of them or not

public function destroy(Request $request, $commiteId , $mebmerId)
{
    //your awesome codes     
}

and you must pass them with key

note: Form::open is not available in laravel 5+ so you must use the route helper function

route("myroute.name",["commiteeId"=>commiteeId,"memberId"=>$memberId])
like image 1
Mahdi mehrabi Avatar answered Oct 18 '22 18:10

Mahdi mehrabi