php - 试图获取非对象的属性“名称”?当我使用 pluck()
问题描述
Trying to get property 'name' of non-object? when i using pluck() for retrive data from database
//a.contoller
public function index(){
$insurance = Insurance_Providers::pluck('id','name');
$physicians = Physicians::pluck('id','pp','sp');
$pharmacies = Pharmacies::pluck('id','name');
return view('auth.profile',compact('insurance','physicians','pharmacies'));
}
//a.blade.php
<select name="insurance" id="ip" class="form-control" required>
<option value="">Insurance Provider</option>
@foreach($insurance as $pro) <option value="{{ $pro->name }}">{{ $pro->name }}</option>
@endforeach
</select>
试图获取非对象的属性“名称”?当我使用 pluck() 从数据库中检索数据时
解决方案
您在 pluck 中使用了三个参数,
$insurance = Insurance_Providers::pluck('name', 'id'); // name is value and id is key
在刀片中这样做,
@foreach($insurance as $pro)
<option value="{{ $pro }}">{{ $pro }}</option>
@endforeach
它应该工作。
第一个参数是值,第二个是键
推荐阅读
- python - 从 x 导入 y ModuleNotFoundError:没有名为“x”的模块
- javascript - 功能有效,但 setState 未更新
- php - 为什么 phpseclib SSH2 不运行命令?
- java - 尝试使用 gson 解析带有空格的 json 对象时出现 java 错误
- javascript - `${variableName}` 和 {variableName} 有什么区别?
- python - 为什么python中的'in'运算符充当比较运算符,但在for循环中使用时它充当分配运算符?
- javascript - 如何在nuxt项目中下载pdf?
- reactjs - 按字母顺序对状态中的项目进行排序
- javascript - 为什么在下面的代码中 useEffect 钩子使用花括号,但没有 return 语句,它仍然有效?
- flutter - Flutter:为什么我在使用斩波器的 api 调用中只得到 3 个结果?