Users Management

The Admin user has access to a users management table page.


The user management can be accessed by clicking User Management from the Laravel Examples section of the sidebar or adding /laravel-users-management in the url. This page is available for users with the Admin role and the user is able to add, edit and delete other users. For adding a new user you can press the + New User button or add in the url /laravel-new-user. If you would like to edit or delete an user you can click on the Action column. It is also possible to sort the fields or to search in the fields.

On the page for adding a new user you will find a form which allows you to fill the information. All pages are generated using blade templates.

In /resources/views/livewire/laravel-examples/laravel-new-user.blade.php

                  
                    <div class="col-12 col-sm-6">
                        <label>{{ __('First Name') }}</label>
                            <div class="@error('first_name')has-danger @enderror">
                                <input wire:model="first_name" class="multisteps-form__input form-control
                                @error('first_name')is-invalid @enderror" type="text" placeholder="eg. Michael" />
                            </div>
                            @error('first_name') <div class="text-danger text-xs">{{ $message }}</div>@enderror
                    </div>
                  
                

The App\Http\Livewire\LaravelExample\LaravelNewUser takes care of data validation and creating the new user:

              
                public function firstStepSubmit() {
                    $validatedData = $this->validate([
                        'first_name' => 'max:20',
                        'last_name' => 'max:20',
                        'role_id' => ['required', Rule::in(collect(DB::table('roles')->pluck('id')))],
                        'company' => 'nullable',
                        'email' => 'required|email|unique:users',
                        'password' => 'required|min:6|same:passwordConfirmation',
                    ]);
                    $this->currentStep = 2;
                }
              
            

Once the user pressed Send at the end of the form the new user is added to the table.

For authorizing this actions have been used policies such as App\Policies\UserPolicy:

              
                /**
                * Determine whether the authenticate user can manage other users.
                *
                * @param  \App\User  $user
                * @return boolean
                */
               public function manageUsers(User $user)
               {
                   return $user->isAdmin();
               }