I have an array property topic on a livewire component that I want to use as query string, the query string looks like this currently example.com/?topic[0]=livewire&topic[1]=laravel,
but I prefer the url to look like this example.com/?topic=livewire,laravel
currently I introduce a seperate property queryStringTopic and convert the topic array to comman separated string
public array topic = [];
#[Url(as: 'topic',except:'')]
public string $queryStringTopic='';
public function updatedTopic()
{
$this->queryStringTopic = implode(',',$this->topic);
}
and convert it back to an array in the mount method and assign it to the topic property.
public function mount()
{
if (! empty($this->queryStringTopic))
{
$this->topic = explode(',', $this->queryStringTopic);
}
}
Is there a better way to achieve this?