简体   繁体   English

检查请求对象为空Laravel

[英]Check Request object is empty Laravel

add multiple contacts at once. 一次添加多个联系人。 If a contact has details in a form.I want to check the if the output values are null I don't want to save the values. 如果联系人在表单中有详细信息。我想检查输出值是否为空,我不想保存这些值。 otherwise, save the values 否则,保存值

 <tr >
 <td> {!!Form::text('contact[][first_name]',NULL,['class'=>'form-control'])!!}
 </td>
 <td> {!!Form::text('contact[][last_name]',NULL,['class'=>'form-control'])!!}
 </td>
 <td> {!!Form::text('contact[][email]',NULL,['class'=>'form-control'])!!}
 </td>
 <td> {!!Form::text('contact[][contact]',NULL,['class'=>'form-control'])!!}
 </td>

the problem I'm facing is 我面临的问题是

 $contacts =  $request->contacts ;

I want to check if any values in this request are not null save to database.if all the values in the request. 我想检查此请求中是否有任何不为null的值保存到database.if请求中的所有值。 don't need to save 不需要保存

Check them with a foreach loop: 用foreach循环检查它们:

$save = true;
foreach ($contacts as $contact) {
    if ($contact === null) {
        $save = false;
    }
}

if ($save) {
    //Save everything
}

I'm not sure if you mean you want to know if there is any value which is null , or if all values are null (or rather, any value isn't null ). 我不确定您是否想知道是否存在任何值为null ,或者是否所有值均为null (或者,任何值都不为 null )。

For either case, there are several native array functions you could use. 无论哪种情况,都可以使用多个本机数组函数。

To know if any value in the array is null 知道数组中的任何值是否为 null

I would use in_array : 我会使用in_array

in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool

Searches haystack for needle using loose comparison unless strict is set. 除非设置严格,否则使用松散比较在干草堆中搜索针。

In your case: 在您的情况下:

$save = in_array(null, $contacts);

To know if any value in the array isn't null 要知道数组中的任何值都不为 null

I would use array_filter 我会用array_filter

array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array

Iterates over each value in the array passing them to the callback function. 遍历数组中的每个值,将它们传递给回调函数。 If the callback function returns true, the current value from array is returned into the result array. 如果回调函数返回true,则将数组中的当前值返回到结果数组中。 Array keys are preserved. 保留阵列键。

... If no callback is supplied, all entries of array equal to FALSE ( see converting to boolean ) will be removed. ...如果未提供回调,则将删除所有等于FALSE的数组条目( 请参阅转换为boolean )。

So if you don't distinct between null or other false -y values, just use: 因此,如果您不区分null或其他false -y值,请使用:

$flitered = array_filter($contacts);

Which will create an array of only the truth -y values in $contacts. 这将在$ contacts中创建仅包含truth值-y值的数组。

If you want to filter null strictly, you'll need to pass a filter callback. 如果要严格过滤null ,则需要传递过滤器回调。 Since there's no is_not_null function, we'll define it: 由于没有is_not_null函数,我们将对其进行定义:

$filtered = array_filter($contacts, function ($val) {
    return $val !=== null;
});

(As an aside, I've seen mentions of it being very slightly optimized to define anonymous callbacks as static , but that's irrelevant). (顺便说一句,我已经提到过对其进行了一些微优化,以将匿名回调定义为static ,但这是不相关的)。

You can then: 然后,您可以:

$save = (bool) $filtered;

Casting an array to boolean will result in false if empty (no valid values in $contacts ), true otherwise. 如果为空,则将数组强制转换为布尔值将导致false$contacts没有有效值),否则返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM