<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sharifuddin / laravel-smart-model-tracker example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Sharifuddin\LaravelSmartModelTracker\Traits\SmartModelTracker;
class Post extends Model
{
use SmartModelTracker;
protected $fillable = ['title', 'content'];
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Sharifuddin\LaravelSmartModelTracker\Traits\SmartModelTracker;
class Post extends Model
{
use SmartModelTracker, SoftDeletes;
protected $fillable = ['title', 'content'];
}
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');
// Timestamp columns (handled automatically)
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
// User tracking columns
$table->unsignedBigInteger('created_by')->nullable();
$table->unsignedBigInteger('updated_by')->nullable();
$table->unsignedBigInteger('deleted_by')->nullable();
$table->softDeletes();
// Optional: Add foreign key constraints
$table->foreign('created_by')->references('id')->on('users');
$table->foreign('updated_by')->references('id')->on('users');
$table->foreign('deleted_by')->references('id')->on('users');
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
};
// Creating a post (user is authenticated)
$post = Post::create([
'title' => 'My First Post',
'content' => 'Post content...'
]);
echo $post->created_at; // Output: 2023-12-01 10:30:00
echo $post->updated_at; // Output: 2023-12-01 10:30:00
echo $post->created_by; // Output: 1 (current user ID)
echo $post->updated_by; // Output: 1 (current user ID)
// Updating the post
$post->update(['title' => 'Updated Title']);
echo $post->updated_at; // Output: 2023-12-01 10:35:00 (updated)
echo $post->updated_by; // Output: 1 (still current user ID)
// Soft deleting the post
$post->delete();
echo $post->deleted_by; // Output: 1 (current user ID)
echo $post->deleted_at; // Output: 2023-12-01 10:40:00
// Restoring the post
$post->restore();
echo $post->deleted_by; // Output: null (cleared on restore)
$post = Post::first();
// Get the user who created the post
$creator = $post->creator;
// Get the user who last updated the post
$updater = $post->updater;
// Get the user who deleted the post (if soft deleted)
$deleter = $post->deleter;
// Usage in views
<h1>{{ $post->title }}</h1>
<p>Created by: {{ $post->creator->name }}</p>
<p>Last updated by: {{ $post->updater->name }}</p>
@if($post->trashed())
<p>Deleted by: {{ $post->deleter->name }}</p>
@endif
$post = Post::first();
// Formatted timestamps
echo $post->getCreatedAtFormatted('Y-m-d'); // Output: 2023-12-01
echo $post->getUpdatedAtFormatted('M d, Y'); // Output: Dec 01, 2023
// Ownership checks
if ($post->wasCreatedBy($currentUser->id)) {
// User created this post
}
if ($post->wasUpdatedBy($currentUser->id)) {
// User updated this post
}
if ($post->wasDeletedBy($currentUser->id)) {
// User deleted this post
}
// Manual tracking update
$post->touchWithTracking(); // Updates updated_at and updated_by
class Post extends Model
{
use SmartModelTracker;
protected $fillable = [
'title',
'content',
'created_by_user_id', // Custom created_by column
'updated_by_user_id', // Custom updated_by column
'deleted_by_user_id' // Custom deleted_by column
];
}
use Sharifuddin\LaravelSmartModelTracker\Facades\SmartModelTracker;
// Get current tracking information
$userId = SmartModelTracker::getCurrentUserId();
$guard = SmartModelTracker::getCurrentGuard();
$user = SmartModelTracker::getCurrentUser();
// Check tracking status
if (SmartModelTracker::isTrackingEnabled()) {
// Perform tracking operations
}
// Get configuration
$columns = SmartModelTracker::getTrackingColumns();
$config = SmartModelTracker::getConfig();
// Works with web guard (default)
Auth::guard('web')->login($user);
// Works with API guard
Auth::guard('api')->login($user);
// Works with any custom guard
Auth::guard('admin')->login($adminUser);