Troubleshooting
Common questions and solutions when using Laravel Auditable.
Common Questions
Fields are not being recorded
Q: The created_by and updated_by fields are always null. Why?
A: This usually happens when:
-
No authenticated user - The fields are only populated when a user is authenticated. If running in console or without authentication, values will be
null. -
Not using Eloquent events - Auditable only works with Eloquent model events. Direct query builders won't trigger the observer.
-
Using
update()on query builder - UseModel::find($id)->update()instead ofModel::where(...)->update().
Custom column names not working
Q: I defined CREATED_BY constant but the trait still uses created_by.
A: Make sure the constant is defined as a class constant:
class Post extends Model{ use AuditableTrait; public const CREATED_BY = 'author_id'; // Not protected $createdBy = 'author_id'}
Relationship returns default value
Q: $post->creator returns a default object instead of null.
A: The relationship uses withDefault() from config. To change this behavior, publish and modify config/auditable.php:
'defaults' => [ 'creator' => null, // Return null instead of default object],
scopeOwned returns unexpected results
Q: Post::owned()->get() returns no records.
A: Ensure:
- A user is authenticated (
auth()->check()returnstrue) - The records have matching
created_byvalues with the user's ID - You're using the correct authentication guard
Cannot use both traits together
Q: Can I use AuditableTrait and AuditableWithDeletesTrait together?
A: No, AuditableWithDeletesTrait already extends the functionality of AuditableTrait. Use only AuditableWithDeletesTrait when you need delete tracking.
Soft delete observer not firing
Q: The deleted_by field is not being set when deleting.
A: Make sure:
- Your model uses
SoftDeletestrait - You're calling
$post->delete(), notPost::destroy($id) - The observer is registered (check in
AppServiceProvideror model's boot method)
Debugging Tips
Check if observer is registered
// In tinker or route$post = new Post();$observers = $post->getObservableEvents();dd($observers);
Verify column configuration
$post = new Post();echo $post->getCreatedByColumn(); // Should be 'created_by' or customecho $post->getQualifiedUserIdColumn(); // Should be 'posts.created_by'
Test authentication
// In a route or tinkerauth()->loginUsingId(1);$post = new Post();$post->title = 'Test';$post->save(); echo $post->created_by; // Should be 1
Getting Help
If you encounter issues not covered here:
- Check the GitHub Issues
- Review the Configuration documentation
- See the Auditable Trait API for method details