php null 空字符串,为什么laravel默认将空字符串转换为null呢

问题1:

官方没有任何文献说明 空字符串 比 null 效率高。

问题2:

数据库设计是矛盾的,既然默认值是空,就没必要设置字段为 not null。如果你非要这样,你可以在 controller 里面使用 array_filter($request->all()) 来过滤掉 null 或者 空字符串 的参数。

最后,Laravel 是在 5.4 开始加入的,同时加入的还有 TrimStrings, 官方的提交记录没有说明是为了什么,个人认为官方是为了标准化数据格式,进行统一规范。

=======分割线=======

继续补充一下:

如果非要按照自己的要求来,也建议不要直接去注释掉官方的中间件引用,以免其他问题,可以定义你自己的 request,在request 处理这个参数的时候,修改成自己想要的值。

php artisan make:request YourRequest

=======继续割========

By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the AppHttpKernel class. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null. This allows you to not have to worry about these normalization concerns in your routes and controllers.

If you would like to disable this behavior, you may remove the two middleware from your application's middleware stack by removing them from the $middleware property of your AppHttpKernel class.

看官方描述的意思 TrimStrings 和 ConvertEmptyStringsToNull 就是为了规范化数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值