Skip to content
Advertisement

Laravel Model not loaded with data when trying to delete

I have one link as below:

    <a href='{{ route('transferDelete', $tran->transfer_id) }}'><i class='far fa-trash-alt'></i></a>

It gets parsed by the following route:

    Route::get('delete/{transfer_id}', [TransferController::class, 'destroy'])->name('transferDelete');

But the destroy method in the TransferController class is empty. On the other hand, the second parameter gets the correct ID (as expected):

    public function destroy(Transfer $transfer, $bla)
    {
         //$transfer behaves like: $transfer = new Transfer;
         //$bla is a real id.
    }

Is there a way to make $transfer be pointing to transfer_id=$bla, without explicitly doing a Transfer::find($bla)?

Advertisement

Answer

You need to match the route parameter name to the name of the typehinted parameter of the Controller method if you want Implicit Route Model Binding to take place:

                       v
Route::get('delete/{transfer}', ...);

                                     v 
public function destroy(Transfer $transfer, $bla)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement