### Full Migration Example with Tracking Columns Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Full migration example that creates a posts table with all tracking columns using the Blueprint macros. Includes soft deletes and a down method to drop the table. ```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('body')->nullable(); $table->timestamps(); $table->softDeletes(); $table->createdBy(); $table->updatedBy(); $table->deletedBy(); $table->restoredBy(); $table->restoredAt(); }); } public function down(): void { Schema::dropIfExists('posts'); } }; ``` -------------------------------- ### Install Laravel Created By via Composer Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Install the package via Composer. ```bash composer require jeffersongoncalves/laravel-created-by ``` -------------------------------- ### Run tests, coverage, static analysis, and formatting Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Commands to run the test suite, generate coverage, run static analysis with PHPStan, and format code with Pint. ```bash # Run all tests vendor/bin/pest # Run with coverage vendor/bin/pest --coverage # Static analysis vendor/bin/phpstan analyse # Code formatting vendor/bin/pint ``` -------------------------------- ### Publish Configuration File Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Publish the package configuration file. ```bash php artisan vendor:publish --tag="created-by-config" ``` -------------------------------- ### Run Tests Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Run the package test suite. ```bash composer test ``` -------------------------------- ### Package source structure Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Shows the package's source directory layout, including the service provider, model traits (WithCreatedBy, WithUpdatedBy, WithDeletedBy, WithRestoredBy, WithRestoredAt), and query scope classes. ```text src/ CreatedByServiceProvider.php # Registers Blueprint macros Models/ Concerns/ WithCreatedBy.php # Trait: auto-set created_by on creating WithUpdatedBy.php # Trait: auto-set updated_by on creating/updating WithDeletedBy.php # Trait: auto-set deleted_by on deleting WithRestoredBy.php # Trait: auto-set restored_by on restoring WithRestoredAt.php # Trait: auto-set restored_at on restoring Scope/ CreatedByScope.php # Query macros: createdBy(), withCreatedBy() UpdatedByScope.php # Query macros: updatedBy(), withUpdatedBy() DeletedByScope.php # Query macros: deletedBy(), withDeletedBy() RestoredByScope.php # Query macros: restoredBy(), withRestoredBy() ``` -------------------------------- ### Published Configuration File Content Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Default configuration file content showing the guard setting and column name mappings. ```php return [ // The authentication guard used to resolve the current user (null = default guard). 'guard' => null, // The column names used to store the audit information. 'columns' => [ 'created_by' => 'created_by', 'updated_by' => 'updated_by', 'deleted_by' => 'deleted_by', 'restored_by' => 'restored_by', 'restored_at' => 'restored_at', ], ]; ``` -------------------------------- ### Test blueprint macro with Pest Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Pest test verifying the createdBy blueprint macro adds a created_by column. Creates a temporary table, asserts the column exists, then drops the table. ```php use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; it('adds created_by column via macro', function () { Schema::create('test_table', function (Blueprint $table) { $table->id(); $table->createdBy(); }); expect(Schema::hasColumn('test_table', 'created_by'))->toBeTrue(); Schema::dropIfExists('test_table'); }); ``` -------------------------------- ### Add Blueprint macros in CreatedByServiceProvider::packageRegistered() Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Registers Blueprint macros 'approvedBy' and 'dropApprovedBy' if not already defined. The approvedBy macro adds a nullable foreignIdFor column for the user model, and dropApprovedBy drops the column. ```php if (! Blueprint::hasMacro('approvedBy')) { Blueprint::macro('approvedBy', function () { $this->foreignIdFor(config('auth.providers.users.model', User::class), 'approved_by') ->nullable() ->default(null); }); } if (! Blueprint::hasMacro('dropApprovedBy')) { Blueprint::macro('dropApprovedBy', function () { $this->dropColumn('approved_by'); }); } ``` -------------------------------- ### Query Macros for Filtering and Eager Loading Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Query builder macros for filtering by audit columns and eager loading related user relationships. ```php // Filter by the user id stored in the column Post::query()->createdBy($userId)->get(); Post::query()->updatedBy($userId)->get(); Post::query()->deletedBy($userId)->get(); Post::query()->restoredBy($userId)->get(); // Eager load the related user relationship Post::query()->withCreatedBy()->get(); Post::query()->withUpdatedBy()->get(); Post::query()->withDeletedBy()->get(); Post::query()->withRestoredBy()->get(); ``` -------------------------------- ### Create ApprovedByScope in src/Models/Scope/ApprovedByScope.php Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Implements the ApprovedByScope with an empty apply method and an extend method that adds macros 'approvedBy' and 'withApprovedBy' to the builder for filtering and eager loading the approvedBy relationship. ```php namespace JeffersonGoncalves\CreatedBy\Models\Scope; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; class ApprovedByScope implements Scope { public function apply(Builder $builder, Model $model) {} public function extend(Builder $builder): void { $builder->macro('approvedBy', function (Builder $builder, $value) { return $builder->where('approved_by', $value); }); $builder->macro('withApprovedBy', function (Builder $builder) { return $builder->with('approvedBy'); }); } } ``` -------------------------------- ### Combining All Traits in a Model Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Combines all tracking traits in a single model, enabling automatic tracking of created, updated, deleted, restored, and restored_at fields. ```php use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithCreatedBy; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithUpdatedBy; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithDeletedBy; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithRestoredBy; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithRestoredAt; class Post extends Model { use SoftDeletes; use WithCreatedBy, WithUpdatedBy, WithDeletedBy, WithRestoredBy, WithRestoredAt; } ``` -------------------------------- ### Create WithApprovedBy trait in src/Models/Concerns/WithApprovedBy.php Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Defines the WithApprovedBy trait with a boot method that adds a global scope and updates the approved_by field on model updating when approved is dirty and true. Also defines an approvedBy relationship to the user model. ```php namespace JeffersonGoncalves\CreatedBy\Models\Concerns; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Foundation\Auth\User; use JeffersonGoncalves\CreatedBy\Models\Scope\ApprovedByScope; trait WithApprovedBy { public static function bootWithApprovedBy(): void { static::addGlobalScope(new ApprovedByScope); // Choose the appropriate model event static::updating(static function ($model) { if ($model->isDirty('approved') && $model->approved) { $model->approved_by = auth()->id(); } }); } public function approvedBy(): BelongsTo { return $this->belongsTo(config('auth.providers.users.model', User::class)); } } ``` -------------------------------- ### Add Audit Columns to Table Schema Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Add the audit columns to your table schema using the package's blueprint macros. ```php Schema::create('posts', function (Blueprint $table) { $table->createdBy(); $table->updatedBy(); $table->deletedBy(); $table->restoredBy(); $table->restoredAt(); $table->softDeletes(); }); ``` -------------------------------- ### Blueprint Macros: Add and Drop Tracking Columns Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Registers Blueprint macros for adding and dropping tracking columns. The user model for foreignIdFor is resolved from config('auth.providers.users.model') with a fallback to Illuminate\Foundation\Auth\User::class. ```php // Adding columns $table->createdBy(); // foreignIdFor(User, 'created_by')->nullable()->default(null) $table->updatedBy(); // foreignIdFor(User, 'updated_by')->nullable()->default(null) $table->deletedBy(); // foreignIdFor(User, 'deleted_by')->nullable()->default(null) $table->restoredBy(); // foreignIdFor(User, 'restored_by')->nullable()->default(null) $table->restoredAt(); // timestamp('restored_at')->nullable()->default(null) // Dropping columns $table->dropCreatedBy(); // dropColumn('created_by') $table->dropUpdatedBy(); // dropColumn('updated_by') $table->dropDeletedBy(); // dropColumn('deleted_by') $table->dropRestoredBy(); // dropColumn('restored_by') $table->dropRestoredAt(); // dropColumn('restored_at') ``` -------------------------------- ### Add Package Traits to Eloquent Model Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/README.md Add the package traits to your Eloquent model to enable automatic audit field tracking. ```php create(); $this->actingAs($user); Post::create(['title' => 'Test']); $posts = Post::createdBy($user->id)->get(); expect($posts)->toHaveCount(1); }); it('eager loads createdBy relationship', function () { $user = User::factory()->create(); $this->actingAs($user); Post::create(['title' => 'Test']); $post = Post::withCreatedBy()->first(); expect($post->relationLoaded('createdBy'))->toBeTrue() ->and($post->createdBy->id)->toBe($user->id); }); ``` -------------------------------- ### Test unauthenticated created_by with Pest Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Pest test verifying that created_by is null when no user is authenticated. Creates a post without acting as a user and asserts the attribute is null. ```php it('sets null when no user is authenticated', function () { $post = Post::create(['title' => 'Test']); expect($post->created_by)->toBeNull(); }); ``` -------------------------------- ### Test trait auto-assignment with Pest Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Pest tests verifying the trait auto-assigns created_by, updated_by, deleted_by, restored_by, and restored_at on the corresponding model events. Each test creates a user, authenticates them, and asserts the attribute equals the user's id (or is not null for restored_at). ```php use App\Models\Post; use App\Models\User; it('sets created_by on creating', function () { $user = User::factory()->create(); $this->actingAs($user); $post = Post::create(['title' => 'Test']); expect($post->created_by)->toBe($user->id); }); it('sets updated_by on updating', function () { $user = User::factory()->create(); $this->actingAs($user); $post = Post::create(['title' => 'Test']); $post->update(['title' => 'Updated']); expect($post->updated_by)->toBe($user->id); }); it('sets deleted_by on soft deleting', function () { $user = User::factory()->create(); $this->actingAs($user); $post = Post::create(['title' => 'Test']); $post->delete(); expect($post->deleted_by)->toBe($user->id); }); it('sets restored_by and restored_at on restoring', function () { $user = User::factory()->create(); $this->actingAs($user); $post = Post::create(['title' => 'Test']); $post->delete(); $post->restore(); expect($post->restored_by)->toBe($user->id) ->and($post->restored_at)->not->toBeNull(); }); ``` -------------------------------- ### WithRestoredAt Trait: Set restored_at on Restore Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Sets restored_at timestamp when a soft-deleted model is restored. Requires the SoftDeletes trait. Does NOT add query scopes; only sets the timestamp. ```php use Illuminate\Database\Eloquent\SoftDeletes; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithRestoredAt; class Post extends Model { use SoftDeletes, WithRestoredAt; } // Automatic on restore $post->restore(); // restored_at = now() ``` -------------------------------- ### WithUpdatedBy Trait: Set updated_by on Create and Update Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Sets updated_by on both creating and updating events. Provides a relationship and query macros for filtering and eager loading. ```php use JeffersonGoncalves\CreatedBy\Models\Concerns\WithUpdatedBy; class Post extends Model { use WithUpdatedBy; } // Set on create AND update $post = Post::create(['title' => 'My Post']); // updated_by = auth()->id() $post->update(['title' => 'Updated']); // updated_by = auth()->id() // Relationship and query macros $user = $post->updatedBy; Post::updatedBy($userId)->get(); Post::withUpdatedBy()->get(); ``` -------------------------------- ### WithCreatedBy Trait: Auto-set created_by on Create Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Automatically sets created_by to the authenticated user's ID when a model is created. Provides a BelongsTo relationship and query macros for filtering and eager loading. ```php use JeffersonGoncalves\CreatedBy\Models\Concerns\WithCreatedBy; class Post extends Model { use WithCreatedBy; } // Automatic: $post->created_by = auth()->id() on creating $post = Post::create(['title' => 'My Post']); // Relationship $user = $post->createdBy; // BelongsTo relationship // Query macros Post::createdBy($userId)->get(); // Filter by created_by Post::withCreatedBy()->get(); // Eager load createdBy relationship ``` -------------------------------- ### Resolve user model from auth config Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Resolves the user model from Laravel's auth configuration. Used by the package for foreignIdFor and BelongsTo relationships. Falls back to Illuminate\Foundation\Auth\User::class if not set. ```php // The user model used for foreignIdFor and BelongsTo relationships config('auth.providers.users.model') // Fallback: Illuminate\Foundation\Auth\User::class ``` -------------------------------- ### WithRestoredBy Trait: Set restored_by on Restore Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Sets restored_by when a soft-deleted model is restored. Requires the SoftDeletes trait. Provides a relationship and query macros for filtering and eager loading. ```php use Illuminate\Database\Eloquent\SoftDeletes; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithRestoredBy; class Post extends Model { use SoftDeletes, WithRestoredBy; } // Automatic on restore $post->restore(); // restored_by = auth()->id() // Relationship and query macros $user = $post->restoredBy; Post::restoredBy($userId)->get(); Post::withRestoredBy()->get(); ``` -------------------------------- ### WithDeletedBy Trait: Set deleted_by on Soft Delete Source: https://github.com/jeffersongoncalves/laravel-created-by/blob/master/resources/boost/skills/created-by-development/SKILL.md Sets deleted_by when a model is soft-deleted. Requires the SoftDeletes trait. Provides a relationship and query macros for filtering and eager loading. ```php use Illuminate\Database\Eloquent\SoftDeletes; use JeffersonGoncalves\CreatedBy\Models\Concerns\WithDeletedBy; class Post extends Model { use SoftDeletes, WithDeletedBy; } // Automatic on soft delete $post->delete(); // deleted_by = auth()->id() // Relationship and query macros $user = $post->deletedBy; Post::deletedBy($userId)->get(); Post::withDeletedBy()->get(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.