Laravel Blade:在对象中调用属性会导致NULL

时间:2018-03-05 21:47:09

标签: php json laravel object blade

我正在使用刀片来显示包裹。该软件包包含一些真或假的选项。但是,当我尝试使用刀片访问这些选项时,它返回NULL。这是完整的JSON对象$item

{"id":1,"name":"Budget Pakket","created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","script_options":[{"id":1,"name":"CMS","price":506,"parent_id":null,"type_id":3,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":1,"included":1}},{"id":2,"name":"Foto Album","price":481,"parent_id":null,"type_id":2,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":2,"included":1}},{"id":3,"name":"Parallax Images","price":643,"parent_id":null,"type_id":1,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":3,"included":1}},{"id":4,"name":"Meerdere Menus","price":868,"parent_id":null,"type_id":3,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":4,"included":1}},{"id":5,"name":"Logo Design","price":370,"parent_id":null,"type_id":1,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":5,"included":0}},{"id":6,"name":"Deadline","price":179,"parent_id":null,"type_id":2,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":6,"included":0}},{"id":7,"name":"Thema Op Maat","price":146,"parent_id":null,"type_id":3,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":7,"included":0}},{"id":8,"name":"Thema Kant-en-Klaar","price":390,"parent_id":null,"type_id":1,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":8,"included":1}},{"id":9,"name":"Bulk Email","price":311,"parent_id":null,"type_id":1,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":9,"included":0}},{"id":10,"name":"Layouts","price":497,"parent_id":null,"type_id":1,"created_at":"2018-03-05 20:38:36","updated_at":"2018-03-05 20:38:36","pivot":{"package_id":1,"script_option_id":10,"included":1}}]}

所以,据我所知,这应该可以显示script_options

{{ $item->script_options }}

我也尝试使用以下代码行迭代它们:

@foreach($item->script_options as $option)
    <tr>
        {{ $option }}
    </tr>
@endforeach

但由于某种原因,它给了NULL ....

修改

我用

检索了结果
$result = Package::with('scriptOptions')->get();
return view('package.index', compact('result'));

然后我用这个迭代3个结果:

@foreach($result as $item)
    ......
@endforeach

编辑2

enter image description here

1 个答案:

答案 0 :(得分:2)

在您的刀片文件顶部尝试{{ dd($result) }},看看$ result是否包含数据。