0

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');
5
  • check your routes php artisan route:list that resource isn't going to make any route with more than 1 parameter Commented Aug 11, 2020 at 2:39
  • yes, can you show me more what to do? Commented Aug 11, 2020 at 2:43
  • you will have to define a route yourself that takes 2 parameters ... but why do you need your 'user_id' passed? Commented Aug 11, 2020 at 2:46
  • i need user_id to route back to that user edit page Commented Aug 11, 2020 at 2:59
  • you would either need to create this as a nested resource or create your own delete route that takes 2 parameters Commented Aug 11, 2020 at 3:00

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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');

2 Comments

got null for $id
can you try using action('McuFormDocumentController@destroy', [ 'id' => $id, 'img'=>$mcu_form_document['id'] ]) should have worked with route() too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.