-
app/Http/Livewire/CreateUserComponent.php
Open in GitHubuse App\Mail\InvitationMail; use App\Models\Role; use App\Models\User; use App\Providers\AppServiceProvider; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Mail; use Illuminate\Validation\Rule; use Livewire\Component; class CreateUserComponent extends Component { use HasLivewireAuth; public $user; public $roles; public function render() { $this->roles = Role::orderBy('name')->get(); return view('users.create') ->extends('layouts.app'); } public function store() { $this->validate(); $user = User::create([ 'email' => $this->user['email'], 'role_id' => $this->user['role_id'], AppServiceProvider::OWNER_FIELD => auth()->id(), ]); msg_success('User has been successfully created.'); Mail::to($user) ->queue(new InvitationMail($user, Carbon::tomorrow())); return redirect()->route('users.index'); } protected function rules() { return [ 'user.email' => [ 'required', 'email', Rule::unique('users', 'email'), ], 'user.role_id' => [ 'required', Rule::exists('roles', 'id'), ], ]; } }
-
resources/views/users/create.blade.php
Open in GitHub<div> @section('title') Create New User @endsection @section('content-header') <x-content-header> Create New User </x-content-header> @endsection <x-savings.content> <x-slot name="card_header"> <h3 class="card-title">Create New User</h3> <a href="{{ route('users.index') }}" class="float-right">Back</a> </x-slot> <x-slot name="card_body"> <form method="POST" wire:submit.prevent="store"> @csrf <x-inputs.email key="user.email" required="required" placeholder="{{ trans('validation.attributes.email') }}" autofocus /> <x-inputs.dropdown key="user.role_id" :options="$roles" textField="name" required="required" /> <div class="row"> <div class="offset-8 col-4"> <x-inputs.button text="Save" class="btn-success" /> </div> </div> </form> </x-slot> </x-savings.content>