Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Nested Getters and Setters #50

Closed
wants to merge 7 commits into from
Closed

Nested Getters and Setters #50

wants to merge 7 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Oct 10, 2013

Added capabilities for models to get and set nested attributes using dot notation or camel-case.

$user = User::find($id);

$firstName = $user->nameFirst;

The above code would pull attributes from

$model->attributes['name']['first'];
$user->nameFirst = "David";

This would set the the attribute

$model->attribute['name']['first']
public function setNameFirstAttribute($value)
{
    $this->attributes['name']['first'] = $value;
}

public function getNameFirstAttribute($value)
{
    return "First name : " . $value;
}

public function getNameAttribute($value)
{
    $first = $this->getAttribute("name.first");
    $last = $this->getAttribute("name.last");

    return $first . ' ' . $last;
}

Mutators work using dot notation with the getAttribute() and setAttribute()

@ghost
Copy link
Author

ghost commented Oct 10, 2013

Having spent some more time thinking this through, I don't think camel case is the way to go as that would conflict with the Eloquent way of getting underscored attributes. Maybe underscores to denote array depth with camel case attributes. Again this is a touchy subject as there would need to be database naming convention compromises made. Some suggestion on this would be good.

This pull request was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants