检查Laravel中的请求数组是否为空 [英] Checking if a request array is empty in Laravel

查看:67
本文介绍了检查Laravel中的请求数组是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的表格,该表格为我提供了一系列输入.但是,该数组可能为空,则foreach将失败.

I have a dynamically generated form that gives me an array of inputs. However the array might be empty, then the foreach will fail.

    public function myfunction(Request $request)
    {
    if(isset($request))
     {
       #do something
     }

    }

这显然是行不通的,因为它是一个$ request对象,并且始终处于设置状态.但是我不知道如何检查是否有任何输入.

This obviously doesn't work since it is a $request object and is always set. I have no idea however how to check if there is any input at all.

有什么想法吗?

推荐答案

在安装时,我总是通过在App\Http\Controllers目录中的Controller中添加一个函数来完成此操作.

I always do this with my installations by adding a function to the Controller in the App\Http\Controllers directory.

use Illuminate\Http\Request;
public function hasInput(Request $request)
{
    if($request->has('_token')) {
        return count($request->all()) > 1;
    } else {
        return count($request->all()) > 0;
    }
}

不言自明,如果_token以外的其他输入变量,则返回true;否则,如果没有token并且包含其他变量,则返回true.

Rather self explanatory, return true if other input variables outside of the _token, or return true if no token and contains other variables.

这篇关于检查Laravel中的请求数组是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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