src/DBCast.php
/**
     * Generate a raw DB query to cast an array or json string to a JSON field.
     *
     * @param array|string $json
     * @throws \Exception
     * @return \Illuminate\Database\Query\Expression
     */
Generate a raw DB query to cast an array or json string to a JSON field.
Generate a raw DB query to cast an array or json string to a JSON field .
public static function toJson($json)
    {
        if (is_array($json)) {
            $json = addslashes(json_encode($json));
        } elseif (is_null($json) || is_null(json_decode($json))) {
            throw new \Exception('A valid JSON string was not provided.');
        }
        return \DB::raw("CAST('{$json}' AS JSON)");
    }
public static function toJson ( $ json ) { if ( is_array ( $ json ) ) { $ json = addslashes ( json_encode ( $ json ) ) ; } elseif ( is_null ( $ json ) || is_null ( json_decode ( $ json ) ) ) { throw new \ Exception ( 'A valid JSON string was not provided.' ) ; } return \ DB :: raw ( " CAST(' { $ json } ' AS JSON) " ) ; }
Generate a raw DB query to cast an array or json string to a JSON field.
Generate a raw DB query to cast an array or json string to a JSON field .