Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

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? (29 h 14 min)

You also get:

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

Already a member? Login here

Comments & Discussion

M
muuucho ✓ Link copied!

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

N
Nerijus ✓ Link copied!

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

CL
Caribation Labs ✓ Link copied!

please explain how @class() works

N
Nerijus ✓ Link copied!
CL
Caribation Labs ✓ Link copied!

this is great: less @ifs

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.