欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Add a custom attribute to a Laravel / Eloquent model on load? 模型对像增加自定义的属性 本站 laravel 目录下 搜下 访问器 & 修改器 有大用 有大大用

I'd like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean's $model->open() method.

For instance, at the moment, in my controller I have:

public function index()
{
    $sessions = EventSession::all();
    foreach ($sessions as $i => $session) {
        $sessions[$i]->available = $session->getAvailability();
    }
    return $sessions;
}

It would be nice to be able to omit the loop and have the 'available' attribute already set and populated.

I've tried using some of the model events described in the documentation to attach this property when the object loads, but without success so far.

Notes:

  • 'available' is not a field in the underlying table.

  • $sessions is being returned as a JSON object as part of an API, and therefore calling something like $session->available() in a template isn't an option



7 Answers 正确答案

up vote243down voteaccepted

The problem is caused by the fact that the Model's toArray() method ignores any accessors which do not directly relate to a column in the underlying table.

As Taylor Otwell mentioned here, "This is intentional and for performance reasons." However there is an easy way to achieve this:

class EventSession extends Eloquent {

    protected $table = 'sessions';
    protected $appends = array('availability');

    public function getAvailabilityAttribute()
    {
        return $this->calculateAvailability();  
    }
}

Any attributes listed in the $appends property will automatically be included in the array or JSON form of the model, provided that you've added the appropriate accessor.

Old answer (for Laravel versions < 4.08):

The best solution that I've found is to override the toArray() method and either explicity set the attribute:

class Book extends Eloquent {

    protected $table = 'books';

    public function toArray()
    {
        $array = parent::toArray();
        $array['upper'] = $this->upper;
        return $array;
    }

    public function getUpperAttribute()
    {
        return strtoupper($this->title);    
    }

}

or, if you have lots of custom accessors, loop through them all and apply them:

class Book extends Eloquent {

    protected $table = 'books';

    public function toArray()
    {
        $array = parent::toArray();
        foreach ($this->getMutatedAttributes() as $key)
        {
            if ( ! array_key_exists($key, $array)) {
                $array[$key] = $this->{$key};   
            }
        }
        return $array;
    }

    public function getUpperAttribute()
    {
        return strtoupper($this->title);    
    }

}

    The last thing on the Laravel Eloquent doc page is:

    protected $appends = array('is_admin');

    That can be used automatically to add new accessors to the model without any additional work like modifying methods like ::toArray().

    Just create getFooBarAttribute(...) accessor and add the foo_bar to $appends array.

      If you rename your getAvailability() method to getAvailableAttribute() your method becomes an accessor and you'll be able to read it using ->available straight on your model.

      Docs: https://laravel.com/docs/5.4/eloquent-mutators#accessors-and-mutators

      EDIT: Since your attribute is "virtual", it is not included by default in the JSON representation of your object.

      But I found this: Custom model accessors not processed when ->toJson() called?

      In order to force your attribute to be returned in the array, add it as a key to the $attributes array.

      class User extends Eloquent {
          protected $attributes = array(
              'ZipCode' => '',
          );
      
          public function getZipCodeAttribute()
          {
              return ....
          }
      }

      I didn't test it, but should be pretty trivial for you to try in your current setup.

        you can use setAttribute function in Model to add a custom attribute

        I had something simular: I have an attribute picture in my model, this contains the location of the file in the Storage folder. The image must be returned base64 encoded

        //Add extra attribute
        protected $attributes = ['pictureData'];
        
        //Make it available in the json response
        protected $appends = ['pictureData'];
        
        //implement the attribute
        public function getPictureDataAttribute()
        {
            $file = Storage::get($this->picture);
            $type = Storage::mimeType($this->picture);
            return "data:" . $type . ";base64," . base64_encode($file);
        }

        in my case, creating an empty column and setting its accessor worked fine. my accessor filling user's age from dob column. toArray() function worked too.

        public function getAgeAttribute()
        {
          return Carbon::createFromFormat('Y-m-d', $this->attributes['dateofbirth'])->age;
        }

        In my subscription model, I need to know the subscription is paused or not. here is how I did itpublic function getIsPausedAttribute() {        $isPaused = false;        if (!$this->is_active) {            $isPaused = true;        } }

        then in the view template,I can use $subscription->is_paused to get the result.

        The getIsPausedAttribute is the format to set a custom attribute,

        and uses is_paused to get or use the attribute in your view.

        来自  https://stackoverflow.com/questions/17232714/add-a-custom-attribute-to-a-laravel-eloquent-model-on-load

        普通分类: