Laravel Livewire Change Event Example

By Hardik Savani | February 5, 2021 | Category : Laravel


In this tutorial we will go over the demonstration of laravel livewire change event example. step by step explain laravel livewire wire:change example. i would like to show you laravel livewire dropdown change event. i would like to show you laravel livewire change. Follow bellow tutorial step of laravel livewire change event with arguments.

Here, i will give you very simple example of change event in laravel livewire. we will take city dropdown and on change we will get selected city id. you can easily do change event with laravel 6, laravel 7 and laravel 8 version.

So, let's follow bellow step and you will get bellow layout:

Step 1: Install Laravel

first of all we need to get fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Livewire

now in this step, we will simply install livewire to our laravel application using bellow command:

composer require livewire/livewire

Step 3: Create Component

Now here we will create livewire component using their command. so run bellow command to create changeEvent component.

php artisan make:livewire changeEvent

Now they created fies on both path:

app/Http/Livewire/ChangeEvent.php

resources/views/livewire/change-event.blade.php

Now both file we will update as bellow for our contact us form.

app/Http/Livewire/ChangeEvent.php

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class ChangeEvent extends Component

{

public $cities = [

1 => 'Rajkot',

2 => 'Surat',

3 => 'Vadodara'

];

public $city_id = '';

/**

* Write code on Method

*

* @return response()

*/

public function render()

{

return view('livewire.change-event')->extends('layouts.app');

}

/**

* Write code on Method

*

* @return response()

*/

public function changeEvent($value)

{

$this->city_id = $value;

}

}

resources/views/livewire/change-event.blade.php

<div>

<h1>Laravel Livewire Change Event Example - ItSolutionStuff.com</h1>

<div>

<strong>Select City:</strong>

<select class="form-control" wire:click="changeEvent($event.target.value)">

<option value="">-- Select City --</option>

@foreach($cities as $key => $city)

<option value="{{ $key }}">{{ $city }}</option>

@endforeach

</select>

<p>Selected City ID: {{ $city_id }}</p>

</div>

</div>

Step 4: Create Route

now we will create one route for calling our example, so let's add new route to web.php file as bellow:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Livewire\ChangeEvent;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('change-event', ChangeEvent::class);

Step 5: Create View File

here, we will create blade file for call form route. in this file we will use @livewireStyles, @livewireScripts. so let's add it.

resources/views/layouts/app.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Livewire Example - ItSolutionStuff.com</title>

@livewireStyles

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">

<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

</head>

<body>

<div class="container">

@yield('content')

</div>

</body>

@livewireScripts

</html>

Now you can run using bellow command:

php artisan serve

Open bellow URL:

localhost:8000/change-event

I hope it can help you...



Tags : Laravel Livewire


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: