In Laravel 6, the requested parameter is an empty string, and the backend is saved as null
1. The parameters requested by the front-end are empty strings. as shown in Figure 1
{
"image": null,
"align": "center",
"title": "Image with text overlay",
"description": "Use overlay text to give your customers insight into your brand. Select imagery and text that relates to your style and story.",
"button": {
"text": "Shop Now",
"url_object": {
"ID": 1,
"children": [],
"key": 3944,
"object": "home",
"object_id": 1,
"title": "Home",
"url": "/"
},
"url": "/"
},
"text_color": "#fff",
"button_text_color": "#000",
"button_background_color": "#fff",
"show_overlay": false,
"opacity": 0
}
2. After the backend is saved, the value of the title is null .
{
"image": null,
"align": "center",
"title": null,
"description": "Use overlay text to give your customers insight into your brand. Select imagery and text that relates to your style and story.",
"button": {
"text": "Shop Now",
"url_object": {
"ID": 1,
"children": [],
"key": 3944,
"object": "home",
"object_id": 1,
"title": "Home",
"url": "/"
},
"url": "/"
},
"text_color": "#fff",
"button_text_color": "#000",
"button_background_color": "#fff",
"show_overlay": false,
"opacity": 0
}
3. Print all request parameters in the backend, var_dump($request->all()); Confirm that the value of the title has been converted to null. as shown in Figure 2
4. Reference:https://learnku.com/docs/laravel/6.x/validation/5144#6633ca. By default, ConvertEmptyStringsTonUll middleware is included in the Laravel app’s global middleware stack app\http\kernel class. This middleware converts an empty string to null.
5. However, if this middleware is rashly deleted, I am worried that other functions will be affected, and the scope of impact is difficult to evaluate. View the implementation of ConvertEmptyStringsTonUll
/**
* Transform the given value.
*
* @param string $key
* @param mixed $value
* @return mixed
*/
protected function transform($key, $value)
{
return is_string($value) && $value === '' ? null : $value;
}
6. Determine the reverse conversion, and when responding to the front end, determine whether it is NULL, and if so, convert it to an empty string. The final decision to use the null merge operator.
// is_null($item['title']) ? '' : $item['title'];
$item['title'] ?? '';

