282

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
Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
coatesap
  • 10,707
  • 5
  • 25
  • 33
  • What is the code stands for getAvailability method? If it is possible to calculate on SQL side maybe then better add a custom field into select? – Igor Popov Aug 12 '22 at 01:16
  • use Illuminate\Database\Eloquent\Model as MysqlModel; use Basemkhirat\Elasticsearch\Model as EsModel; class BaseModel extends EsModel{ } how we can change dynamically extend derive in Laravel model?? In this case, in configuration, we use elastic search if we want to change the driver to MySQL we need to manually change the driver? is there any way to change it from configuration ? – Muhammad Awais Zulifqar Aug 17 '22 at 18:49

10 Answers10

407

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);    
    }

}
coatesap
  • 10,707
  • 5
  • 25
  • 33
  • Best question and answer for today. I was working on different implementations on how to achieve this and just before posting an question on laravel.io i found this ! yay !!! – Gayan Hewa Jul 21 '14 at 07:36
  • And is there a way to don't add them to json for just some cases? – Jordi Puigdellívol Jan 18 '15 at 21:36
  • 3
    These customs attributes don't seem to appear when calling the model through a relationship. (Ex: Models\Company::with('people')). Any idea? – Andrew Feb 10 '15 at 23:19
  • @JordiPuigdellívol In Laravel 5, you can use the `protected $hidden = []` to add columns/accessors to have exluded. – junkystu Nov 05 '15 at 09:22
  • Good day, Sir. I am wondering if my case [here](https://stackoverflow.com/questions/69241063/how-to-modify-a-relationship) is sort of achievable by this kind of solution. May I ask for some help from you, my kind Sir? – user16780927 Sep 20 '21 at 04:49
  • "use Illuminate\Database\Eloquent\Model as MysqlModel; use Basemkhirat\Elasticsearch\Model as EsModel; class BaseModel extends EsModel{ } "" how we can change dynamically extend derive in Laravel model?? In this case, in configuration, we use elastic search if we want to change the driver to MySQL we need to manually change the driver? is there any way to change it from configuration ? – Muhammad Awais Zulifqar Aug 17 '22 at 18:50
134

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.

James
  • 4,442
  • 4
  • 19
  • 15
trm42
  • 2,536
  • 2
  • 19
  • 16
  • 5
    Ah interesting. This feature has been added to Laravel since my question was posted (https://github.com/laravel/framework/commit/615f16fb0407f7668536bdc0b578fea60dda5d18). This is right answer for anyone using v4.08 or higher. – coatesap Oct 19 '13 at 06:34
  • 3
    This won't be available to you if you're using relationships to generate the content for your accessors. In this instance, you may have to resort to overriding the `toArray` method. – gpmcadam Apr 05 '15 at 03:29
  • 2
    It seems as though the documentation that you referred to has been moved to here: [https://laravel.com/docs/5.5/eloquent-serialization](https://laravel.com/docs/5.5/eloquent-serialization). – mibbler Oct 12 '17 at 08:38
43

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.

Jazerix
  • 4,729
  • 10
  • 39
  • 71
Alexandre Danault
  • 8,602
  • 3
  • 30
  • 33
  • This works in as much as it makes `->available` available on the `$session` object, but as `$sessions` gets converted directly into a JSON string (it's part of an API), there isn't a chance to use this. – coatesap Jun 21 '13 at 13:28
  • I'm not sure I understand how your stuff works. If `EventSession::all()` returns a JSON object from an API, you are not really using a Laravel model, right? Sorry, I'm confused on how you implemented you model. – Alexandre Danault Jun 21 '13 at 13:40
  • EventSession is a standard Eloquent object (`class EventSession extends Eloquent`). `::all()` is just as an example. `EventSession::find(170071)` would be another. These are called at various points throughout SessionController (`SessionController extends \BaseController`) which would be called via URLs such as '/sessions/170071'. – coatesap Jun 21 '13 at 13:55
  • I think the key may lie in Eloquent's built-in object to JSON conversion that takes place. Even if you add a custom attribute such as `public $available` to the model, this isn't shown when the object is converted. – coatesap Jun 21 '13 at 14:01
  • See my edit, apparently you can force your "virtual" attribute to be return in the JSON. – Alexandre Danault Jun 21 '13 at 20:03
  • As the OP found in the comments, unfortunately this doesn't seem to work in L4. – coatesap Jun 22 '13 at 11:12
  • 3
    This is available since the release of Laravel 4.0.8 on October 8th, 2013. See the official docs: http://laravel.com/docs/eloquent#converting-to-arrays-or-json (look for `protected $appends = array('is_admin');`) – Ronald Hulshof Nov 21 '13 at 13:58
31

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 = ['picture_data'];

//Make it available in the json response
protected $appends = ['picture_data'];

//implement the attribute
public function getPictureDataAttribute()
{
    $file = Storage::get($this->picture);
    $type = Storage::mimeType($this->picture);
    return "data:" . $type . ";base64," . base64_encode($file);
}
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
Patrique
  • 311
  • 3
  • 2
29

Step 1: Define attributes in $appends
Step 2: Define accessor for that attributes.
Example:

<?php
...

class Movie extends Model{

    protected $appends = ['cover'];

    //define accessor
    public function getCoverAttribute()
    {
        return json_decode($this->InJson)->cover;
    }

Bedram Tamang
  • 3,748
  • 31
  • 27
20

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

jianfeng
  • 2,440
  • 4
  • 21
  • 28
11

Let say you have 2 columns named first_name and last_name in your users table and you want to retrieve full name. you can achieve with the following code :

class User extends Eloquent {


    public function getFullNameAttribute()
    {
        return $this->first_name.' '.$this->last_name;
    }
}

now you can get full name as:

$user = User::find(1);
$user->full_name;
ShuBham GuPta
  • 194
  • 2
  • 9
3

In my subscription model, I need to know the subscription is paused or not. here is how I did it

public 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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
li bing zhao
  • 1,388
  • 13
  • 12
2

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;
}
0

You can use map.

$user = User::all(); 
$user->map(function ($u) 
{ 
      $u['AttributeName'] = 'Attribute Value'; 
      return $u 
});

Then object user should have Attribute Name added to it