itsgoingd / clockwork

Clockwork - php dev tools in your browser - server-side component
https://underground.works/clockwork
MIT License
5.69k stars 321 forks source link

JSON error when Clockwork is used w/ Laravel EncryptCookies middleware #632

Closed PatrickSJ closed 1 year ago

PatrickSJ commented 1 year ago

To reproduce:

  1. Install a fresh Laravel 9 or 10 project
  2. Require Clockwork 5.1
  3. Install DB & create a new user
  4. Create a route to any Laravel view and add in {{ Js::from(User::find(1)) }} to the view
  5. Visit the page.

Error will appear stating Error encoding model [App\Models\User] with ID [1] to JSON: Syntax error or Error encoding model [App\Models\User] with ID [1] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded. If the error does appear on the first page load just refresh the page.

Error will go away if Clockwork is disabled in config/clockwork.php.

itsgoingd commented 1 year ago

Hey, this is actually a Laravel bug. I've made an upstream PR with a fix - https://github.com/laravel/framework/pull/47170.

PatrickSJ commented 1 year ago

As a temp fix I've simply added the following to all my files using Js::from as this clears the error.

@php
    // Clears last JSON error to prevent false error.  See:
    // https://github.com/itsgoingd/clockwork/issues/632
    // https://github.com/laravel/framework/pull/47170

    json_decode('{}');
@endphp