Ability to use computed properties for updateQueryString

When using an array (even it it’s just a plain array with zero-based keys) the query will update to something like

https://example.com?list[0]=hello&list[1]=world

It would be really nice to have that be

https://example.com?list=hello,world

We could have Livewire handle this specific case by checking the keys (a bit too much IMO), or we could get the user to have a very simple computed property that will convert this array into a comma-separated string. The problem at the moment is that you can’t use computed properties to update the query string.

Implementing this might be a bit complicated since the query string updating is handled by the Javascript side of Livewire, so we’d have to send the value of the computed property along with the response. Any ideas?

I don’t think you need to touch javascript for this, and there’s a tool that exists already that you can take advantage of. First, if you have the computed properties already, you’re almost there. They’re just quality of life shortcuts. You would just have to handle them like regular functions.

Take this example based on yours:

public function getListProperty()
{
    return ['hello', 'world'];
}

It’s essentially some behind the scenes magic that is a shortcut for doing:

public $list;

public function mount()
{
    $this->list = getlist()
}

public function hydrate()
{
    $this->list = getlist()
}

public function getList()
{
    return ['hello', 'world'];
}

So basically you just need your two functions, one that turns an array into a string with commas, and one that explodes the string into an array. Then run your string-to-array at the beginning of the request (mount() and hydrate()), and the array-to-string at the end (render()).

That’s where custom casters come into play and gives you the built in tool to handle the two transformations behind the scenes and give you some cleaner code.