检查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.