php - Laravel 5.4 - 模型访问器不起作用
问题描述
我一直试图让访问器工作几个小时,但无济于事。我已将我的模型代码简化为最基本的,但仍然没有运气。这是 PersonaIdentificacion.php 中的代码:`
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PersonaIdentificacion extends Modelo
{
public $table = "personaidentificacion";
public function getFooAttribute() {
return 1;
}
}`
我使用 Artisan Tinker 尝试检索“foo”属性的值,但我只得到:“null”。我不明白。我错过了什么??
解决方案
你的 getFooAttribute() 很好。假设它是一个新属性,则在您的模型中添加以下内容
protected $appends=['foo'];
然后你可以打电话
$id = App\PersonaIdentificacion::first(); $id->foo;
这应该可以正常工作。
推荐阅读
- ocaml - 覆盖变体时如何访问全局构造函数?
- c# - 对话一直卡在最后一句话
- node.js - npm 错误!cb() 从未在 windows10 中调用过
- javascript - React.js 材质 UI - 具有弹性显示的 CardActionArea - 子元素不使用全宽
- google-app-engine - 谷歌应用引擎重定向问题中的角度应用
- java - Java数组/逆新手
- python - 计算所有可能的三角形
- android - Intellij IDEA 2019 中的离线 Android Gradle 插件 3.5.0
- alexa - 如何从传感器发送 Alexa 智能家居主动变更报告?
- algorithm - 求二叉树高度的递归关系和时间复杂度