skip to Main Content

I want to use method destroy to delete one or more users, so it suppose to recieve an array and then delete whatever values given:

    public function destroy(Request $request)
    {
            // DB::table('users')->whereIn('id', $request->user)->delete();
            
            dd(var_dump($request->user));
    }

However, the method recieves it as string, which is the first id passed, and does not recieve other values, how can i make it recieve the array as is ? thanks in advance

const deleteSelectedUsers = () => {
    const ids = [];
    selectedUsers.value.forEach((val) => {
        ids.push(val.id);
    });
    router.delete(route("users.destroy", ids));
    deleteUsers.value = false;
    selectedUsers.value = null;
    toast.add({
        severity: "success",
        summary: "Successful",
        detail: "Users Deleted",
        life: 3000,
    });
};

2

Answers


  1. Please check this answer if it helps you

         deleteSelectedUsers() {
            const ids = this.selectedUsers.map(user => user.id);
    
            // Send a DELETE request to the Laravel backend using Inertia
            this.$inertia.delete(route('users.destroy'), { ids: ids })
                .then(() => {
                    // Handle successful response
                    this.deleteUsers = false;
                    this.selectedUsers = null;
                    this.$toast.success('Users deleted successfully', { duration: 3000 });
                })
                .catch(error => {
                    // Handle error
                    console.error(error);
                });
        },
    

    and your controller function is this

     public function destroy(Request $request){
        $ids = $request->input('ids');
    
        // Perform your delete logic here using $ids
        // For example:
        User::whereIn('id', $ids)->delete();
    
        return redirect()->back()->with('success', 'Users deleted successfully');
    }
    
    Login or Signup to reply.
  2. In accordance with best practices, a typical Laravel Controller encompasses CRUD methods that handle individual model operations. However, if your requirements deviate from the standard CRUD operations, it is advisable to relocate the corresponding code to a new Controller tailored to your needs.

    For instance, consider a UserController containing methods like index, show, edit, update, and destroy, each serving specific functionalities. However, if the task involves deleting multiple users, it might be more fitting to segregate this code into a distinct controller and organize it accordingly.

    My recommendation is to use an Invokable Controller, following these steps:

    1. Create Route:

    Route::delete('/users/delete-many', DeleteManyUsersController::class)->name('users.delete-many');
    

    2. Develop the Invokable Controller:

    <?php
    
    namespace AppHttpControllers;
    
    class DeleteManyUsersController extends Controller
    {
        public function __invoke(Request $request)
        {
            $ids = $request->input('ids');
    
            // Validate user IDs if necessary
    
            User::whereIn('id', $ids)->delete();
    
            return redirect()->back()->with('success', 'Users deleted successfully');
        }
    }
    ?>
    

    3. Execute the API call using InertiaJs:

    async deleteSelectedUsers() {
        try {
            const ids = this.selectedUsers.map(user => user.id);
    
            // Send a DELETE request to the Laravel backend using Inertia
            await this.$inertia.delete(route('users.delete-many'), { ids });
    
            // Handle a successful response
            this.deleteUsers = false;
            this.selectedUsers = null;
            this.$toast.success('Users deleted successfully', { duration: 3000 });
        } catch (error) {
            // Handle any errors
            console.error(error);
        }
    }
    

    By employing this approach, your code adheres to best practices and maintains a cleaner, more modular structure, ensuring seamless management of user-related operations.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search