eloquent-blameable icon indicating copy to clipboard operation
eloquent-blameable copied to clipboard

Not filling created_by or updated_by values

Open paytah232 opened this issue 2 years ago • 3 comments

Hi @richan-fongdasen,

I believe I've installed as per documentation (add provider, migrations and attach), but my created_by and updated_by values are not being filled. It is my understanding that once referencing 'use BlameableTrait' and setting up my migrations, it should automatically get the current user id without requiring the override default configuration?

I have removed nullable from created_by and updated_by table entries (as they should always have a user), and I'm using unsignedBigInteger as the method of creation (because it matches the $table->id() method) . I'm getting the following sql error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updated_by' cannot be null (Connection: mysql, SQL: insert into 'categories' ('name', 'description', 'updated_by', 'created_by', 'updated_at', 'created_at') values (Test, <p>test</p>, ?, ?, 2023-04-16 12:54:23, 2023-04-16

However, I believe this package should be providing the updated_by and created_by values automatically. Is there something I've done wrong? I'm relatively new to laravel, but I think I've done it right. Can you please help?

Laravel 10.7.1 PHP 8.1.18 Docker

paytah232 avatar Apr 16 '23 13:04 paytah232

Hi @Paytah232

Thanks for reaching out. I understand that you are having issues using this package and your created_by and updated_by values are not being filled automatically.

Regarding your concern, I would like to clarify that by default, the created_by and updated_by columns should be nullable. This is because not all database insert / update operations will be triggered by a user. For example, when running database seeding, the user who created or updated the records will be null. Therefore, I suggest you revert the changes you made to the migration file and make the columns nullable again.

Regarding the error you are getting, it seems that the updated_by column is not being filled, which is causing the integrity constraint violation. To troubleshoot this issue, I recommend checking if the BlameableTrait is properly set up and if the user is authenticated before performing the update operation. Also, make sure that the user id is being passed correctly to the model.

If the issue persists, please provide more information on your setup, such as the code for your model, controller, and migration file, so we can further investigate the issue.

Thanks.

richan-fongdasen avatar Apr 17 '23 10:04 richan-fongdasen

Thanks for the prompt reply.

Understandable, makes sense. I will add the nullable type to the columns once again.

Yeah, I definitely agree that it's not being passed the user's ID. I (the admin user) was logged in when I ran the create command.

My user ID has the column name 'id' in the 'users' table. Is this what it is looking for, or is it looking for 'user_id' in the user's table?

How do you suggest checking that the user's id is being passed to the model?

Thanks!

Sent from Outlook for Androidhttps://aka.ms/AAb9ysg


From: Richan Fongdasen @.> Sent: Monday, April 17, 2023 8:50:37 PM To: richan-fongdasen/eloquent-blameable @.> Cc: Paytah232 @.>; Mention @.> Subject: Re: [richan-fongdasen/eloquent-blameable] Not filling created_by or updated_by values (Issue #42)

Hi @Paytah232https://github.com/Paytah232

Thanks for reaching out. I understand that you are having issues using this package and your created_by and updated_by values are not being filled automatically.

Regarding your concern, I would like to clarify that by default, the created_by and updated_by columns should be nullable. This is because not all database insert / update operations will be triggered by a user. For example, when running database seeding, the user who created or updated the records will be null. Therefore, I suggest you revert the changes you made to the migration file and make the columns nullable again.

Regarding the error you are getting, it seems that the updated_by column is not being filled, which is causing the integrity constraint violation. To troubleshoot this issue, I recommend checking if the BlameableTrait is properly set up and if the user is authenticated before performing the update operation. Also, make sure that the user id is being passed correctly to the model.

If the issue persists, please provide more information on your setup, such as the code for your model, controller, and migration file, so we can further investigate the issue.

Thanks.

— Reply to this email directly, view it on GitHubhttps://github.com/richan-fongdasen/eloquent-blameable/issues/42#issuecomment-1511117173, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AKQIURDE2EK53EW7R4LM3LTXBUN73ANCNFSM6AAAAAAXADLKQY. You are receiving this because you were mentioned.Message ID: @.***>

paytah232 avatar Apr 17 '23 11:04 paytah232

The package will get the currently logged in user instance approximately like this: $user = Auth::guard(config(‘blameable.guard’))->user();

However, if the value of $user is not compatible with the object defined in config('blameable.user'), the package will assume that the current user is null.

For further reference, please visit this URL: https://github.com/richan-fongdasen/eloquent-blameable/blob/main/config/blameable.php#L5-L33

richan-fongdasen avatar Apr 17 '23 11:04 richan-fongdasen