Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setPasswordAttribute in Laravel

I was trying to seed DB in Laravel with the given data, and i saw a snippet about HASH::make, mentioning it in The Model not in Seeder file..

TaskerTableSeeder.php

class TaskerTableSeeder extends Seeder{

    public function run(){

        Tasker::truncate();

        Tasker::create([
            'username'=>'junni',
            'email'=> '[email protected]',
            'password'=> 'Junaid'
            ]);
        Tasker::create([
            'username'=>'test',
            'email'=> '[email protected]',
            'password'=> 'Test'
            ]);
        Tasker::create([
            'username'=>'poni',
            'email'=>'[email protected]',
            'password'=>'Poni'
            ]);

    }
}

and I Put that Code in my Tasker Model for Hash::make

class Tasker extends Eloquent{

    public function setPasswordAttribute($value){

        $this->attributes['password'] = Hash::make($value);
    }

}

it is the way to make your password HASH encrypted, but i didn't find any information about setPasswordAttribute Function in Laravel Documentation.. and how many other attributes are there for which we can use such type of Functions.

like image 784
Junaid Farooq Avatar asked May 20 '14 07:05

Junaid Farooq


People also ask

What is Bcrypt in laravel?

The Laravel Hash facade provides secure Bcrypt hashing for storing user passwords. If you are using the AuthController controller that is included with your Laravel application, it will be take care of verifying the Bcrypt password against the un-hashed version provided by the user.


1 Answers

They are called Accessors and Mutators.

See Laravel documentation for more information.

They allow you to define behaviour when you set (mutator) or get (accessor) variables from your Eloquent models.

Another example would be

   public function setUsernameAttribute($value){

        $this->attributes['username'] = strtolower($value);
    }
like image 189
Laurence Avatar answered Sep 28 '22 18:09

Laurence