Search code examples
jsonlaraveldoublequotes

to import double quotes with laravel json


current record;

["ss,bb,nn"]

want to save as follows;

["ss","bb","nn"]

my model;

protected $casts = [
    'options' => 'array',
];

my controller;

$cpoll->options = $request->options;

Solution

  • You can use both implode and explode to get result. Assign it to variable and do processing then import

    1. remove braces from the array

       $myString =["ss,bb,nn"];
       $string = implode('', $myString);
      
    2. explode it using comma(,)

       $myArray = explode(',', $string );
      

    So finally code will be

                    $myString = ["ss,bb,nn"];
                    $string  = implode('', $myString);
                    $myArray = explode(',', $string);
                    return $myArray;