Skip to content

Commit 5edffbc

Browse files
committed
Simplify computed property tag and drop support for accessing public properties and calling methods on objects
1 parent bbee9fb commit 5edffbc

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ The addon provides a tag for interacting with Computed Properties. Read more abo
210210
{{ livewire:computed:user:first_name /}}
211211
{{ livewire:computed property="user:first_name" /}}
212212
{{ livewire:computed property="user.first_name" /}}
213-
214-
{{# You can mix and match the notation and even call methods on objects. #}}
215-
{{ livewire:computed property="user:latestArticle().title" /}}
216-
{{ livewire:computed:user.latestArticle:title /}}
213+
214+
{{# You can run modifiers on the result as well. #}}
215+
{{ {livewire:computed:nested:data:users} | pluck('email') | join(', ') /}}
217216
```
218217
219218
### Keying Components

src/Tags/Livewire.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MarcoRieser\Livewire\Tags;
44

5-
use Illuminate\Support\Collection;
65
use Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets;
76
use Livewire\Mechanisms\FrontendAssets\FrontendAssets;
87
use Statamic\Support\Arr;
@@ -62,7 +61,7 @@ public function component(): string
6261
/**
6362
* This will return the value of a computed property.
6463
*
65-
* {{ livewire:computed property="my-component" }}
64+
* {{ livewire:computed:my_computed_property }}
6665
*/
6766
public function computed()
6867
{
@@ -71,42 +70,20 @@ public function computed()
7170
}
7271

7372
$property = Str::replace([':', '.'], ':', $property);
74-
75-
if (! Str::contains($property, ':')) {
76-
return \Livewire\Livewire::current()?->$property;
77-
}
78-
7973
$path = collect(explode(':', $property));
8074
$property = $path->shift();
8175

82-
return $path
83-
->reduce(function ($carry, string $property) {
84-
if ($carry === null) {
85-
return $carry;
86-
}
87-
88-
if (is_array($carry)) {
89-
return Arr::get($carry, $property);
90-
}
76+
$property = collect([$property, Str::camel($property), Str::snake($property)])
77+
->filter(fn (string $property) => method_exists(\Livewire\Livewire::current(), $property))
78+
->first();
9179

92-
if ($carry instanceof Collection) {
93-
return $carry->get($property);
94-
}
95-
96-
if (is_object($carry)) {
97-
$property = Str::before($property, '(');
98-
99-
if (method_exists($carry, $property)) {
100-
return $carry->{$property}();
101-
}
80+
if (! $property) {
81+
return null;
82+
}
10283

103-
if (property_exists($carry, $property)) {
104-
return $carry->{$property};
105-
}
106-
}
84+
// TODO[mr]: support public properties or methods on objects in nested data (12.04.2025 mr)
10785

108-
return null;
109-
}, \Livewire\Livewire::current()->$property);
86+
return Arr::get(\Livewire\Livewire::current()->$property, $path->join(':'));
11087
}
11188

11289
/**

0 commit comments

Comments
 (0)