如何在laravel上区分请求为空字符串和null [英] how to differentiate request empty string and null on laravel

查看:285
本文介绍了如何在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆