如何在Laravel中使用每个作为递归?

时间:2017-05-22 06:47:46

标签: laravel laravel-5.2 laravel-5.4

我有一些像以下的对象:

array:8 [▼
  "_id" => MongoId {#261 ▶}
  "name" => "Objects"
  "default" => "900"
  "visibility" => "1"
  "type" => "2"
  "only_numbers" => null
  "value" => array:2 [▼
    0 => "59222778470e6c8e0b3c988a"
    1 => "59222880470e6cb90e3c9897"
  ]
  "available" => "1"
]

我尝试在页面上显示:

{{$field["name"]}} {{$field["value"]}}

但是value内部存在具有相同结构的嵌套对象上的键。

所以,我需要展示这些孩子。

如何在刀片Laravel中使用each operator进行此操作?

我试过了:

@each('field.objects',$ field [“value”],'nested')

模板是:

<div class="fields-list row">
    {{dd($v)}}
    @foreach($field["value"] as $k => $v)
        <div class="col-md-4">{{$v['name']}}</div>
        <div class="col-md-8">
            {{Form::checkbox('field_object['.\App\Helpers\Helper::getMongoIDString($v['_id']).']', $v["default"], array("class" => "form-control"))}}
        </div>
    @endforeach
</div>

1 个答案:

答案 0 :(得分:0)

如果您只希望对值进行一级递归,那么这是一个简单的@if检查:

@foreach($fields as $field)
    @if(is_array($field->value))
        @foreach($field->value as $key => $value)
            {{$value}} <br/>
        @endforeach
    @else
        {{$field->value}}
    @endif
@endforeach