PHP code example of cloudstudio / laravel-livewire-modal
1. Go to this page and download the library: Download cloudstudio/laravel-livewire-modal library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cloudstudio / laravel-livewire-modal example snippets
<livewire:modal />
namespace App\Livewire;
use Cloudstudio\Modal\LivewireModal;
use Illuminate\View\View;
class CreateUser extends LivewireModal
{
public $name = '';
public $email = '';
protected $rules = [
'name' => ' 'password' => bcrypt('password'),
]);
$this->closeModal();
// Optionally emit events when the modal is closed
$this->dispatch('userCreated', $user->id);
}
public function render(): View
{
return view('livewire.create-user');
}
}
public User $user;
public function mount(User $user)
{
$this->user = $user;
$this->name = $user->name;
$this->email = $user->email;
}
public function update()
{
$this->validate();
$this->user->update([
'name' => $this->name,
'email' => $this->email,
]);
$this->closeModalWithEvents([
'userUpdated', // Event name
'userUpdated' => $this->user->id, // Event with data
UserOverview::class => 'userModified', // Component event
UserOverview::class => ['userModified', [$this->user->id]], // Component event with parameters
]);
}
public static function modalFlyout(): bool
{
return true;
}
public static function modalFlyoutPosition(): string
{
return 'left';
}
public static function closeModalOnEscape(): bool
{
return false;
}
public static function closeModalOnClickAway(): bool
{
return false;
}
public static function closeModalOnEscapeIsForceful(): bool
{
return false;
}
public static function dispatchCloseEvent(): bool
{
return true;
}
public static function destroyOnClose(): bool
{
return true;
}
@script
<script>
$wire.on('closingModalOnEscape', data => {
if ($wire.isDirty && !confirm('{{ __('You have unsaved changes. Are you sure you want to close this dialog?') }}')) {
data.closing = false;
}
});
$wire.on('closingModalOnClickAway', data => {
if ($wire.isDirty && !confirm('{{ __('You have unsaved changes. Are you sure you want to close this dialog?') }}')) {
data.closing = false;
}
});
</script>
@endscript
public function delete()
{
// Delete logic here
// Skip the previous modal and close with events
$this->skipPreviousModal()->closeModalWithEvents([
TeamOverview::class => 'teamDeleted'
]);
// Or skip multiple previous modals
// $this->skipPreviousModals(2)->closeModal();
// Optionally destroy the skipped modals' state
// $this->destroySkippedModals();
}