如何在laravel上区分请求为空字符串和null

编程入门 行业动态 更新时间:2024-10-27 18:21:16
本文介绍了如何在laravel上区分请求为空字符串和null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在php中我们可以做到

in php we can do

isset($_POST['name'])

以便可以检测到用户提交空文本.

so that user submit empty text can be detected.

但在laravel中

$request->has('name')

空字符串也返回null.

return null on empty string too.

我需要找到一种方法来判断它是一个空字符串还是该字段根本不发布

I need to find a way to tell if it is an empty string or the field not posted at all

推荐答案

您可以在请求实例中使用exist()方法.如果要检查请求检查中是否有值:

You can use exists() method in request instance. If you want to check if there is value in request check:

$request->has('key');

如果要检查密钥是否存在,请使用:

if you want to check if the key is exists use:

$request->exists('key');

更多推荐

如何在laravel上区分请求为空字符串和null

本文发布于:2023-06-02 14:47:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/447340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   为空   如何在   null   laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!