Back

Extend Eloquent Model with created_by, updated_by, deleted_by


[email protected],

i'm new at Laravel, and now i'm building my first app.

My app is a database to store addresses an related content to each address (Companies). I would like to have a Authentication via Laravel - thats the next step.

But now i need to know, how i can extend my Company Model (class Company extends Eloquent) - what uses timestamps - with three columns (created_by, updated_by, deleted_by).

I allready have this columns created with my migration-skript. But now i need to update this columns when the desired action is called (create, update, delete), then the username should also stored...

Does somebody know, how to achieve that?

best regards, Andy

TerrePorter replied 2 years ago

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."

larastarter replied 2 years ago

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...

jarektkaczyk replied 2 years ago

You can use this instead:

https://github.com/jarektkaczyk/revisionable

TerrePorter replied 2 years ago

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

socrec replied 8 months ago

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;
        });
}

...

}

Sign in to participate in this thread!



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