Skip to content

Instantly share code, notes, and snippets.

@serkanalgur
Last active June 18, 2021 08:26
Show Gist options
  • Save serkanalgur/19f53dd99a250825d34a1f0018858c81 to your computer and use it in GitHub Desktop.
Save serkanalgur/19f53dd99a250825d34a1f0018858c81 to your computer and use it in GitHub Desktop.
json_extract and json_unquote creator functions for Mysql < 5.7 . If you use translatable texts with Laravel you can use this. (I was forced to use Laravel with cPanel built with MySQL 5.6)
DELIMITER $$
CREATE FUNCTION `json_extract`(`details` TEXT, `required_field` VARCHAR(255)) RETURNS text CHARSET utf8mb4
BEGIN
RETURN TRIM(
BOTH '"' FROM SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
details,
CONCAT(
'"',
SUBSTRING_INDEX(required_field,'$.', - 1),
'"'
),
- 1
),
'",',
1
),
':',
- 1
)
) ;
END$$
DELIMITER ;
DELIMITER $$
CREATE FUNCTION `json_unquote`(`mdata` TEXT CHARSET utf8mb4) RETURNS text CHARSET utf8mb4
BEGIN
RETURN mdata;
END$$
DELIMITER ;
@serkanalgur
Copy link
Author

json_extract and json_unquote creator functions for Mysql < 5.7 . If you use translatable texts with Laravel you can use this. (I was forced to use Laravel with cPanel built with MySQL 5.6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment