Skip to main content

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

Read more here

New Product Form: Full-Page Component

Premium
6 min read

In this lesson, let's create a second Livewire component for creating a product. We can call this component in two ways.

The first one is the same way as we did with the Products Livewire component making a controller that returns a View and calls the Livewire component. The second one is making the Livewire component a full-page and mapping it directly in the routes file.

This lesson will use the second method, the Livewire component, as a full page.


Creating Livewire Component

So first, we need to create a component.

php artisan make:livewire ProductsCreate

Now we can create a route and map it to this component and add a link to this route.

routes/web.php:

Route::get('products/create', \App\Livewire\ProductsCreate::class)->name('products.create');

resources/livewire/products.blade.php:

<div class="space-y-6">
<div class="flex justify-between">
<div class="space-x-8">
<input wire:model.live="searchQuery" type="search" id="search" placeholder="Search...">
 
<select wire:model.live="searchCategory" name="category">
<option value="0">-- CHOOSE CATEGORY --</option>
@foreach($categories as $id => $category)
<option value="{{ $id }}">{{ $category }}</option>
@endforeach
</select>
</div>
<a wire:navigate href="{{ route('products.create') }}" class="inline-flex items-center px-4 py-2 bg-gray-800 rounded-md font-semibold text-xs text-white uppercase tracking-widest">
Add new product
</a>
</div>
// ...
</div>

add new product button

Now let's add a form and make it work by binding properties and making a method 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!

ProductsCreate.php should inherit: use Livewire\Attributes\Rule;

N
Nerijus ✓ Link copied!

Thanks

CC
Caleb Chen ✓ Link copied!

I guess there might be a mistake. The file name below routes/web.php showed: resources/livewire/products-create.blade.php, which cantains 'Add new product' button. I guess this file should be: resources/livewire/products.blade.php

J
Joe ✓ Link copied!

There are two code blocks with the heading resources/livewire/products-create.blade.php: but the first one should actually be resources/livewire/products.blade.php:

N
Nerijus ✓ Link copied!

Thanks.

FF
Francisco Ferreira Roque Júnior ✓ Link copied!

If someone got this error "SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value".

you just need to add the "category_id" to your Product Model filable array;

protected $fillable = [ 'name', 'description', 'category_id', ];

C
Cristian ✓ Link copied!

For those that have the exception below:

Livewire page component layout view not found: [components.layouts.app]

You need to create the the directory config/livewire.php with this code below:

return [ // 'layout' => 'layouts.app' ];
N
Nerijus ✓ Link copied!

Config files should be published not created manually

N
Nerijus ✓ Link copied!
C
Cristian ✓ Link copied!

You're right, thanks, I ran the command to publish php artisan livewire:publish --config.

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.