LaraSell
5 months ago
512
8
General

Check Users in roles before remove

Posted 5 months ago by LaraSell

I have this function for remove role (spatie package):

public function destroy(Request $request){

    $item_id = $request->get('item_id');
    $item = Role::find($item_id);
    if(empty($item)) {
        return response()->json([
            'success' => false,
            'message' => 'Item not found!',
        ], 404);
    } else {
        $item->delete();

        return response()->json([
            'success' => true,
            'message' => 'Item successfully deleted.',
        ], 200);
    }

}

this code work for me But i need to check role(users in role) before remove it. my mean is remove only role if There is no any user in that role.

Please sign in or create an account to participate in this conversation.