skip to Main Content

I am creating a datatable that tracks multiple properties with the #[Url] attribute.

    #[Url]
    public string $search = '';

    #[Url]
    public array $sorts = [];

    #[Url]
    public int $perPage = 15;

When I modify any of these properties they are updated in the URL as expected. The issue is when I send a link to another user.

Let’s use this URL for example: website.com/?search=123&sorts[created_at]=asc&perPage=25

When we hit enter it will set all (3) properties correctly but will remove everything except ?search=123 from the URL. Regardless of everything I have tried search always works and no other property will work with #[Url] attribute.

Any ideas what could be the issue?

2

Answers


  1. When using array in #[URL] you will need to serialize the array because using PHP array directly in the URL will show unexpected behaviour:

    This is how you are supposed to initialize $sorts:

    #[Url]
    public string $sorts = '[]';  // Serialized JSON string
    

    Similarly when setting the value

    $this->sorts = json_encode(['created_at' => 'asc']);
    
    Login or Signup to reply.
  2. It could depend on how the properties are valued.

    Try using #[Url(except: '')] instead of #[Url], it should work.
    Here the documentation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search