繁体   English   中英

Laravel 5.4-模型访问器不起作用

[英]Laravel 5.4 - Model accessor not working

我一直在尝试使访问器工作几个小时,但无济于事。 我已经将模型代码简化为简单的代码,但是仍然没有运气。 这是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;

这应该工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM