Voyager: How to create a created_by column containing the id of the user who created the record

Created on 9 Dec 2016  路  14Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.3
  • Voyager Version: 0.10
  • PHP Version: 5.6
  • Database Driver & Version: MySQL

How to create a created_by column containing the id of the user who created a new item in a new content type like "products".

This column should be hidden and automatically filled by the id like standard workflow in Laravel.

discussion feature question

Most helpful comment

To do exactly what you wish to do, then this should do it:

<?php

namespace App\Observers;

use App\User;

class UserObserver
{
    /**
     * Listen to the User creating event.
     *
     * @param  User  $user
     * @return void
     */
    public function creating(User $user)
    {
        $user->created_by = auth()->user() ? auth()->user()->id : null;
    }
}

Let me know if you have any issues or questions.

All 14 comments

This functionality does not currently exist OOTB in Voyager, so it would be custom code to do so. You could extend the various models to add the field, and override the controllers' store and update actions to set the values.

If this is functionality that enough people want and can make a case for, I'd be open to a pull request for this.

This is possible by making an event for that model and set the user ID before saving.

https://laravel.com/docs/5.3/eloquent#observers

To do exactly what you wish to do, then this should do it:

<?php

namespace App\Observers;

use App\User;

class UserObserver
{
    /**
     * Listen to the User creating event.
     *
     * @param  User  $user
     * @return void
     */
    public function creating(User $user)
    {
        $user->created_by = auth()->user() ? auth()->user()->id : null;
    }
}

Let me know if you have any issues or questions.

THANKS!

What is the difference between both User classes
TCG\Voyager\Models\User
and
App\User

@marktopper

The different is that Voyager's User model have added the required VoyagerUser trait, but you can easily add that to your own User model.
Also the Voyager's User model will set a default avatar if a avatar is not set.

However this is on my refactoring list before we go for the final stable release of v1.0.

Good,

Regarding the VoyagerUser Trait, I'm getting this error just after upgrading to v0.10 when accessing any admin page.

arruw3wdktnvaaaaaelftksuqmcc

Unfortunately I get the same error after updating voyager.php file.

Did you follow the upgrade guide?

Yes I followed it, but I figured out that I didn't run this command

php artisan voyager:admin [email protected]

Now it is fixed!
Thanks.

I know I'm several years late in asking about this.

But, I was wondering if there was either something in the works, or a simple means of extending the bread editor to include this sort of information.

For example, on my models I'm adding a very basic level 'last_updated_by' field, and thanks to this handy issue request, I was able to inject the value to be the current users id.
Now though, what i want is the ability to either see it in the table, or even in the view that last_updated_by and instead of returning the id number, returning the user's name.

Any advice?

That is either a simple BelongsTo relationship, or an accessor which transforms the ID to whatever you want.

Ok, cool. I'll look into that, I was more curious of the actual method of rendering it into the admin section. I was looking at the blade file for editing records and seemed a bit out of my current depth in Laravel so just thought I'd ask. Thanks a bunch!

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings