Skip to main content

Edit Modal Window

Premium
5 min read

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 of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

muuucho avatar

Could you show how to make this work also as a "create modal"? It would be very apreciated and doubtless valuable for many projects.

Nerijus avatar

It's the same thing. But you can check this post Livewire 3 CRUD with Form Objects and Modal Wire Elements

Caribation Labs avatar

please explain how @class() works

Caribation Labs avatar

this is great: less @ifs