@vincent15000 You have {{ $category }} as the very first line of your view, but you don’t pass a variable named category to your view in your controller. You query categories, but then assign them to a variable called $items instead, and pass that to your view.
Aug 14, 2025
5
Level 63
HTMX and blade fragment
Hello,
I'm trying to use HTMX (first time).
I have followed the dedicated HTMX series on Laracast, but it doesn't work.
https://laracasts.com/series/crafting-web-applications-with-htmx
Here what I'm doing.
// view
{{ $category }}
<div
hx-get="/hello"
hx-target="#hello"
>
All categories
</div>
@fragment('hello')
<div id="hello">
@if (isset($items))
@foreach ($items as $item)
<div>{{ $item->name }}</div>
@endforeach
@endif
</div>
@endfragment
// controller
public function hello(Request $request)
{
$items = Category::all();
return view('tables.page', compact('items'))->fragmentIf($request->hasHeader('Hx-Request'), 'hello');
}
And I get this error.
Undefined variable $category
As it's an HX request (checked !), Laravel should only return the fragment view and thanks to hx-target, only the target part of the view should be rerendered (so no need to have the $category variable.
Hmmm ... what am I doing wrong ?
Thanks for your help.
V
Level 80
1 like
Please or to participate in this conversation.