Accessors & Mutators

What is accessors

Accessors also called getters are used to format data when you retrieve them from the database.

What is mutators

Mutators also called setters are used to change data before saving data inside the database.so if you want all your emails in your database to be lowercase only, you could do:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class todo extends Model
{
	/* Accessor */
	public function getNameAttribute($value){
		return ucfirst($value);
	}

	/* Mutator */
	public function setEmailAttribute($value)
	{
		$this->attributes['email'] = strtolower($value);
	}
}

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s