laravel/framework — pull request #1530 by cviebrock (2013-06-04 19:10:55+00:00)
Title: Allow $model->visible to declare custom accessors

Scenario: An eloquent model has a custom accessor that we want included when converting the model to an array.

```php
class User extends Eloquent {

// first and last name are stored in the DB, full name isn't

protected $visible = array('first_name','last_name','full_name');

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

}

...

$user = User::find($id);
dd($user->toJson());

```

This will return:

```
{"first_name":"Colin","last_name":"Viebrock"}
```

The `full_name` isn't included because that attribute doesn't already exist in the model. i.e. `toArray()` and `toJson()` only operate on existing attributes, or custom accessors that modify existing attributes, but not custom accessors that create new values.

This patch fixes it, and returns:

```
{"first_name":"Colin","last_name":"Viebrock","full_name":"Colin Viebrock"}
```