Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working on json casting to array #14

Open
webparadoxer opened this issue May 23, 2020 · 4 comments
Open

Not working on json casting to array #14

webparadoxer opened this issue May 23, 2020 · 4 comments

Comments

@webparadoxer
Copy link

Schema::create('customers', function (Blueprint $table) { $table->id(); $table->json('data'); $table->timestamps(); });

` protected $casts = [
'data' => 'array',
];

protected $encryptable = [
    'data'
];`

Customer::firstOrCreate(['data' => ['name' => 'me', 'email' => '[email protected]'] ]);

App\Customer {#3958 data: ""eyJpdiI6Iko3aG5OdmhEb1k0TFcyK0ZDSUFRL3c9PSIsInZhbHVlIjoiSzZlaEJEeUlMTEc5OEVveUN6SXJseUJ0OTV3dWNiNTBpa3Vxd1pwTzlQYW9FbUpXdGtWZkZmWmN6YzI4b1pVeC9lRE9BN2V5MTBpSUhyVUw5YkRMOHc9PSIsIm1hYyI6ImM1ZGQzMDNkYjJhNzUyNTNkZGQzODlkYWY3YTYzYTg4ODYwMGYwN2E4NWY2MTcyNWI0YWZlNzRkN2M3ODlmM2EifQ=="", updated_at: "2020-05-23 17:21:38", created_at: "2020-05-23 17:21:38", id: 1, }
Customer::first()->data['email']
PHP Warning: json_decode() expects parameter 1 to be string, array given in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 820
PHP Notice: Trying to access array offset on value of type null in Psy Shell code on line 1

@juan-montilla
Copy link

I know it might be too late but...
Are you still having this issue? I was not able to reproduce (using laravel 7.19.1 and php 7.2)

@phillmorgan28
Copy link

phillmorgan28 commented Oct 17, 2020

I am also having this issue with a casted array on Laravel 8. When I take the encrypted string out of the database and pass it through Crypt::decrypt() the result is a PHP array. It looks like when the field is being stored the array is encrypted BEFORE it is passed through json_encode (or not passed through json_encode at all)

(Laravel 8 on PHP 7.4)

@phillmorgan28
Copy link

To fix this, you just need to add the following just above line 67 (at time of writing) of the Trait so that the PHP array is cast to JSON before storing. Tested successfully.

if ($this->isJsonCastable($key) && ! is_null($value)) { $value = $this->castAttributeAsJson($key, $value); }

@Oxicode
Copy link

Oxicode commented Oct 4, 2021

merged? or anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants