如何检查对象密钥是否存在并且不为null,或者在Laravel刀片服务器的foreach循环中它是否为某个值?

问题描述 投票:0回答:1

我在blade中有一个foreach循环,其中我正在遍历项目数组,并且某些项目还有一个名为validation_status的附加键,该键要么是null object。

示例:

category: "Storage"
client_id: 6575
created_at: "2019-08-12 15:50:04"
created_by: 233
datasheet: null
deleted_at: null
description: "3erty"
energy_rating: 0
fk_item_type: 83
id: 369
item_model_id: 475
model_number: null
power_consumption: 3
searchable: 1
shared: 1
updated_at: "2019-11-09 17:59:04"
updated_by: 233
validated: 0
validation_status: null
vendor: null

category: "Network"
client_id: 6575
created_at: "2019-07-11 16:04:56"
created_by: 233
datasheet: null
deleted_at: null
description: "2we5"
energy_rating: 0
fk_item_type: 83
id: 357
item_model_id: 470
model_number: null
power_consumption: 55
searchable: 1
shared: 1
updated_at: "2019-11-07 10:29:57"
updated_by: 233
validated: 0
validation_status:
created_at: "2019-11-07 16:08:49"
id: 2
item_id: 357
item_type: "IT_Analysis"
message: null
name: "pending"
updated_at: "2019-11-07 16:08:49"
__proto__: Object
vendor: null

我想做某事,如果validation_status首先是exists,并且其名为namekey某些值,例如“待定”。如何在刀片服务器上做到这一点?

这是我的尝试,现在else condition被执行为true,所以我的if条件出了点问题:

    <p>
    @if($item->validation_status && $item->validation_status !== null) 
    @if($item->validation_status->name  === "pending" )
     <?php echo link_to('admin/items/' . $item->id . '/validation-status' . $item->validation_status->id . '/complete', 'Validate', array('class' => 'btn btn-primary w-full')); ?>
     <?php echo link_to('admin/items/' . $item->id . '/validation-status' . $item->validation_status->id . '/documentation-needed', 'Request documentation', array('class' => 'btn btn-primary w-full')); ?>
     <?php echo link_to('admin/items/' . $item->id . '/validation-status' . $item->validation_status->id . '/reject', 'Reject', array('class' => 'btn btn-primary w-full')); ?>
    @endif
    @else
      <?php echo Form::submit('Accept and Save!', array('class' => 'btn btn-primary w-full')); ?>
    @endif
    </p>
php foreach laravel-blade key-value
1个回答
0
投票

这是一个对象,而不是数组。

@php $check = isset($items->validation_status); @endphp

isset()将返回false,如果该属性不存在或为null。

因此非常适合您的情况。

@if($check)
    //Execute your code
@endif
© www.soinside.com 2019 - 2024. All rights reserved.