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

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

我正在使用刀片显示包。该软件包包含一些真或假的选项。但是,当我尝试使用刀片访问这些选项时,它返回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

php json laravel object blade
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.