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

Posted

技术标签:

【中文标题】Laravel Blade:在对象中调用属性会导致 NULL【英文标题】:Laravel Blade: calling property in object results in NULL 【发布时间】:2018-08-13 16:09:16 【问题描述】:

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

【问题讨论】:

【参考方案1】:

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

【讨论】:

我已经在编辑中显示了结果。数据一切正常 您是否已将列从script_options 更新为scriptOptions 显示 dd($item-&gt;script_options) 的回复 这当然不能在页面顶部完成。但是,我使用 dd($result[0]-&gt;script_options) 来获得您想要的结果,并返回null

以上是关于Laravel Blade:在对象中调用属性会导致 NULL的主要内容,如果未能解决你的问题,请参考以下文章

试图获取非对象的属性“品牌”(查看:D:\Laravel\Laravel Internship\Laravel file\resources\views\carrental\index.blade.p

Laravel:ErrorException(E_ERROR)试图获取非对象的属性(视图:..../show.blade.php)[关闭]

Laravel Blade If 语句在使用 Guard 进行身份验证检查时抛出非对象属性错误

请教laravel view 怎么显示 controller 中传递的对象值

在 Laravel 8 Jetstream 的 Blade 组件中绑定 Livewire 属性

试图获取非对象的属性“名称”(视图:(...)资源/视图/产品/index.blade.php)