【问题标题】:in_array() expects parameter 2 to be array, null given in Laravel Controllerin_array() 期望参数 2 是数组,在 Laravel 控制器中给出 null
【发布时间】:2019-12-16 06:17:17
【问题描述】:

我有两个请求样本和不完整样本,我通过提交表单而不勾选任何复选框来获取完整样本,然后通过单击复选框获取不完整样本。

我可以通过勾选复选框来获取不完整的样本。我在获取完整样本时遇到错误。

if(!in_array($sample, $request->pending)){
    $tests = Session('tests');
    $createSample = Sample::create([
        'test_user_id' => $request->test_user_id,
        'received_by' => (integer)$request->received_by,
        'received_at' => $now,
        'received_name' => $request->received_name,
        'biobank' => $request->biobank,
        'order_id' => $request->order_id,
        'order_type' => $request->order_type,
        'name' => $request->name,
    ]);

【问题讨论】:

  • 首先,检查您的请求中是否有待处理的值
  • 没有,但我需要跳过错误并添加样本值

标签: arrays laravel view controller php-7.2


【解决方案1】:

这是因为如果您不选中该复选框,则该值将不会提交,因此 $request->pending 将为 null 而不是数组。你可以尝试检查它是否为空,然后你可以用它做任何你想做的事情。

if($request->pending){
    if(!in_array($sample, $request->pending)){
        $tests = Session('tests');
        $createSample = Sample::create([
                        'test_user_id' => $request->test_user_id,
                        'received_by' => (integer)$request->received_by,
                        'received_at' => $now,
                        'received_name' => $request->received_name,
                        'biobank' => $request->biobank,
                        'order_id' => $request->order_id,
                        'order_type' => $request->order_type,
                        'name' => $request->name,
        ]);
     }
}

【讨论】:

  • 是的,即使请求不包含待处理,我如何仍能保存请求
  • 那你为什么要使用 if 条件呢?删除它。
猜你喜欢
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2013-08-30
  • 2014-06-19
相关资源
最近更新 更多