Home > Mobile >  Laravel check which button clicked
Laravel check which button clicked

Time:11-28

This is a roles&permissions table, I can change user's role so far, but delete user.

How can I sperate change role and delete actions ?

<form action="edit-role-permission/{{ $user->id }}" method="POST">
    @csrf

    <select name="roles">
    <option name ="user" value="user">User</option>
    <option name= "staff" value="staff">Staff</option>
    </select>
  
    <button type="submit">Change role</button>
    <button type="submit">Delete</button>
</form>

Route::post('edit-role-permission/{user}', [AdminController::class, 'editRolePermission']);
//class AdminController extends Controller

    function editRolePermission(Request $request, User $user)
    {

        $user->update(["role" => $request->roles]);
        $user->save();

        return redirect()->back()->with("message", "User role updated successfully");
    }

CodePudding user response:

Add the name of the buttons name="action" and values:

<button type="submit" name="action" value="update">Change role</button>
<button type="submit" name="action" value="delete">Delete</button>

and check it with:

if ($request->input('action') == "update") {
    // update here
} else if ($request->input('action') == "delete") {
    // delete here
}
  • Related