Tutorial: Searchable Customer List
Build a list page: search as you type, show a match count, delete rows, and keep expanded rows expanded when the list refreshes.
The finished result has one Linker, one controller method and two Blade files. No JavaScript.
1. Plan: who does what
| Need | Handled by |
|---|---|
| Search keyword, match count | A Linker method returns data → data-state-text |
| The list HTML | renderBlade (Linker returns render.fragment) |
| Expanding a row | Client state (data-state-toggle) |
| Delete | A Linker method + data-linker-confirm-danger |
2. Page controller
The ordinary route still handles the first render; the same controller also serves fragments:
namespace App\Http\Controllers;
use App\Models\Customer;
use App\Traits\CanRenderCallSafely;
use Illuminate\Contracts\View\View;
class CustomerController extends Controller
{
use CanRenderCallSafely;
public function index(): View
{
return view('pages.customer.index', [
'customers' => Customer::query()->latest()->paginate(20),
]);
}
/** Entry point for renderBlade. params are untrusted input. */
public function renderCallSafely(array $params = [], string $method = 'components'): View
{
$keyword = mb_substr((string) ($params['keyword'] ?? ''), 0, 64);
return view('partials.customer.results', [
'customers' => Customer::query()
->when($keyword !== '', fn ($q) => $q->where('name', 'like', "%{$keyword}%"))
->latest()
->paginate(20),
]);
}
}3. The Linker component
php artisan make:linker Customernamespace App\Http\LinkerComponents;
use App\Attributes\Linker\Authorize;
use App\Attributes\Linker\Computable;
use App\Attributes\Linker\RequiresAuth;
use App\Attributes\Linker\Validate;
use App\Http\Controllers\CustomerController;
use App\Models\Customer;
use App\Support\Linker\LinkerResponse;
#[RequiresAuth]
class CustomerLinker extends LinkerComponent
{
public static string $alias = 'customer';
#[Computable]
#[Validate(['keyword' => 'nullable|string|max:64'])]
public function search(array $args): LinkerResponse
{
$keyword = $args['keyword'] ?? '';
$this->set('keyword', $keyword);
return $this->ok(['matchCount' => $this->query($keyword)->count()])
->call('render.fragment', [
'target' => '#customer-list-results',
'name' => CustomerController::class,
'params' => ['keyword' => $keyword],
]);
}
#[Computable]
#[Validate(['id' => 'required|integer'])]
#[Authorize('delete', Customer::class, arg: 'id')]
public function delete(array $args): LinkerResponse
{
Customer::query()->findOrFail($args['id'])->delete();
$keyword = $this->get('keyword', '');
return $this->ok(['matchCount' => $this->query($keyword)->count()])
->call('ui.toast', ['level' => 'success', 'message' => 'Deleted'])
->then('render.fragment', [
'target' => '#customer-list-results',
'name' => CustomerController::class,
'params' => ['keyword' => $keyword],
]);
}
protected function query(string $keyword)
{
return Customer::query()->when($keyword !== '', fn ($q) => $q->where('name', 'like', "%{$keyword}%"));
}
}Notes:
- The
idfordeleteis user-editable input, so#[Authorize]hands it to the policy. - After deleting, the list re-renders with the
keywordthe server remembered; the browser doesn't need to send it again. - If the policy has no
deleteability,Gate::allowsreturns false: denied by default.
4. The page template
resources/views/pages/customer/index.blade.php:
<div class="customer-page"
@linker('customer')
@renderObject('customerPanel', ['render', 'state:read', 'state:write'])>
<input type="search" name="keyword" placeholder="Search customers"
data-state-bind="keyword"
data-linker-call="search"
data-linker-on="input"
data-linker-debounce="400"
data-linker-args="keyword">
<p><span data-state-text="matchCount">{{ $customers->total() }}</span> matches</p>
<div id="customer-list-results">
@include('partials.customer.results')
</div>
</div>@linkeris on the outer element, which is never re-rendered (otherwise each render would create a new instance).- The list container
#customer-list-resultsis the inner part that gets replaced. data-state-bind="keyword": the search text survives re-renders.
5. The list fragment
resources/views/partials/customer/results.blade.php:
<ul>
@forelse ($customers as $c)
<li>
<button type="button" data-state-toggle="open{{ $c->id }}">{{ $c->name }}</button>
<div data-state-show="open{{ $c->id }}">
<p>{{ $c->email }}</p>
<button type="button"
data-linker-call="delete"
data-linker-args='{"id": {{ $c->id }}}'
data-linker-confirm="Deleting {{ $c->name }} can't be undone"
data-linker-confirm-danger>Delete</button>
</div>
</li>
@empty
<li>No matching customers</li>
@endforelse
</ul>Bindings are re-scanned after the fragment is inserted, so the data-linker-call and data-state-* inside it work automatically: they find the outer @linker and @renderObject.
6. Verify
Open the page and DevTools:
- Type → the Network panel shows a single
linkerrequest (400 ms debounce), followed by arenderrequest. - Expand a row → search again → if that row is still in the results, it's still expanded.
- Delete → the confirmation needs two clicks → toast → list and count update.
- Call
deletewith curl on another user's customer id →policy.
document.addEventListener('cg:state', e => console.log(e.detail.key, '→', e.detail.value));7. Going further: dom.repeat
When every row looks the same, you can send data instead of HTML. Replace render.fragment with:
->call('dom.repeat', ['ref' => 'customers', 'items' => $this->query($keyword)->paginate(20)->toArray()])The template switches to a template row; see Tables: Hidden Templates & Repeats. The trade-off: fewer bytes, but without JavaScript only the empty-state row shows.