PHP code example of testmonitor / laravel-accountable

1. Go to this page and download the library: Download testmonitor/laravel-accountable 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/ */

    

testmonitor / laravel-accountable example snippets


use TestMonitor\Accountable\Accountable;

class CreateProjectsTable extends Migration
{
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');

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

            // This will add the 

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use TestMonitor\Accountable\Traits\Accountable;

class Project extends Model
{
    use Accountable, SoftDeletes;

    protected $table = 'projects';
}

$project = new Project(['name' => 'Awesome project']);
$project->save();

// Show the name of user that created the project
echo $project->creator->name;

$user = User::findOrFail(42);

// Get all projects created by user with id 42
Project::onlyCreatedBy($user)->get();

// Get the user that created the model
$model->created_by_user_id;
$model->creator->name;

// Get the user that last updated the model
$model->updated_by_user_id;
$model->editor->name;

// Get the user that last deleted the model
$model->deleted_by_user_id;
$model->deleter->name;

// Retrieve the models either created, updated,
// or deleted by $user.
Model::onlyCreatedBy($user)->get();
Model::onlyUpdatedBy($user)->get();
Model::onlyDeletedBy($user)->get();

// And one extra: get all models that were created
// by the currently authenticated user.
Model::mine()->get();

$project = new Project(['name' => 'Do not track me']);
$project->disableUserLogging()->save();

accountable()->actingAs($event->causer);