PHP code example of kamansoft / laravel-blame

1. Go to this page and download the library: Download kamansoft/laravel-blame library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kamansoft / laravel-blame example snippets


//config/auth.php

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class, // <-- this is the important part
    ],
],

//dabase/migrations/2023_01_02_154102_create_somes_table

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('somes', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');

            $system_user_id = env('BLAME_SYSTEM_USER_ID');
            $table->unsignedBigInteger('created_by')->default($system_user_id);
            $table->unsignedBigInteger('updated_by')->default($system_user_id);
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('updated_by')->references('id')->on('users');

            $table->timestamps();
        });
    }

//app/Models/SomeModel.php

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Kamansoft\LaravelBlame\Contracts\ModelBlame;
use Kamansoft\LaravelBlame\Traits\ModelBlamer;

class SomeModel extends Model implements ModelBlame
{

    use ModelBlamer; // <-- this is the important part

//database/migrations/2023_01_02_154102_add_blame_fields_to_some_table_name_table.php
return new class extends Migration
{
    public function up()
    {

        Schema::table('some_table_name', function (Blueprint $table) {
            $system_user_id = env('BLAME_SYSTEM_USER_ID');
            $table->unsignedBigInteger(config('blame.created_by_field_name'))->default($system_user_id);
            $table->unsignedBigInteger(config('blame.updated_by_field_name'))->default($system_user_id);
        });
        Schema::table('some_table_name', function (Blueprint $table) {
            $table->unsignedBigInteger(config('blame.created_by_field_name'))->default(null)->change();
            $table->unsignedBigInteger(config('blame.updated_by_field_name'))->default(null)->change();
        });
        Schema::table('some_table_name', function (Blueprint $table) {
            $table->foreign(config('blame.created_by_field_name'))->references('id')->on('users');
            $table->foreign(config('blame.updated_by_field_name'))->references('id')->on('users');
        });
    }
bash
php artisan blame:install
bash
php artisan blame:set:systemuser 
bash
php artisan blame:set:systemuser --key=some_user_id