Однако, когда я отправляю форму, я получаю сообщение «Попытка». чтобы прочитать свойство «имя» в исключении строки, но оно даже не доходит до метода проверки. Мне трудно понять, что происходит за кулисами Livewire. Он пытается немедленно повторно отобразить модальное окно с отправленными данными?
Вот модальное окно:
Код: Выделить всё
@slot('body')
@foreach ($locations as $location)
{{ $location->id }} - {{ $location->name }}
@endforeach
@foreach ($roles as $role)
{{ $role->name }}
@endforeach
@endslot
Код: Выделить всё
class Create extends Component
{
public User $user;
public $roles;
public $locations;
protected $rules = [
'user.first_name' => ['required', 'string', 'max:150'],
'user.last_name' => ['required', 'string', 'max:150'],
'user.email' => ['required', 'string', 'email', 'max:255', 'unique:App\Models\User,email'],
'user.phone' => 'required|string|min:12|max:12|regex:/^\d{3}\-\d{3}\-\d{4}$/|unique:App\Models\User,phone',
'user.role_id' => 'required|exists:roles,id',
'locations.*.id' => 'required|exists:locations,id'
];
protected $messages = [
'user.role_id.required' => 'The role field is required.'
];
public function mount(User $user)
{
$this->user = $user;
$this->role_id = '';
$this->roles = Auth::user()->assignable_roles;
$this->locations = Auth::user()->locations;
}
public function render()
{
return view('livewire.users.create');
}
public function create()
{
$validatedData = $this->validate();
try {
DB::beginTransaction();
$newUserData = array_merge($validatedData, ['password' => Hash::make('YGWwxjsxrmsgRJpDrVRd')]);
$user = User::create($newUserData);
$locations = Arr::flatten($newUserData['locations']);
$user->locations()->sync($locations);
DB::commit();
} catch (Exception $e) {
DB::rollback();
throw $e;
}
$this->dispatchBrowserEvent('hideModal');
$this->emit('refreshUsers');
$this->resetExcept('roles', 'locations');
}
}
Код: Выделить всё
[h4]Users[/h4]
@if (Auth::user()->isInternal())
[i][/i]Add User
@endif
@slot('head')
ID
First Name
Last Name
Email
Role
Status
@endslot
@slot('body')
@forelse($users as $user)
{{ $user->id }}
{{ $user->first_name }}
{{ $user->last_name }}
{{ $user->email }}
{{ $user->role->name }}
@empty
@endforelse
@endslot
@slot('paginationLinks')
{{ $users->links() }}
@endslot
Код: Выделить всё
class UserIndex extends Component
{
use WithPagination;
public $search;
public $roles;
public $user;
protected $paginationTheme = 'bootstrap';
protected $listeners = ['refreshUsers' => '$refresh'];
public function mount()
{
$this->search = '';
}
public function render()
{
return view('livewire.users.index', [
'users' => User::withTrashed()->search(['first_name', 'last_name'], $this->search)->paginate(5)
]);
}
public function show($id)
{
return redirect()->route('users.edit', ['user' => $id]);
}
}
[img]https://i. sstatic.net/c1mgk.png[/img]
Подробнее здесь: https://stackoverflow.com/questions/718 ... -on-string