Livewire Development Tutorials, Guides & Insights
Unlock 5+ expert-curated livewire tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your livewire skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Livewire Cheat Sheet: PHP & JavaScript Tips
Livewire.emitTo('componentName', 'eventName', optionalData);- Listen for Livewire events in JavaScript:
Managing WYSIWYG Editors with Livewire: A Step-by-Step Guide
document.addEventListener('livewire:load', function() {
Livewire.on('elementAdded', (data) => {
$('.custom-editor').summernote({
height: 200,
callbacks: {
onPaste: function(contents, $editable) {
var pastedText = contents;
var wireId = $(this).data('wire-id');
if (wireId) {
Livewire.emit('updateContent', wireId, pastedText);
}
return false;
},
onChange: function(contents, $editable) {
var typedText = contents;
var wireId = $(this).data('wire-id');
if (wireId) {
Livewire.emit('updateContent', wireId, typedText);
}
}
}
});
});
});This script sets up the Summernote editor on elements with the .custom-editor class. It includes two key callbacks:
Implementing Real-Time Search with Livewire in Laravel
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Post;
class SearchPosts extends Component
{
public $query = '';
public $posts = [];
public function updatedQuery()
{
$this->posts = Post::where('title', 'like', '%' . $this->query . '%')->get();
}
public function render()
{
return view('livewire.search-posts');
}
}Next, create the Blade view for this component in resources/views/livewire/search-posts.blade.php.
Dynamically Updating Form Fields with Livewire in Laravel
Open the newly created DynamicForm.php file and set up the properties and methods to manage the form fields:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class DynamicForm extends Component
{
public $selectedCategory = null;
public $subcategories = [];
public function updatedSelectedCategory($category)
{
$this->subcategories = $this->getSubcategories($category);
}
public function getSubcategories($category)
{
// This is where you'd fetch subcategories from the database or an API based on the category
$subcategoriesData = [
'technology' => ['Web Development', 'Mobile Development', 'AI & ML'],
'business' => ['Marketing', 'Finance', 'Entrepreneurship'],
'design' => ['Graphic Design', 'UI/UX Design', '3D Modeling']
];
return $subcategoriesData[$category] ?? [];
}
public function render()
{
return view('livewire.dynamic-form', [
'categories' => ['technology', 'business', 'design']
]);
}
}Managing Modals with Livewire and JavaScript
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class ModalManager extends Component
{
public function openUserProfileModal()
{
$this->dispatchBrowserEvent('open-modal', ['modalId' => 'user-profile-modal']);
}
public function openSettingsModal()
{
$this->dispatchBrowserEvent('open-modal', ['modalId' => 'settings-modal']);
}
public function openNotificationsModal()
{
$this->dispatchBrowserEvent('open-modal', ['modalId' => 'notifications-modal']);
}
public function render()
{
return view('livewire.modal-manager');
}
}Modify your Livewire component's Blade view to call the methods defined in the component class.