### Install Laravel Auditable via Composer Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Commands to install the package and publish the configuration file to your Laravel project. ```bash composer require yajra/laravel-auditable:"^13" php artisan vendor:publish --tag=auditable ``` -------------------------------- ### Install Laravel Auditable via Composer Source: https://github.com/yajra/laravel-auditable-docs/blob/master/installation.md Installs the latest version of the yajra/laravel-auditable package using Composer. This is the primary method for adding the package to your Laravel project. ```bash composer require yajra/laravel-auditable:"^13" ``` -------------------------------- ### Publish Laravel Auditable Configuration Source: https://github.com/yajra/laravel-auditable-docs/blob/master/installation.md Publishes the auditable.php configuration file to your application's config directory using the Artisan command. This allows for customization of package settings. ```bash php artisan vendor:publish --tag=auditable ``` -------------------------------- ### Default Auditable Configuration Source: https://github.com/yajra/laravel-auditable-docs/blob/master/installation.md Shows the default configuration values for creator, updater, and deleter in the auditable.php file when no user is authenticated. This configuration can be modified to suit specific application needs. ```php // config/auditable.php return [ 'defaults' => [ 'creator' => [ 'name' => '', ], 'updater' => [ 'name' => '', ], 'deleter' => [ 'name' => '', ], ], ]; ``` -------------------------------- ### Custom Column Names for Auditing in Laravel Source: https://github.com/yajra/laravel-auditable-docs/blob/master/installation.md Illustrates how to define custom column names for tracking creation and update events on an Eloquent model by using public constants `CREATED_BY` and `UPDATED_BY`. This allows for flexible database schema design. ```php namespace App\Models; use Yajra\Auditable\AuditableTrait; use Illuminate\Database\Eloquent\Model; class Post extends Model { use AuditableTrait; public const CREATED_BY = 'author_id'; public const UPDATED_BY = 'last_editor_id'; } ``` -------------------------------- ### Setup Migration with Auditable Deletes Source: https://github.com/yajra/laravel-auditable-docs/blob/master/soft-deletes.md This PHP code snippet demonstrates how to set up a database migration for a 'posts' table using Laravel's schema builder. It includes the `auditableWithDeletes()` macro to add the `deleted_by` column, along with standard columns like `id`, `title`, `content`, `timestamps`, and `softDeletes`. ```php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->auditableWithDeletes(); $table->timestamps(); $table->softDeletes(); }); } public function down(): void { Schema::dropIfExists('posts'); } }; ``` -------------------------------- ### Usage Example: Deleting and Restoring Records Source: https://github.com/yajra/laravel-auditable-docs/blob/master/soft-deletes.md This PHP code demonstrates the basic usage of the `AuditableWithDeletesTrait`. It shows how to find a record, delete it (which automatically sets the `deleted_by` field to the current user's ID), and then how to restore the record (which clears the `deleted_by` field). ```php $post = Post::find(1); $post->delete(); // Sets deleted_by to current user ID // Restore the record (clears deleted_by) Post::withTrashed()->find(1)->restore(); ``` -------------------------------- ### Custom User Class for Auditing in Laravel Source: https://github.com/yajra/laravel-auditable-docs/blob/master/installation.md Demonstrates how to specify a custom user class for auditing on a specific Eloquent model by defining the `$auditUser` property. This is useful when your application uses multiple user types. ```php namespace App\Models; use Yajra\Auditable\AuditableTrait; use Illuminate\Database\Eloquent\Model; class Post extends Model { use AuditableTrait; protected $auditUser = App\Models\Admin::class; } ``` -------------------------------- ### Get Created By Name Attribute Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Provides an attribute accessor to get the full name of the user who created the record. This simplifies displaying the creator's name directly. ```php public function getCreatedByNameAttribute(): string ``` -------------------------------- ### Retrieve Creator and Updater Information (PHP) Source: https://github.com/yajra/laravel-auditable-docs/blob/master/usage.md This PHP code demonstrates how to retrieve the creator and updater of a 'Post' record using the relationships and accessors provided by Laravel Auditable. It shows how to get the user objects, their IDs, and their names. ```php $post = Post::find(1); // Get the user who created the post $creator = $post->creator; // Get the user who last updated the post $updater = $post->updater; // Get creator and updater names $creatorName = $post->created_by_name; $updaterName = $post->updated_by_name; ``` -------------------------------- ### Get Updated By Name Attribute Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Provides an attribute accessor to get the full name of the user who last updated the record. This simplifies displaying the updater's name directly. ```php public function getUpdatedByNameAttribute(): string ``` -------------------------------- ### Get Deleted By Name Attribute (with Deletes) Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Provides an attribute accessor to get the full name of the user who deleted the record. This requires the AuditableWithDeletesTrait and simplifies displaying the deleter's name. ```php public function getDeletedByNameAttribute(): string ``` -------------------------------- ### Get Deleter Relationship (with Deletes) Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Retrieves the user instance who deleted the record via a BelongsTo relationship. This method requires the AuditableWithDeletesTrait to be used. ```php public function deleter(): BelongsTo ``` -------------------------------- ### Get Created By Column Name Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Returns the name of the column used to store the ID of the user who created the record. It accounts for potential constant overrides. ```php public function getCreatedByColumn(): string ``` -------------------------------- ### Get User Instance Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Returns a new instance of the configured user model. This is helpful for creating or manipulating user objects within the context of the auditable model. ```php public function getUserInstance(): Model ``` -------------------------------- ### Get Deleted By Column Name (with Deletes) Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Returns the name of the column used to store the ID of the user who deleted the record. This method requires the AuditableWithDeletesTrait. ```php public function getDeletedByColumn(): string ``` -------------------------------- ### Get Updater Relationship Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Retrieves the user instance who last updated the record via a BelongsTo relationship. This is useful for tracking the last modification of a record. ```php public function updater(): BelongsTo ``` -------------------------------- ### Get Creator Relationship Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Retrieves the user instance who created the record via a BelongsTo relationship. This method is essential for linking records back to their original author. ```php public function creator(): BelongsTo ``` -------------------------------- ### Get Updated By Column Name Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Returns the name of the column used to store the ID of the user who last updated the record. It accounts for potential constant overrides. ```php public function getUpdatedByColumn(): string ``` -------------------------------- ### Get Qualified User ID Column Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Returns the fully qualified column name for the user ID field (e.g., `posts.created_by`). This is useful for constructing precise SQL queries. ```php public function getQualifiedUserIdColumn(): string ``` -------------------------------- ### Get User Class Name Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Retrieves the class name of the user model being used, respecting any `auditUser` property override. This ensures correct user model instantiation. ```php protected function getUserClass(): string ``` -------------------------------- ### Accessing Deleter Information Source: https://github.com/yajra/laravel-auditable-docs/blob/master/soft-deletes.md This PHP code snippet illustrates how to access information about the user who deleted a soft-deleted record. It shows how to retrieve the deleted record using `withTrashed()` and then access the `deleter` relationship or the `deleted_by_name` accessor to get the deleter's name. ```php $post = Post::withTrashed()->first(); $deleter = $post->deleter; echo $deleter->name; // "Admin User" echo $post->deleted_by_name; // "Admin User" ``` -------------------------------- ### Implement Heading Anchors Source: https://github.com/yajra/laravel-auditable-docs/blob/master/AGENTS.md Required HTML anchor tag to be placed immediately before every H2 section to ensure proper navigation and linking within the documentation. ```html ## Heading Title ``` -------------------------------- ### Define VitePress YAML Frontmatter Source: https://github.com/yajra/laravel-auditable-docs/blob/master/AGENTS.md Standard YAML frontmatter block required at the top of every documentation file for SEO and page metadata. It must be the first content element in the file. ```yaml --- title: "Page Title" description: "Brief description for SEO" --- ``` -------------------------------- ### Utilize debugging helpers Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Use built-in methods to verify column mapping, retrieve user instances, and test authentication auditing. ```php $post = new Post(); echo $post->getCreatedByColumn(); echo $post->getQualifiedUserIdColumn(); $userInstance = $post->getUserInstance(); ``` -------------------------------- ### Verify Column Configuration Source: https://github.com/yajra/laravel-auditable-docs/blob/master/troubleshooting.md Illustrates how to check the configured column names for created by and user ID. This helps ensure that the auditable trait is using the expected database columns. ```php $post = new Post(); echo $post->getCreatedByColumn(); // Should be 'created_by' or custom echo $post->getQualifiedUserIdColumn(); // Should be 'posts.created_by' ``` -------------------------------- ### Configure default audit values Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Publish the configuration file to set default names for system-generated actions when no user is authenticated. ```php return [ 'defaults' => [ 'creator' => ['name' => 'System'], 'updater' => ['name' => 'System'], 'deleter' => ['name' => 'System'], ], ]; ``` -------------------------------- ### Test Authentication and Auditable Fields Source: https://github.com/yajra/laravel-auditable-docs/blob/master/troubleshooting.md A simple test case to verify that the authentication system is working correctly and that the `created_by` field is populated upon saving a new record. ```php // In a route or tinker auth()->loginUsingId(1); $post = new Post(); $post->title = 'Test'; $post->save(); echo $post->created_by; // Should be 1 ``` -------------------------------- ### Use AuditableTrait in Eloquent Model Source: https://github.com/yajra/laravel-auditable-docs/blob/master/readme.md This PHP code shows how to apply the `AuditableTrait` to a Laravel Eloquent model ('Post' in this case). This enables the model to automatically track audit information. ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Yajra\Auditable\AuditableTrait; class Post extends Model { use AuditableTrait; } ``` -------------------------------- ### Add Audit Columns in Migrations Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Use migration macros to add audit columns to database tables. auditable() adds created_by and updated_by, while auditableWithDeletes() includes deleted_by for soft deletes. ```php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->auditable(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('posts'); } }; ``` ```php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->auditableWithDeletes(); $table->timestamps(); $table->softDeletes(); }); ``` -------------------------------- ### Define Custom Created By Column Name Source: https://github.com/yajra/laravel-auditable-docs/blob/master/troubleshooting.md Demonstrates how to define a custom column name for the user who created the record. Ensure the constant is defined as a class constant within your Eloquent model. ```php class Post extends Model { use AuditableTrait; public const CREATED_BY = 'author_id'; // Not protected $createdBy = 'author_id' } ``` -------------------------------- ### Implement Auditable Traits in Models Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Apply AuditableTrait or AuditableWithDeletesTrait to Eloquent models to automatically track user actions. Requires the corresponding database columns to be present. ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Yajra\Auditable\AuditableTrait; class Post extends Model { use AuditableTrait; } ``` ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Yajra\Auditable\AuditableWithDeletesTrait; class Post extends Model { use AuditableWithDeletesTrait; use SoftDeletes; } ``` -------------------------------- ### Create Migration with Auditable Fields (PHP) Source: https://github.com/yajra/laravel-auditable-docs/blob/master/usage.md This code snippet demonstrates how to create a database migration for a 'posts' table, including the auditable fields 'created_by' and 'updated_by' using the `auditable()` blueprint macro. It defines the schema for the table and specifies how to revert the changes. ```php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->auditable(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('posts'); } }; ``` -------------------------------- ### Access Audit Information from Model Source: https://github.com/yajra/laravel-auditable-docs/blob/master/readme.md This PHP code demonstrates how to access audit-related information directly from an Eloquent model instance after applying the `AuditableTrait`. It shows how to retrieve the creator, updater, and their names. ```php $post = Post::find(1); $post->creator; // User who created the post $post->updater; // User who last updated the post $post->created_by_name; // Creator's name $post->updated_by_name; // Updater's name ``` -------------------------------- ### Retrieve Audit Relationships Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Access the creator and updater users via the built-in creator() and updater() relationships. ```php $post = Post::first(); $creator = $post->creator; $updater = $post->updater; // Eager loading $posts = Post::with(['creator', 'updater'])->get(); ``` -------------------------------- ### Add Auditable Fields with Soft Deletes to Migration - PHP Source: https://github.com/yajra/laravel-auditable-docs/blob/master/blueprint.md Adds `created_by`, `updated_by`, and `deleted_by` columns to a migration using the `auditableWithDeletes()` schema blueprint macro, along with `softDeletes()`. These columns will be unsigned big integers, nullable, and indexed. Requires the laravel-auditable package. ```php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->auditableWithDeletes(); $table->timestamps(); $table->softDeletes(); }); ``` -------------------------------- ### Define custom user class Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Specify a custom user model for auditing by setting the $auditUser property on the Eloquent model. ```php class AdminPost extends Model { use AuditableTrait; protected $auditUser = \App\Models\Admin::class; } ``` -------------------------------- ### AuditableWithDeletesTraitObserver: Deleting and Restoring Logic Source: https://github.com/yajra/laravel-auditable-docs/blob/master/soft-deletes.md This PHP code outlines the core logic within the `AuditableWithDeletesTraitObserver`. The `deleting` method sets the `deleted_by` attribute to the authenticated user's ID before saving, while the `restoring` method sets `deleted_by` to `null`. ```php // Deleting event public function deleting(Model $model): void { $model->deleted_by = $this->getAuthenticatedUserId(); $model->saveQuietly(); } // Restoring event public function restoring(Model $model): void { $model->deleted_by = null; } ``` -------------------------------- ### Override Model Audit Settings Source: https://github.com/yajra/laravel-auditable-docs/blob/master/configuration.md Demonstrates how to customize audit behavior per model, including specifying a custom user class and overriding default database column names. ```php namespace App\Models; use Yajra\Auditable\AuditableTrait; use Illuminate\Database\Eloquent\Model; class Post extends Model { use AuditableTrait; protected $auditUser = Admin::class; } ``` ```php namespace App\Models; use Yajra\Auditable\AuditableTrait; use Illuminate\Database\Eloquent\Model; class Document extends Model { use AuditableTrait; public const CREATED_BY = 'author_id'; public const UPDATED_BY = 'last_editor_id'; public const DELETED_BY = 'remover_id'; } ``` -------------------------------- ### Add Auditable Fields to Migration - PHP Source: https://github.com/yajra/laravel-auditable-docs/blob/master/blueprint.md Adds `created_by` and `updated_by` columns to a migration using the `auditable()` schema blueprint macro. These columns will be unsigned big integers, nullable, and indexed. Requires the laravel-auditable package. ```php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->auditable(); $table->timestamps(); }); ``` -------------------------------- ### Configure custom column names Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Define class constants on your model to override default audit column names like created_by or updated_by. ```php class Post extends Model { use AuditableWithDeletesTrait; public const CREATED_BY = 'author_id'; public const UPDATED_BY = 'last_editor_id'; public const DELETED_BY = 'remover_id'; } ``` -------------------------------- ### Access creator and updater names Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Use accessors to retrieve the full name of the user who created, updated, or deleted a record. If no user is authenticated, it falls back to system defaults. ```php $post = Post::first(); echo $post->created_by_name; // "John Doe" echo $post->updated_by_name; // "Jane Smith" $postDeleted = Post::withTrashed()->first(); echo $postDeleted->deleted_by_name; // "Admin User" ``` -------------------------------- ### Configure Global Defaults Source: https://github.com/yajra/laravel-auditable-docs/blob/master/configuration.md Defines default values for creator, updater, and deleter fields in the config/auditable.php file. These values are used when no authenticated user is present, such as during system processes. ```php return [ 'defaults' => [ 'creator' => [ 'name' => 'System', ], 'updater' => [ 'name' => 'System', ], 'deleter' => [ 'name' => 'System', ], ], ]; ``` -------------------------------- ### Remove Audit Columns in Migrations Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Use drop macros to remove audit columns from existing database tables. ```php Schema::table('posts', function (Blueprint $table) { $table->dropAuditable(); }); Schema::table('posts', function (Blueprint $table) { $table->dropAuditableWithDeletes(); }); ``` -------------------------------- ### Add Auditable Fields to Migration Source: https://github.com/yajra/laravel-auditable-docs/blob/master/readme.md This PHP code snippet demonstrates how to add auditable fields to a database migration for a 'posts' table using Laravel's Schema Builder. The `auditable()` macro automatically adds the necessary columns for tracking. ```php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->auditable(); $table->timestamps(); }); ``` -------------------------------- ### Apply AuditableTrait to Eloquent Model (PHP) Source: https://github.com/yajra/laravel-auditable-docs/blob/master/usage.md This PHP code shows how to apply the `AuditableTrait` to an Eloquent model named 'Post'. By using this trait, the model will automatically track the creation and update of its records, including who performed these actions. ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Yajra\Auditable\AuditableTrait; class Post extends Model { use AuditableTrait; } ``` -------------------------------- ### Check Registered Observers Source: https://github.com/yajra/laravel-auditable-docs/blob/master/troubleshooting.md Provides a method to verify if the auditable observer is correctly registered with an Eloquent model. This is useful for debugging issues where auditable fields are not being updated. ```php // In tinker or route $post = new Post(); $observers = $post->getObservableEvents(); dd($observers); ``` -------------------------------- ### Drop Auditable Fields with Soft Deletes from Migration - PHP Source: https://github.com/yajra/laravel-auditable-docs/blob/master/blueprint.md Removes `created_by`, `updated_by`, and `deleted_by` columns from a migration using the `dropAuditableWithDeletes()` schema blueprint macro. This is useful for reverting changes made by the `auditableWithDeletes()` macro. Requires the laravel-auditable package. ```php Schema::table('posts', function (Blueprint $table) { $table->dropAuditableWithDeletes(); }); ``` -------------------------------- ### Boot Auditable Trait Observer Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md Automatically registers an observer when the trait is booted to handle audit field updates. This method is called internally by the trait. ```php public static function bootAuditableTrait(): void ``` -------------------------------- ### Configure Model with Auditable and Soft Deletes Traits Source: https://github.com/yajra/laravel-auditable-docs/blob/master/soft-deletes.md This PHP code shows how to configure a Laravel Eloquent model ('Post') to use both the `AuditableWithDeletesTrait` for audit logging of deletions and Laravel's built-in `SoftDeletes` trait for soft deletion functionality. ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Yajra\Auditable\AuditableWithDeletesTrait; class Post extends Model { use AuditableWithDeletesTrait; use SoftDeletes; } ``` -------------------------------- ### Configure Relationship Default Value Source: https://github.com/yajra/laravel-auditable-docs/blob/master/troubleshooting.md Shows how to modify the default value returned by relationships when the related model is not found. This involves publishing and editing the auditable configuration file. ```php 'defaults' => [ 'creator' => null, // Return null instead of default object ] ``` -------------------------------- ### Filter records with scopeOwned Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Use the scopeOwned query scope to filter Eloquent results to only those created by the currently authenticated user. ```php auth()->loginUsingId(1); // Get all posts created by the authenticated user $myPosts = Post::owned()->get(); // Combine with other query conditions $myPublishedPosts = Post::owned() ->where('status', 'published') ->orderBy('created_at', 'desc') ->get(); ``` -------------------------------- ### Drop Auditable Fields from Migration - PHP Source: https://github.com/yajra/laravel-auditable-docs/blob/master/blueprint.md Removes `created_by` and `updated_by` columns from a migration using the `dropAuditable()` schema blueprint macro. This is useful for reverting changes made by the `auditable()` macro. Requires the laravel-auditable package. ```php Schema::table('posts', function (Blueprint $table) { $table->dropAuditable(); }); ``` -------------------------------- ### Retrieve deleter relationship Source: https://context7.com/yajra/laravel-auditable-docs/llms.txt Access the user who deleted a record using the deleter relationship. Requires the AuditableWithDeletesTrait to be implemented on the model. ```php $post = Post::withTrashed()->first(); $deleter = $post->deleter; echo $deleter->name; // "Admin User" // List all deleted posts with deleter info $deletedPosts = Post::onlyTrashed()->with('deleter')->get(); foreach ($deletedPosts as $post) { echo "{$post->title} deleted by {$post->deleter->name}"; } ``` -------------------------------- ### Scope Owned Query Source: https://github.com/yajra/laravel-auditable-docs/blob/master/auditable-trait.md A query scope that limits results to records owned by the currently authenticated user. It filters records based on the `created_by` field matching the user's ID. ```php public function scopeOwned(Builder $query): Builder ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.