Courses

Livewire 3 From Scratch: Practical Course

Edit Modal Window

Summary of this lesson:
- Creating edit modal component
- Managing modal state with boolean property
- Implementing edit form functionality
- Using magic actions for modal toggle

We will do a simple modal for editing a record for the third mini-project. We will use the same Product model as in the previous example.

edit modal


Livewire Component

First, let's start by creating a Livewire component and showing a list of products.

php artisan make:livewire EditModal

app/Livewire/EditModal.php:

use App\Models\Product;
use Illuminate\Contracts\View\View;
 
class EditModal extends Component
{
public function render(): View
{
return view('livewire.edit-modal', [
'products' => Product::all(),
]);
}
}

resouces/views/livewire/edit-modal.blade.php:

<div class="min-w-full align-middle">
<table class="min-w-full border divide-y divide-gray-200">
<thead>
<tr>
<th class="bg-gray-50 px-6 py-3 text-left">
<span class="text-xs font-medium uppercase leading-4 tracking-wider text-gray-500">Name</span>
</th>
<th class="bg-gray-50 px-6 py-3 text-left">
<span class="text-xs font-medium uppercase leading-4 tracking-wider text-gray-500">Price</span>
</th>
<th class="bg-gray-50 px-6 py-3 text-left">
</th>
</tr>
</thead>
 
<tbody class="bg-white divide-y divide-gray-200 divide-solid">
@forelse($products as $product)
<tr class="bg-white">
<td class="px-6 py-4 text-sm leading-5 text-gray-900 whitespace-no-wrap">
{{ $product->name }}
</td>
<td class="px-6 py-4 text-sm leading-5 text-gray-900 whitespace-no-wrap">
{{ $product->price }}
</td>
<td class="px-6 py-4 text-sm leading-5 text-gray-900 whitespace-no-wrap">
<a !href="#" class="mt-4 rounded-md bg-gray-800 px-4 py-2 text-xs font-semibold uppercase tracking-widest text-white hover:bg-gray-700">
Edit
</a>
</td>
</tr>
@empty
<tr>
<td class="px-6 py-2 text-sm leading-5 text-gray-900 whitespace-no-wrap" colspan="3">No products found.</td>
</tr>
@endforelse
</tbody>
</table>
</div>

products table


Showing Modal

To show and hide a modal, we need a boolean public property. Also, we need properties for...

The full lesson is only for Premium Members.
Want to access all 30 lessons of this course? (108 min read)

You also get:

  • 73 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord