5
class Profile extends Eloquent {

    protected $fillable = array('name', 'last_name', 'website', 'facebook', 'twitter', 'linkedin', 'google_plus');

    public static function boot(){
        parent::boot();

        self::updating(function($model){
            $model->name = Crypt::encrypt($model->name);
            $model->last_name = Crypt::encrypt($model->last_name);
            $model->facebook = Crypt::encrypt($model->facebook);
            $model->twitter = Crypt::encrypt($model->twitter);
            $model->linkedin = Crypt::encrypt($model->linkedin);
            $model->website = Crypt::encrypt($model->website);
            $model->google_plus = Crypt::encrypt($model->google_plus);
        });
    }
}

I'm using too call the event..

$user->profile()->update(array(
    'name' => e($input['name']),
    'last_name' => e($input['last_name']),
    'website' => e($input['website']),
    'facebook' => e($input['facebook']),
    'twitter' => e($input['twitter']),
    'linkedin' => e($input['linkedin']),
    'google_plus' => e($input['google_plus'])
));

For some reason, it's not triggering any of the events... I'm trying to encrypt the user information before saving it to the database

rmobis
  • 26,129
  • 8
  • 64
  • 65
Mauro Casas
  • 2,235
  • 1
  • 13
  • 15
  • Of course its not updating, for the boot function, you did not pass anything, second, for the updating function you just passed model, that's incorrect, you need to pass all the params to the function what you want to encrypt –  Jun 30 '14 at 07:10
  • @LeventeNagy did you read the docs on laravels model events? The `boot()` method does not need any arguments and for the events closure the model is passed. http://laravel.com/docs/eloquent#model-events – thpl Jun 30 '14 at 07:12
  • did you read my answer carefully? –  Jun 30 '14 at 07:14
  • @LeventeNagy Sorry, I didn't get it either. Care to clarify? – rmobis Jun 30 '14 at 08:17

3 Answers3

5

The boot events will only be fired when the save() method is called, instead of update() which you are using.

LC Yoong
  • 1,772
  • 3
  • 15
  • 19
4

As for Laravel 9 (and probably earlier) It is firing on both update and save, the only catch is there must be data to update. If the request is identical as the existing data the events will not be fired.

moghwan
  • 1,829
  • 2
  • 17
  • 29
0

May I suggest you instead go for a solution using something similar to Accessors & Mutators? This is, in my opinion, a better solution for a few reasons. For instance, you won't have to worry if your data is currently encrypted or not; if you try using your current code, you would have non-encrypted data before saving and encrypted data right after doing so, you will even have encrypted and non encrypted data saved on the object at the same time. That may cause unexpected stuff to happen. Say you have a simple method like this one:

public function index($id) {
    $user = User::find($id);

    $user->name = 'Raphael';
    echo($user->name); // 'Raphael'

    $user->save();
    echo($user->name); // Encrypted value for 'Raphael'
}

How can you possibly know if when you call for $user->name it will be encrypted or not? I mean, of course you could establish a convention of only sending through models after saving them, but that seems like asking for trouble and any small lack of attention might give you a headache.

On the other hand, if you go for this solution, you would always have the data saved as encrypted but wouldn't have to worry about decrypting it when you want to read it, nor encrypting before saving. Something along these lines should get your problems solved:

class Profile extends Eloquent {

    protected static $encryptable = ['name', 'last_name', 'website', 'facebook', 'twitter', 'linkedin', 'google_plus'];

    public function getAttribute($key) {
        $value = parent::getAttribute($key);

        if (in_array($key, static::$encryptable)) {
            return Crypt::decrypt($value);
        }

        return $value;
    }

    public function setAttribute($key, $value) {
        if (in_array($key, static::$encryptable)) {
            $value = Crypt::encrypt($value);
        }

        parent::setAttribute($key, $value);
    }
}

Note: I'd recommend turning this into an abstract class and extending from it or even using a trait if you're on PHP >= 5.4.

rmobis
  • 26,129
  • 8
  • 64
  • 65