简体   繁体   中英

Laravel 5.4 - Model accessor not working

I´ve been trying to make an accessor work a few hours now, to no avail. I have simplified my model code to the bare bones, and still no luck. Here´s the code in PersonaIdentificacion.php:`

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PersonaIdentificacion extends Modelo
{
    public $table = "personaidentificacion";

    public function getFooAttribute() {
        return 1;
    }

}`

I use Artisan Tinker to try and retrieve the value of the 'foo' property, but I only get: 'null'. I don´t get it. What am I missing??

Your getFooAttribute() is fine. To your model add the following assuming that it's a new attribute

 protected $appends=['foo'];

and then you can call

 $id = App\PersonaIdentificacion::first(); $id->foo;

This should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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