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."
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...
You can use this instead:
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
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;
});
}
...
}
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community