I am using Quill as a rich text editor in my Laravel application, and I am storing its output, known as Quill delta, as text in the database. When displaying this stored value for a specific purpose, I want to retrieve the Quill delta as plain text using accessors.
I have attempted to achieve this with the following code within my Laravel model:
protected function description(): Attribute
{
return Attribute::make(
get: fn (string $value) => collect(optional(json_decode($value, true))['ops'])
->map(fn ($item) => is_string($item['insert']) ? $item['insert'] : '')->join(' '),
);
}
Surprisingly, the output of this code is consistently an empty string. However, if I directly place the get function from this accessor into a controller, it retrieves the expected output correctly. Additionally, before the join operation, the code functions correctly and provides an array in the desired format. However, everything breaks after adding the join operation.
I would appreciate any insights into why this issue might be occurring and how I can correctly extract plain text from a Quill delta using Laravel accessors.