Laravel - Use of Accessors and Mutators to format Eloquent attributes

Laravel - Use of Accessors and Mutators to format Eloquent attributes

In this tutorial, I will let you know the use of Accessors and Mutators to automatically format database fields.

In short term, Accessors are used to format database fields when retrieving from the database and in the same way Mutators are used to format the attributes before saving to the database.

In the secure application, Mutators are commonly used to bcrypt the password before saving into the database.

Defining an Accessors Combine two attributes to create just one

In this example, i have a "User" model with following fields :

  • first_name
  • last_name
  • email
  • password
  • created_at
  • updated_at

Now we will combine first_name and last_name to get full_name with the help of accessors. We will create accessor method within the model in following way:

  1. function getFullNameAttribute()
  2. {
  3. return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];
  4. }

Now you can get the full_name in following way :

  1. $user = App\User::find(1); // works!
  2. echo $user->full_name;
Order Results by Accessors Attribute?

If you want to order results by full_name using orderBy then it will throws an error because the full_name is not real attribute that exist in the database.

  1. $users = User::orderBy('full_name')->get(); // doesn't work

Solution is very simple to get user list in order using sortBy and sortByDesc method.

  1. $users = User::get()->sortBy('full_name'); // works!
  2. return $users;
Defining a Mutators

Mutators use setAttribute to format field before saving into database.

Capitalize the first name before saving

In this example, We will capitalize the first name with the help of mutators in following way:

  1. public function setFirstNameAttribute($value)
  2. {
  3. $this->attributes['first_name'] = ucfirst($value);
  4. }
Hashing Passwords

Never save your password as plain text into your database. Here is quick example to bcrypt the password before save.

  1. public function setPasswordAttribute($value) {
  2. $this->attributes['password'] = bcrypt($value);
  3. }

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
ataşehir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort
bedava bahis