I'm working with Laravel 5.8 and in this project, I wanted to delete some data from a table in the DB, so I coded this at the Blade:
<form action="{{ route('destroyWallet', $wallet->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="fa text-dark mr-2"> Delete </button>
</form>
And here is the route destroyWallet
:
Route::delete('wallets/delete/{wallet}','Wallet\WalletController@destroy')->name('destroyWallet');
And this is the Controller destroy
method at WalletController
:
public function destroy(Request $request, Wallet $wallet)
{
$subscribe = UserWallet::where('wallet_id',$wallet->id)->get();
$subscriptions = UserWalletTransaction::where('wallet_id', $wallet->id)->get();
if($subscribe != NULL){
$subscribe->delete();
}
if($subscriptions != NULL){
$subscriptions->delete();
}
$wallet->delete();
return redirect(url('admin/wallets/index'));
}
But as soon as I run this I get this error message:
Method Illuminate\Database\Eloquent\Collection::delete does not exist.
So what's going wrong here? How can I solve this issue?
You are using get()
method which return collection then trying to call delete()
method on collection
.
Instead of that method call delete method like below
$subscriptions = UserWalletTransaction::where('wallet_id', $wallet->id)->delete();
or
if its one record then
$subscriptions = UserWalletTransaction::where('wallet_id', $wallet->id)->first();
if($subscribe){
$subscribe->delete();
}