Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ethump's avatar
Level 1

Passing variables in Livewire

Hi -- been bashing my head on this all afternoon. Finally caved in and writing it up here so hopefully the hive mind can tell me what I'm doing stupid. It's pretty simple I think, but I'm new-ish to Laravel and basically green with Livewire so go easy :) Here goes...

I have a table of items and each item has a group field. Items belong in groups. I aiming for a page that displays all the items in a given group. The whole process is started with a call to a page /showItems/{group}. That route (in web.php) calls a class on a controller. The controller class catches the group with public function showItems (Request $request, Group $group) and then also passes it out of the return view ('showItems',['group',$group']) and into the blade showItems.blade.php. I can see the correct value makes it's way to showItems.blade.php since I can {{$group}} it. The next line calls the livewire component and I want to pass this $group variable into that component.

It looks like this: @livewire ('show-items',['group'=>$group])

In the render() function of the livewire component class, I want to do a database search based on $group. For instance **$items=Items::where('group_id',$group')->get(); and then pass $items into the livewire blade component.

Every which way I try this, I end up with Unable to resolve dependency [Parameter #0 [ $group ]] in class App\Livewire\Items.

I've tried declaring it public, not public, declaring it in render ($group).. nothing works. It's doing my head in. I've googled it, everything I can see other folk getting the same error but the solution if listed doesn't seem to fit my use case. I just want to pass an arbitrary value into a livewire class. What am I doing wrong?

If I hardcode the $group=4; in the livewire render() class and remove other references, the logic works as I'd expect, but that's not what I want!

cheers! e

0 likes
3 replies
tykus's avatar

App\Livewire\Items? Do you have another Livewire component named Items, or did you simply forget to alias (use) the Items model in the ShowItems Component:

<?php

namespace App\Livewire;

use App\Models\Items;
// ...

class ShowItems extends Component
{
    //...
ethump's avatar
Level 1

@tykus thanks - no - i've not declared anything extra 'up there'. I just want to pass a simple variable though - not a collection or anything...?

if $group=4 in the blade, I need a $group=4 in the livewire bit.

ethump's avatar
Level 1

Wellllll I worked it out. Don't know why it wasn't working before since I must've hit the right incantation several times already.... but here we are...

usual @livewire ('listItems',['group'=>$group]) on the 'calling' blade.

declare $group public on the Livewire class

Finally, just reference it $this->group inside the function. It works as I expected it to. Absolutely no clue why it kept failing earlier. Ah well....

Please or to participate in this conversation.