Home > Software design >  Laravel 8 Delete function does not throw error but isn't working
Laravel 8 Delete function does not throw error but isn't working

Time:02-18

When I press the delete button it redirects me successfully but does not delete the query in the database, my code is as follows: Route:

Route::resource('v_users', VUserController::class);

Controller:

  public function destroy(request $request)
     {  
        
        v_users::destroy($request->id);
            session()->flash('delete');   
            return redirect()->route('v_users.index')
                        ->with('success','User deleted successfully');
    }

View.index:

 <form action="{{ route('v_users.destroy',$v_users->id) }}" method="POST">
   
                    <a  href="{{ route('v_users.show',$v_users->id) }}">View QR Code</a>
                    <br>
                    <a  href="{{ route('v_users.edit',$v_users->id) }}">Edit</a>
                    <br>
                    @csrf
                    @method('DELETE')
      
                    <button type="submit" >Delete</button>
                </form>

CodePudding user response:

When injecting a model ID to a route or controller action, you will often query the database to retrieve the model that corresponds to that ID. So, in your route file you must make a declared parameter like Route::get('/users/{v_user}', [UserController::class, 'delete'])

Then, in your destroy method you must add the model that you are injecting

public function destroy(Request $request, v_user $user)
{  

    $user->delete();
    session()->flash('delete');   

    return redirect()
            ->route('v_users.index')
            ->with('success','User deleted successfully');
}

CodePudding user response:

Route

Route::resource('v_users', VUserController::class);

controller

public function destroy(VUsers $v_users)
{  
    VUsers->delete();
    session()->flash('delete');   
    return redirect()->route('v_users.index')
           ->with('success','User deleted successfully');
}

blade

<form action="{{ route('v_users.destroy',$v_users->id) }}" method="POST">
   
    @csrf
    @method('DELETE')

    <button type="submit" >Delete</button>
</form>
  • Related