Page recipes
The three most common jobs in a panel: adding a new page, writing a list page and editing records in a form modal.
Note
The recipes use Zubazar UI Pro's panel layout (ready in the starter kit). In an existing project, first follow Zubazar UI Pro in an existing project.
A new panel page
The example adds a "Customers" page to the panel; the steps are the same for every new page.
-
Write the view. Panel pages live under
resources/views/admin/:{{-- resources/views/admin/customers/index.blade.php --}} @extends('layouts.admin.vertical') @section('title', __('Customers')) @section('content') <section class="space-y-6"> <zb:page-header :title="__('Customers')" :breadcrumbs="[['label' => __('Records')], ['label' => __('Customers')]]" /> <zb:card> <zb:empty-state :title="__('No customers yet')" :description="__('Get started by adding your first customer.')"> <zb:slot:action><zb:button>{{ __('New customer') }}</zb:button></zb:slot:action> </zb:empty-state> </zb:card> </section> @endsection@section('title')is the tab title;@section('content')goes into the layout's content area. -
Add the route. For a page that shows no data,
Route::viewis enough; when it needs data, write a controller:// routes/admin.php Route::view('/customers', 'admin.customers.index')->name('customers.index');The starter kit loads
routes/admin.phpwith the/adminprefix: the address is/admin/customersand the route name has no prefix (route('customers.index')). -
Add a menu line. Write it into the
menuarray ofresources/menu/main.json:{ "title": "Customers", "icon": "users", "route": "customers.index", "active": "customers.*" }The page shows in the menu and in the ⌘K search; thanks to
activeit stays selected on sub-pages such ascustomers.create.
For a page with a top menu or with no menu, change the layout name in the @extends line: Choosing a layout.
A Livewire page
Write an interactive page, such as a form or a live search, as a Livewire component:
// app/Livewire/Customers/Index.php
namespace App\Livewire\Customers;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Layout('layouts.admin.vertical')]
#[Title('Customers')]
final class Index extends Component
{
public function render(): View
{
return view('admin.customers.index');
}
}
// routes/admin.php
use App\Livewire\Customers\Index as CustomersIndex;
Route::livewire('/customers', CustomersIndex::class)->name('customers.index');
#[Layout] puts the page into the layout and #[Title] is the translation key of the tab title (the layout translates it). A Livewire view has no @extends or @section; it starts with a single root element (<section>…</section>).
A single-file component
In the single-file component that Livewire 4's php artisan make:livewire writes (a .blade.php with the ⚡ prefix), Zubazar UI tags work as they are:
{{-- resources/views/components/⚡cart-line.blade.php --}}
<?php
use Livewire\Component;
new class extends Component
{
public int $quantity = 1;
public function increase(): void
{
$this->quantity++;
}
};
?>
<div>
<zb:input wire:model="quantity" type="number" label="Quantity" />
<zb:button wire:click="increase">One more</zb:button>
</div>
Put it on a page with <livewire:cart-line />.
A page-specific script
Push JavaScript that runs on one page only to the page-scripts stack; the layout prints it at the end of the page:
@push('page-scripts')
@vite('resources/js/pages/customers.js')
@endpush
Add the file to the Vite inputs too: Page-specific code.
Authorization
The menu does not check permissions. Protect the route of a page that needs them with your own middleware or policy; to hide the item from the menu: Authorization.
A list page
Zubazar UI draws the list: the toolbar, search, the table, sortable headings, the selection bar and pagination. The code that fetches, searches and pages the data is yours. A searchable, paginated product list:
// app/Livewire/Products.php
namespace App\Livewire;
use App\Models\Product;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
final class Products extends Component
{
use WithPagination;
#[Url(as: 'q')]
public string $search = '';
public function updatedSearch(): void
{
$this->resetPage();
}
public function render(): View
{
$this->authorize('viewAny', Product::class); // runs on every request
return view('livewire.products', [
'products' => Product::query()
->when($this->search !== '', fn ($query) => $query->where('name', 'like', '%'.$this->search.'%'))
->orderBy('name')
->paginate(25),
]);
}
}
{{-- resources/views/livewire/products.blade.php --}}
<div class="space-y-4">
<zb:list-toolbar>
<zb:search wire:model.live.debounce.300ms="search" placeholder="Product name" />
</zb:list-toolbar>
<zb:table>
<zb:table.columns>
<zb:table.column>Product</zb:table.column>
<zb:table.column align="end">Stock</zb:table.column>
</zb:table.columns>
<zb:table.rows>
@foreach ($products as $product)
<zb:table.row wire:key="product-{{ $product->id }}">
<zb:table.cell>{{ $product->name }}</zb:table.cell>
<zb:table.cell align="end">{{ $product->stock }}</zb:table.cell>
</zb:table.row>
@endforeach
</zb:table.rows>
</zb:table>
{{ $products->links('zubazar-ui::components.ui.pagination') }}
</div>
Put the list on a page with <livewire:products />; for the route see A Livewire page.
The parts
| Part | What it does |
|---|---|
zb:list-toolbar |
The top bar: page size and search on the left, actions on the right (actions slot) |
zb:search |
The search box; bind it to your component with wire:model |
zb:per-page-select |
The page size; the perPage property in the component |
zb:filter-button (PRO) |
Opens the filter panel and shows the number of active filters |
zb:table and its parts |
The table; zb:table.column sortable field="name" is a sortable heading |
zb:sortable-th |
A sortable heading in a hand-written <thead>; calls sortBy when clicked |
zb:selection-bar |
The bulk action bar shown while something is selected |
links('zubazar-ui::components.ui.pagination') |
Zubazar UI's view of Laravel's pagination |
zb:empty-state |
An empty list or a "no results" message |
The component side of sorting, filters and bulk selection: List parts. Cards on narrow screens: Table.
Choosing a data table
The parts are only the look; you choose the code behind them:
- Your own Livewire component: Like the example above. The best choice for large lists filtered on the server.
- Another Livewire table package: Publish the package's views and draw the table with
zb:table, the toolbar withzb:list-toolbarandzb:search, and the pagination with Zubazar UI's view. - DataTables (PRO): Search, sorting and paging happen in the browser; fine up to a few thousand records. Setup: DataTables.
- Another JavaScript table: Write the table as a
<table>with theui-tableclass insidezb:table; the colours and dark mode apply to it.
Authorization
A list must check permissions for every record it shows and changes:
- Seeing the list: Write
viewAnyin the policy and call$this->authorize('viewAny', Product::class)in the component'srender()method.mount()runs only on the first request,render()on every Livewire request. - Actions: In every method, such as delete, find the row again from the query and check it:
$this->authorize('delete', $product). - Values from the address bar: Use the sort field, the filters and the page size only after comparing them with the values you allow.
Warning
return truein the policy opens the list to everyone who is signed in. On Livewire requests only the route'sauthandcan:middleware run again; write any check you do in another middleware into the policy too.
Lists written with zb:table
The list files that php artisan zb:table wrote into your application before 2.2.0 (app/Zubazar/Table/, resources/views/zubazar/table.blade.php) are yours and keep working. The parts and texts they use (zubazar-ui-pro::table.*) stay in the package until 3.0; you can still change the texts with lang/vendor/zubazar-ui-pro/{locale}/table.php. The command was removed, so these files get no new versions.
A form modal
Opens the form that adds and edits a record in a modal; the Previous, Next, Add, Delete, Cancel and Save buttons come with it. The form modal is in Zubazar UI Pro.
// app/Livewire/Customers/CustomerForm.php
namespace App\Livewire\Customers;
use App\Models\Customer;
use Livewire\Attributes\Locked;
use Livewire\Component;
final class CustomerForm extends Component
{
public bool $showForm = false;
public string $name = '';
// The open record's id; null for a new record.
#[Locked]
public ?int $customerId = null;
public function createNew(): void
{
$this->reset('name', 'customerId');
$this->resetValidation();
$this->showForm = true;
}
public function save(): void
{
$customer = Customer::create($this->validate(['name' => ['required', 'string', 'max:120']]));
$this->customerId = $customer->id;
$this->showForm = false;
}
public function render()
{
return view('livewire.customers.customer-form');
}
}
{{-- resources/views/livewire/customers/customer-form.blade.php --}}
<div>
<zb:button wire:click="createNew">New customer</zb:button>
<zb:form-modal
id="customer-form"
title="Customer"
wire:model="showForm"
wire:submit="save"
:record-exists="$customerId !== null"
>
<zb:input wire:model="name" label="Name" required />
</zb:form-modal>
</div>
Buttons that can't be used are not hidden but disabled. Save turns on only when a field changes. When Save's request comes back without validation errors, the record counts as saved; you don't need to send an event. Closing the modal with changes shows a warning.
Warning
Public methods such as
edit()ordelete()can be called from the browser with any id. Check permissions on the server every time you load, save and delete; make the state properties#[Locked].
The open state
wire:model="showForm" binds whether the modal is open to a Livewire property; when the user closes it (X, Cancel, Esc, the backdrop) the property becomes false. Without wire:model the modal only hides in the browser and the component's next request opens it again; to manage it yourself, listen to the ui:form-close-approved event.
Options
By default the buttons call the previous, next, createNew and delete methods. For other names or settings, put your own zb:form-actions bar into the modal's actions slot:
<zb:form-modal id="customer-form" title="Customer" wire:model="showForm" wire:submit="save">
…
<zb:slot:actions>
<zb:form-actions previous="previousCustomer" next="nextCustomer" create="createNew" delete="remove" />
</zb:slot:actions>
</zb:form-modal>
| Prop | What it does | Default |
|---|---|---|
previous, next |
The method of the Previous and Next buttons | previous, next |
create, delete |
The method of the Add and Delete buttons; Delete asks first | createNew, delete |
delete-confirmation |
The text of the delete confirmation | "Are you sure you want to delete this record?" |
delete-dialog |
false: the browser's own box (wire:confirm) asks |
true |
cancel |
The Cancel button; given a method name, it calls that method | true |
sticky |
Keeps the bar at the bottom of the screen | false |
variant |
buttons or pills (coloured pill buttons with icons) |
buttons |
when-dirty |
With changes: disable turns off Previous, Next and Add; confirm keeps them on and asks when clicked |
disable |
dirty |
true for a change the fields don't show (an uploaded file); null: the fields decide |
null |
record-exists, has-previous, has-next, can-delete |
The record's state in a page form; in a modal, the modal's props of the same name are used | null |
save |
An old prop; it no longer does anything | true |
Save always submits the form; wire:submit decides the method it calls.
With when-dirty="confirm" the buttons stay on while there are changes; clicking one asks a question: Cancel returns to the form, Continue discards the changes and runs the action.
Report a change the fields don't show with dirty: <zb:form-modal id="product-form" :dirty="$filesChanged" …>.
The modal's body
The body scrolls on its own and keeps a padding; body-class adds classes to it (body-class="space-y-6"). For a Livewire component with its own form inside, use flush: the body has no padding, the modal's form tag becomes a div and the default bar is not drawn.
<zb:form-modal id="product-form" title="Product" wire:model="showForm" flush>
<livewire:products.product-form />
</zb:form-modal>
The child component's form carries its own scrolling area and its own <zb:form-actions /> bar, and sends its events with the modal's id (form: 'product-form').
The action bar on a page form
zb:form-actions works outside a modal too; inside a form, it binds itself to that form:
<zb:form id="product-form" wire:submit="save" dirty-check>
<zb:input wire:model="name" label="Product name" required />
<zb:form-actions sticky cancel="discard"
:record-exists="$productId !== null"
:has-previous="$previousId !== null"
:has-next="$nextId !== null"
:can-delete="$canDelete">
<p class="text-xs text-muted">{{ $name }}</p>
</zb:form-actions>
</zb:form>
record-exists,has-previous,has-nextandcan-deleteturn the buttons on; they are read again on every render.- On a page, Cancel resets the form to its last saved values;
cancel="discard"calls that Livewire method instead. stickykeeps the bar at the bottom of the screen; a given slot becomes a status line at the start of the bar.dirty-checkturns on the leave-page warning.
The unsaved changes warning
With changes in the form, closing the modal (X, Cancel, Esc, the backdrop), clicking a wire:navigate link or the browser's back and forward buttons open a warning: Stay returns to the form, Discard changes resets the fields and goes on. Reloading the page or closing the tab shows the browser's own warning.
- To turn the warning off in a modal, use
:dirty-check="false"; Save is then always on. - For a form outside a modal, add
dirty-checktozb:form. The warning dialog (zb:form-guard-dialog) is already in Zubazar UI Pro's layouts. - After Save, a
redirect()to another page shows no warning. It does on a form that only useszb:form dirty-checkwithout an action bar; usezb:form-actionson such a form. - To leave a field or a container out of the tracking, add
data-dirty-ignore.
State and events
The kit doesn't load, save or delete records; those are your component's methods. The modal learns the state from the props (mode, record-exists, has-previous, has-next, can-delete) and from the result of the requests. A validation error (also one added with addError('general', …) for a key that is not a field), an HTTP error, a lost connection or a cancelled request counts the record as not saved.
Warning
If
save()catches the error itself and returns, the request comes back without errors and the record counts as saved. Add an error in that case, or send$this->dispatch('ui:form-save-failed', form: 'customer-form').
On a form that saves outside Livewire (your own fetch request, say), report the result with events. Events bubble up; their detail carries the form's id (form) and its state (state: mode, recordExists, hasPrevious, hasNext, canDelete).
| Event | When |
|---|---|
ui:form-mounted |
The form is first recognised |
ui:dirty, ui:clean |
The form changes; it is clean again |
ui:record-loaded, ui:form-reset |
A record is loaded; a new record starts (you can send these too) |
ui:form-mode-changed |
The state changes (mode: 'saving' locks the buttons) |
ui:form-saved, ui:form-save-failed |
Saving succeeds or fails |
ui:form-discard |
The changes are discarded |
ui:form-close-request |
You send it to close the modal from your own code, through the warning |
ui:form-close-approved |
The modal closes |
Details
When are the buttons on? Previous and Next: while a saved record is clean and there is a previous or next record. Add: while a saved record is clean (never before the first record is saved). Delete: while a saved record is clean and deleting is allowed. Save: while there are changes. Cancel: always. While saving, every button is disabled.
How are changes detected? The form compares the current values with the last clean ones; changing a value back makes the form clean again. Every standard field (for file fields the name, size, type and date), rows added later and the combobox, tag input, rating and code input bound with wire:model are tracked.
Your own action bar Without zb:form-actions or zb:form-modal, add data-server-errors="true" to the form on a render with errors:
<form id="customer-form" data-ui-dirty-form wire:submit="save" @if ($errors->any()) data-server-errors="true" @endif>
JavaScript API Get the change manager in your own code with loadDirtyForms():
import { loadDirtyForms } from '@zubazar/ui-pro';
const { initDirtyForms } = await loadDirtyForms();
const manager = initDirtyForms();
manager.hasDirtyForms();
manager.requestNavigation('/target');
Learn more
- List parts and Table: live examples.
- Appearance: the layout files and every field of the menu.
- A working form example: in the demo app, Forms › Form layouts › Form with sticky buttons.