Heru Handoko
Heru Handoko

Reputation: 89

Laravel passing multiple parameter to controller

I want to delete an image and return back to user page, so i try to pass 2 parameter to my controller (my userid and image_id).

But I get this error

Too few arguments to function App\Http\Controllers\McuFormDocumentController::destroy(), 1 passed and exactly 2 expected

My button code in blade

<button id="delete" class="btn btn-danger btn-sm" data-title="{{ $mcu_form_document['title'] }}"
        style="color: #fff; font-size: 1.2em;"
        href="{{ route('mcu-form-document.destroy', ['id'=>$id ,'img'=>$mcu_form_document['id']]) }}">
    Delete <i class="la la-trash" style="color: #fff; font-size: 1.2em;"></i>
</button>

<form action="{{ route('mcu-form-document.destroy', ['id'=>$id ,'img'=>$mcu_form_document['id']]) }}" method="post" id="deleteForm">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}

    <input type="submit" value="" style="display:none;">
</form>

My controller

public function destroy($id,$img_id)
{
    $mcu_form_document = McuFormDocument::find($img_id);
    $mcu_form_document->delete();

    return redirect()
        ->route('mcu.resume.list', ['id' => $id])
        ->with('success', 'Document has been successfully deleted!');
}

my route

    Route::resource('mcu-resume', 'McuResumeController');

Upvotes: 0

Views: 1511

Answers (2)

Heru Handoko
Heru Handoko

Reputation: 89

my solution is using concatenation when passing the params in href

 href="{{ route('mcu-form-document.destroy', $id .'|'.$mcu_form_document['id']) }}"

and split it with preg_split in my controller

    public function destroy($params)
    {
    $str_arr = preg_split("/\|/", $params);
    $id = $str_arr[0];
    $img_id = $str_arr[1];
    // dd($id . "-" . $img_id);
    }

hope this will help others..thx for all the help

Upvotes: 1

Rabinson
Rabinson

Reputation: 101

Or you can also add a request parameter into your controller which enables you to pass multiple data and get the required value as below:

use Illuminate\Http\Request;

public function destroy(Request $request)
{
   $id = $request->get('id');
   $image_id = $request->get('img');
   $mcu_form_document = McuFormDocument::find($image_id);
   $mcu_form_document->delete();

   return redirect()
    ->route('mcu.resume.list', ['id' => $id])
    ->with('success', 'Document has been successfully deleted!');
 }


 //web.php
 Route::delete('mcu-resume-destroy', 'McuResumeController@destroy');

Upvotes: 0

Related Questions