php - 在 laravel 的刀片中显示来自数据库的值
问题描述
在<option>
标签内,我试图从数据库中添加选定的国家。
<option>
{{ Auth::user()->cpreference()->where('qatype',1)->select('country')->first()}}
</option>
下拉列表中显示的输出如下:
{"country":"Algeria"}
我如何只显示国家名称?
解决方案
访问返回的模型对象的属性
<option>
{{ \Auth::user()->cpreference()->where('qatype', 1)->select('country')->first()->country }}
</option>
并且对刀片视图进行查询不尊重 MVC 架构中的关注点分离
您应该User
像这样向模型添加访问器
public function getCountryAttribute()
{
return $this->cpreference()->where('qatype', 1)->select('country')->first()->country;
}
并在你的视野中打出更优雅的电话
<option>{{ auth()->user()->country }}</option>
希望这可以帮助
推荐阅读
- algorithm - 如何确定这段代码效率描述的递推关系
- node.js - 来自 GitHub 页面的 HTTP“发布”请求失败
- php - 在php中添加十进制年份
- python - 如何使用python对一组数据进行指数曲线拟合?
- google-apps-script - 在 Google 表格的指定表格中运行脚本
- java - 无法连接到 MariaDB (java)
- android - 如何解决 Android 中的“无法解析方法 getAssets()”
- ansible - 稳定 | 条件检查“item.stat.exists”失败
- office365 - 从 Graph API“您没有所需的权限”创建 Planner 计划
- mysql - 为 MySQL 中查询返回的每一行调用一个查询