PHP code example of axn / laravel-eloquent-authorable

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

    

axn / laravel-eloquent-authorable example snippets


Schema::table('posts', function (Blueprint $table) {
    $table->unsignedInteger('created_by')->nullable();
    $table->unsignedInteger('updated_by')->nullable();

    $table->foreign('created_by')
        ->references('id')
        ->on('users');

    $table->foreign('updated_by')
        ->references('id')
        ->on('users');
});

Schema::create('posts', function (Blueprint $table) {
    //...
    $table->addAuthorableColumns();
});

Schema::table('posts', function (Blueprint $table) {
    //...
    $table->dropAuthorableColumns();
});

Schema::create('posts', function (Blueprint $table) {
    //...
    $table->addAuthorableColumns(false);
});

Schema::create('posts', function (Blueprint $table) {
    //...
    $table->addAuthorableColumns(true, App\Models\User::class);
});


use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    //...
}

$post = Post::with('createdBy', 'updatedBy')->first();


use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'users_model' => \App\Admin::class,
        'guard' => 'admin',
    ];

    //...
}


use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'created_by_column_name' => 'custom_created_by',
        'updated_by_column_name' => 'custom_updated_by',
    ];

    //...
}


use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'set_author_when_creating' => false,
        'set_author_when_updating' => false,
    ];

    //...
}


use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'users_model' => \App\Admin::class,
        'guard' => 'admin',
        'created_by_column_name    => 'custom_created_by',
        'set_author_when_creating' => true,
        'updated_by_column_name'   => 'custom_updated_by',
        'set_author_when_updating' => false,
    ];

    //...
}
sh
php artisan vendor:publish --provider="Axn\EloquentAuthorable\ServiceProvider" --tag="config"