0

i have index page with a button to remove a column from a table.

@foreach ($suppliers as $supplier)
        <tr>
              <th>{{ $supplier -> idSupplier }}</th>
              <th style="color:blue;"><a href="/suppliers/{{$supplier->idSupplier}}">{{ $supplier -> column1 }}</a></th>
              <th>{{ $supplier -> column2 }}</th>
              <th>{{ $supplier -> column3  }}</th>
              <th>{!! $supplier -> column4 !!}</th>
              <th>
                  <a class="btn btn-warning" href="/suppliers/{{$supplier->idSupplier}}/edit" role="button">
                        <i class="fa fa-tools"></i>
                  Edit</a>
                  <a class="btn btn-danger" href="{{ action('SuppliersController@destroy') }}" role="button">
                        <i class="fa fa-eraser"></i>
                  Delete</a>
              </th>
        </tr>
@endforeach

but now everytime i open my index page it gives me this error message

Facade\Ignition\Exceptions\ViewException Missing required parameters for [Route: suppliers.destroy] [URI: suppliers/{supplier}]. (View: C:\xampp\htdocs\Invent\resources\views\suppliers\index.blade.php)

this is my route

Route::resource('suppliers', 'SuppliersController');

and this is destroy function from SuppliersController

public function destroy($idSupplier)
    {
        $supplier = Supplier::find($idSupplier);
        $supplier->delete();
        return redirect('/suppliers')->with('success', 'Supplier removed');
    }

I already try this solution and it gives me another error message.

Aditya
  • 1,075
  • 1
  • 14
  • 24

3 Answers3

2

well you are not passing the required parameter for the controller's action. destroy method receives a parameter idSupplier to perform its actions. from blade you are just calling the controller action without passing the parameter. make it like below:

<a class="btn btn-danger" href="{{ action('SuppliersController@destroy', ['idSupplier' => $supplier->idSupplier]) }}" role="button">
    <i class="fa fa-eraser"></i>
    Delete
</a>

but it won't work. your registered route method is DELETE but it will redirect to a GET method. So use the follwing method for deleting.

<form action="{{ route('suppliers.destroy', $supplier->idSupplier) }}" method="POST">
    @csrf
    @method('DELETE')
    <button class="btn btn-danger btn-sm" title="Delete">Delete</button>
</form>
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
  • your answer does remove the error, but the delete button still not working when i press it, it does nothing. – Aditya Oct 11 '19 at 19:52
  • Facing the same error with Laravel 6. Has anyone been able to resolve the issue, please? – Sree Apr 04 '20 at 11:57
  • @Sree have you tried the solution?? what's not working for you?? – zahid hasan emon Apr 04 '20 at 16:55
  • Thanks for your response, Zahid. I face the same issue with Laravel 6.3; Everything works for me until the forward to the edit.blade.php. There laravel throws this error. I have created a new thread for the issue with details: https://stackoverflow.com/questions/61028370/missing-required-parameters-for-route-projects-edit-uri-projects-project/61028739#61028739 – Sree Apr 04 '20 at 17:31
1

I would suggest some improvements using a confirmation modal. I would then submit a form to the controller.

The following exemple consider you use Bootstrap and laravelCollective. (Code had been simplified)

1. The Delete Button

<a href="#" data-toggle="modal" data-target="#modal1">Delete</a>

2. The modal

<div class="modal" id="modal1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-footer">
                <button type="button" data-dismiss="modal">Cancel</button> 
                <button type="button">Delete</button>
            </div>
        </div>
    </div>
</div>

3. The form

{{ Form::open(['method' => 'DELETE', 'route' => array('suppliers.destroy', $supplier->id) ]) }}
  {{ Form::hidden('id', $supplier->id) }}
  <button type="submit">Delete</button>
{{ Form::close() }}

Note how the supplier ID ($idSupplier) is passed from the form to the controller.

Lucien Dubois
  • 1,590
  • 5
  • 28
  • 53
0

you are calling de destroy method without parameters you can doit like this

 @foreach ($suppliers as $supplier)
    <tr>
          <th>{{ $supplier -> idSupplier }}</th>
          <th style="color:blue;"><a href="/suppliers/{{$supplier->idSupplier}}">{{ $supplier -> column1 }}</a></th>
          <th>{{ $supplier -> column2 }}</th>
          <th>{{ $supplier -> column3  }}</th>
          <th>{!! $supplier -> column4 !!}</th>
          <th>
              <a class="btn btn-warning" href="/suppliers/{{$supplier->idSupplier}}/edit" role="button">
                    <i class="fa fa-tools"></i>
              Edit</a>
              <a class="btn btn-danger" href="{{ route('suppliers.destroy',$supplier->idSupplier ) }}" role="button">
                    <i class="fa fa-eraser"></i>
              Delete</a>
          </th>
    </tr>
 @endforeach
Alex Guerrero
  • 229
  • 2
  • 8