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

ecmrnn's avatar
Level 3

Passing a Livewire property to a Blade File removes attribute

I have the following code on my Livewire Class (ReservationForm.php):

public $room_types;  

public function mount() {  
    $this->room_types = RoomType::withCount(['rooms' => function ($query) {  
        $query->where('status', Room::STATUS_AVAILABLE);  
    }])->get();  
}

Then, in my reservation-form.blade.php (Livewire Component) I passes the '$room_types' property to a regular blade component named 'room-category.blade.php' like this:

 <x-room-category :roomTypes="$room_types" />

The problem I am having is that the 'rooms_count' attribute that should be added by the RoomType::withCount() eloquent method is being removed from the collection when I passed the '$room_types' property in the regular blade component (room-category.blade.php).

How can I fix this? T_T

0 likes
8 replies
vincent15000's avatar

That's strange.

Can you show the controller for the room category component please ?

ecmrnn's avatar
Level 3

@vincent15000 the room category component is simply a blade file that receives the property '$room_types' as a props.

@props(['roomTypes'])

@forelse($roomTypes as $room)
    <span>{{ $room->rooms_count }}</span> // not working :(
@else
	// no room types
@endforelse 
1 like
ecmrnn's avatar
Level 3

@valentin_vranic, my bad.

I did not include the loop on the code I provided above. Yes, I did used a forelse() directive to access each member of the collection.

@forelse ($roomTypes as $room)
    <span>{{ $room->rooms_count }}</span>
@empty
    // empty rooms
@endforelse

Unfortunately, I still cannot use the 'rooms_count' attribute ):

When I dump the result of the query on my Livewire Class

dd($this->room_types);

It returns the following eloquent collections:

...
#attributes: array:12 [▼
    "id" => 1
    "name" => "la terreza"
    // other attributes here
    "created_at" => null
    "updated_at" => null
    "rooms_count" => 1
  ]

But, after I passed the $room_types property to the blade file <x-room-category :roomTypes="$room_types"> the rooms_count attribute is removed.

1 like
ecmrnn's avatar
Level 3

@valentin_vranic, by including the following code inside the blade

<pre>{{ print_r($roomTypes, true) }}</pre>

I do get the same collection but without the 'rooms_count' field.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

I think perhaps when the livewire component re-renders, it repopulates the model but not the count property.

If the data does not need to be manipulated, load the roomtypes with count in the render method and not the mount method

3 likes
ecmrnn's avatar
Level 3

@Snapey this is noted, thank you!

Moving the query from mount to render works.

1 like

Please or to participate in this conversation.