Reputation: 496
I have a user_transaction
table and I am trying to delete a record in the table but I am not successful.
UserTransaction.php
protected $table = 'user_transaction';
blade
<form action="{{ route('transactions.destroy', $transaction->id) }}" method="post" >
@method('delete')
@csrf
<button type="submit" class="btn btn-default p-0">
<i class="ft-trash-2 text-grey font-medium-5 font-weight-normal"></i>
</button>
</form>
web.php
Route::resource('transactions', 'Admin\TransactionController');
TransactionController.php
public function destroy(UserTransaction $userTransaction)
{
dd($userTransaction->id);
$userTransaction->delete();
return redirect()->route('transactions.index');
}
This code show null
Upvotes: 1
Views: 791
Reputation: 10210
The likely cause of this is a naming mismatch between your resource route parameter and the name of the variable used in your resource controller for route model binding.
By default resource routes use the singlar of the resource you provide in the route definition as the route parameter. So in your example:
Route::resource('transactions', 'Admin\TransactionController');
The above will produce some routes like the following:
GET|HEAD | /transactions/{transaction}
DELETE | /transactions/{transaction}
GET|HEAD | /transactions/{transaction}/edit
For route model binding to work, the route parameter name and the name of the variable used in your controller need to match.
So what you need to do is change the name of the variable used in your controller:
public function destroy(UserTransaction $transaction)
{
dd($transaction->id);
$transaction->delete();
return redirect()->route('transactions.index');
}
Upvotes: 3