laravel - 如何从 laravel 中的对象获取数据
问题描述
View Code:
<div class="content">
<div class="title m-b-md">
Pizza List - {{$id}}
</div>
{{$p->type}} // This is not working.Here I only want to print type of pizza from the
db.ERROR
=Property [type] does not exist on this collection instance.
{{$p}} // But This Does shows Data in JSON.
</div>
Controller Code:
public function show($id){
$p=Pizza::where('p_name','prashant')->get();
return view('details',['id'=>$id,'p'=>$p]);
}
在这里,我想通过在 db 中具有列名称 p_name 的人来获取披萨订单的类型,所以我给出了一些静态名称。我只是一个初学者。谁能告诉我怎么做才能只打印披萨类型
解决方案
如果你得到一个比萨值,那么使用这个
$p=Pizza::where('p_name','prashant')->first();
<div class="content">
<div class="title m-b-md">
Pizza List - {{$id}}
</div>
{{$p->type}}
</div>
如果你想要多个披萨值,那么使用这个
$p=Pizza::where('p_name','prashant')->get();
<div class="content">
<div class="title m-b-md">
Pizza List - {{$id}}
</div>
@foreach($p as $pizza)
{{$pizza->type}}
@endforeach
</div>
推荐阅读
- c - struct statx 可以在内核中使用吗?
- javascript - 虚拟 DOM 的真正含义是什么?
- android - 如果设备有软(虚拟)导航按钮,谷歌地图上的 getCameraPosition 错误?
- java - 不推荐使用 Fragment prev 如何更改此代码 Android Studio
- bash - bash grep 复杂搜索
- python - 如何使用 python 3 从 arduino 获取模拟值?
- c# - 尝试在 Unity 3D 中创建进入球体的对象变换列表
- c++ - 在opengl中将二维纹理应用于具有z方向变化的单个立方体面
- javascript - 我想学javascript
- javascript - 如何在表格正方形中制作单元格