There is no problem not worth solving, and no technology not worth learning!

In Laravel 6, the requested parameter is an empty string, and the backend is saved as null

在后端打印所有请求参数,var_dump($request->all());。确认 title 的值已经被转换为 null

1. The parameters requested by the front-end are empty strings. as shown in Figure 1

前端请求的参数为空字符串
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

在后端打印所有请求参数,var_dump($request->all());。确认 title 的值已经被转换为 null
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'] ?? '';


PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.