Livewire is a full-stack framework for Laravel framework that makes building dynamic interfaces simple, without leaving the comfort of Laravel.
If you are using livewire with Laravel then you don’t worry about writing jquery ajax code, livewire will help to write very simple way jquery ajax code using PHP without page refresh Laravel validation will work, the form will submit etc.
Laravel Livewire release adds the following:
To get started please follow below link :
composer create-project --prefer-dist laravel/laravel idea
php artisan make:migration create_ideas_table
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateIdeasForms extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('ideas', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('text'); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('ideas'); } } php artisan migrate
Now we will create Idea model by using the following command:
php artisan make:model Idea <?php namespace App; use Illuminate\Database\Eloquent\Model; class Idea extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'text', 'description', ]; }
composer require livewire/livewire
<?php namespace App\Http\Livewire; use Livewire\Component; use App\Idea; class IdeaForm extends Component { public $text; public $description; public function submit() { $validatedData = $this->validate([ 'text' => 'required|min:6', 'description' => 'required', ]); Idea::create($validatedData); return redirect()->to('/form'); } public function render() { return view('livewire.idea-form'); } } resources/views/livewire/idea-form.blade.php <form wire:submit.prevent="submit"> <div class="form-group"> <label for="forText">Name</label> <input type="text" class="form-control" id="forText" placeholder="Enter name" wire:model="name"> @error('name') <span class="text-danger">{{ $message }}</span> @enderror </div> <div class="form-group"> <label for="forDescription">Body</label> <textarea class="form-control" id="forDescription" placeholder="Enter Body" wire:model="body"></textarea> @error('body') <span class="text-danger">{{ $message }}</span> @enderror </div> <button type="submit" class="btn btn-primary">Save Idea</button> </form>
routes/web.php Route::get('/form', function () { return view('form'); });
Now we will start the server to see the magic.
php artisan serve
Please open your browser and paste below link in it.
http://localhost:8000/form
As we have implemented an example of Form using Laravel Livewire and seen the magic of it simplifying the complex jquery ajax code using PHP. So, try it and if you still face any difficulties feel free to reach out our Laravel Experts.
Subscribe to our newsletter and learn about the latest digital trends.