没有不值得去解决的问题,也没有不值得去学习的技术!

Investigating Why Empty Request Parameters Are Saved as Null in Laravel 6

Print all request parameters on the backend using var_dump($request->all());. Confirm that the value of title has already been converted to null

1. The parameter sent from the frontend is an empty string. See Figure 1.

The parameter sent from the frontend is an empty string
Figure 1
JSON

{
  "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 saves it, the value of title is null.

JSON

{
  "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 on the backend using var_dump($request->all());. Confirm that the value of title has already been converted to null. See Figure 2.

Print all request parameters on the backend using var_dump($request->all());. Confirm that the value of title has already been converted to null
Figure 2

4. Reference: https://learnku.com/docs/laravel/6.x/validation/5144#6633ca . By default, the global middleware stack in the Laravel application’s App\Http\Kernel class includes the ConvertEmptyStringsToNull middleware. This middleware converts empty strings to null.

5. However, hastily removing this middleware raises concerns that other features might be affected, and the scope of the impact would be difficult to assess. Review the implementation of ConvertEmptyStringsToNull.

PHP

    /**
     * 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. I decided to perform a reverse conversion: when returning the response to the frontend, check if the value is null, and if so, convert it back to an empty string. I ultimately chose to use the NULL coalescing operator.

PHP

// is_null($item['title']) ? '' : $item['title'];
$item['title'] ?? '';

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系