Modifying roles for user management in Laravel 8x with Livewire

Viewed 1143

What I am trying to do is add the adding user's section from the Team Settings page into a CRUD which I made, what I have been able to do is add the ability to edit the data but not pass over the role management side (see image here:)

This is what I am wanting to have on CRUD section

I have a form which I made to add new Users, is there some code which I can fully copy paste already existing inside the Laravel JetStream solution to achieve adding users. See image:

What I have

Once I have the above sorted I will be able to work out the edit myself :)

Code I need to snip:

@if (Gate::check('addTeamMember', $team))

<!-- Add Team Member -->
<div class="mt-10 sm:mt-0">
    <x-jet-form-section submit="addTeamMember">
        <x-slot name="title">
            {{ __('Add Team Member') }}
        </x-slot>

        <x-slot name="description">
            {{ __('Add a new team member to your team, allowing them to collaborate with you.') }}
        </x-slot>

        <x-slot name="form">
            <div class="col-span-6">
                <div class="max-w-xl text-sm text-gray-600">
                    {{ __('Please provide the email address of the person you would like to add to this team. The email address must be associated with an existing account.') }}
                </div>
            </div>

            <!-- Member Email -->
            <div class="col-span-6 sm:col-span-4">
                <x-jet-label for="email" value="{{ __('Email') }}" />
                <x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="addTeamMemberForm.email" />
                <x-jet-input-error for="email" class="mt-2" />
            </div>

            <!-- Role -->
            @if (count($this->roles) > 0)
                <div class="col-span-6 lg:col-span-4">
                    <x-jet-label for="role" value="{{ __('Role') }}" />
                    <x-jet-input-error for="role" class="mt-2" />

                    <div class="mt-1 border border-gray-200 rounded-lg cursor-pointer">
                        @foreach ($this->roles as $index => $role)
                                <div class="px-4 py-3 {{ $index > 0 ? 'border-t border-gray-200' : '' }}"
                                                wire:click="$set('addTeamMemberForm.role', '{{ $role->key }}')">
                                    <div class="{{ isset($addTeamMemberForm['role']) && $addTeamMemberForm['role'] !== $role->key ? 'opacity-50' : '' }}">
                                        <!-- Role Name -->
                                        <div class="flex items-center">
                                            <div class="text-sm text-gray-600 {{ $addTeamMemberForm['role'] == $role->key ? 'font-semibold' : '' }}">
                                                {{ $role->name }}
                                            </div>

                                            @if ($addTeamMemberForm['role'] == $role->key)
                                                <svg class="ml-2 h-5 w-5 text-green-400" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" stroke="currentColor" viewBox="0 0 24 24"><path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                                            @endif
                                        </div>

                                        <!-- Role Description -->
                                        <div class="mt-2 text-xs text-gray-600">
                                            {{ $role->description }}
                                        </div>
                                    </div>
                                </div>
                        @endforeach
                    </div>
                </div>
            @endif
        </x-slot>

        <x-slot name="actions">
            <x-jet-action-message class="mr-3" on="saved">
                {{ __('Added.') }}
            </x-jet-action-message>

            <x-jet-button>
                {{ __('Add') }}
            </x-jet-button>
        </x-slot>
    </x-jet-form-section>
</div>

@endif

1 Answers

First you have to publish the Jetstream views, if you haven't done so already:

php artisan vendor:publish --tag=jetstream-views

In your existing blade file (the one you are using for user management), you should be able to include that exact view using something like:

@livewire('teams.team-member-manager')

Do note: this is a Livewire component. If you are using the Vue version, you'll need to check the documentation on how to include that instead.

And as I mentioned in a comment above, you may need to modify app/Policies/TeamPolicy::addTeamMember to suit your needs. If you'd rather not change the default policies, you may also be able to intercept a gate check, but that's beyond my level of expertise at this late hour and left as an exercise for the reader.

HTH

Related