Support the ongoing development of Laravel.io →
Authentication Database Eloquent
Last updated 2 years ago.
0

But now i need to update this columns when the desired action is called (create, update, delete), then the username should also stored...

http://laravel.com/docs/4.2/eloquent#timestamps

"By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest."

0

TerrePorter said:

But now i need to update this columns when the desired action is called (create, update, delete), then the username should also stored...

http://laravel.com/docs/4.2/eloquent#timestamps

"By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest."

My problem are not the *_at columns.

I also need to store the Username (Auth::user()->username) in the created_by, updated_by, deleted_by column.

I need to know, who has modified the data...

Last updated 9 years ago.
0

My problem are not the *_at columns.

I also need to store the Username (Auth::user()->username) in the created_by, updated_by, deleted_by column.

I need to know, who has modified the data...

Sorry I misunderstood.

You could try using the events for the eloquent model.

untested - Ran out of brain power to finish setting up a test site.

class Users extends Eloquent  {

...

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

// create a event to happen on updating
        static::updating(function($table)  {
            $table->updated_by = Auth::user()->username;
        });

// create a event to happen on deleting
        static::deleting(function($table)  {
            $table->deleted_by = Auth::user()->username;
        });

// create a event to happen on saving
        static::saving(function($table)  {
            $table->created_by = Auth::user()->username;
        });
}

...

}

You might need to play with the saving vs updating, I'm not sure as to when each is used.

Hope that helps better,

Terre

0

TerrePorter said:

My problem are not the *_at columns.

I also need to store the Username (Auth::user()->username) in the created_by, updated_by, deleted_by column.

I need to know, who has modified the data...

Sorry I misunderstood.

You could try using the events for the eloquent model.

untested - Ran out of brain power to finish setting up a test site.

class Users extends Eloquent  {

...

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

// create a event to happen on updating
       static::updating(function($table)  {
           $table->updated_by = Auth::user()->username;
       });

// create a event to happen on deleting
       static::deleting(function($table)  {
           $table->deleted_by = Auth::user()->username;
       });

// create a event to happen on saving
       static::saving(function($table)  {
           $table->created_by = Auth::user()->username;
       });
}

...

}

You might need to play with the saving vs updating, I'm not sure as to when each is used.

Hope that helps better,

Terre

Your solution is great, but one small problem is saving() is called on both Update and Create. So instead of that, we should use creating(), like so:

class Users extends Authenticatable {

...

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

        // create a event to happen on updating
        static::updating(function($table)  {
            $table->updated_by = Auth::user()->username;
        });

        // create a event to happen on deleting
        static::deleting(function($table)  {
            $table->deleted_by = Auth::user()->username;
        });

        // create a event to happen on saving
        static::saving(function($table)  {
            $table->created_by = Auth::user()->username;
        });
}

...

}
0

you can use the revisionable package with this https://github.com/fico7489/laravel-revisionable-upgrade upgrade this is more powerful and elegant way that described above

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.