I’ll guide you through creating a counter component in Livewire step by step. This is a great first project to understand Livewire’s basics.
Step 1: Install Livewire (if not already installed)
Run this command in your terminal:
composer require livewire/livewire
Step 2: Create a new Livewire component
Run this command to generate your counter component:
php artisan make:livewire counter
This will create two files:
-
app/Http/Livewire/Counter.php(the PHP component class) -
resources/views/livewire/counter.blade.php(the view file)
Step 3: Set up the component class
Open app/Http/Livewire/Counter.php and modify it:
and the welcome.blade.view is:
<html>
<head>
<title>Livewire Counter</title>
@livewireStyles
</head>
<body>
<h1>Welcome to Livewire Counter</h1>
@livewire('counter')
@livewireScripts
</body>
</html>
Step 6: Test your component
Start your Laravel development server:
