Getter & Setter Methods in Eloquent

As I mentioned before, Laravel offers this 2 methods to handle how information is displayed or changed before insert.

The rules are simple: you declare them by appending “set_”  or “get_” to the intended attribute’s name.

Setter example and usage

To format a date:

public function get_date_start()

   return date('M d, Y', $this->get_attribute('date_start')));


Now everytime you use $object->date_start, it will be formatted accordingly.

Getter example and usage

To encrypt a password before inserting in the database:

public function set_password($password) 
    $this->set_attribute('password', Hash::make($password)); 


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s