Installation

To install Sphinx, follow the below steps.

sphinx banner

codecov GitHub Workflow Status GitHub top language GitHub release (latest by date) StyleCi

Install the package via composer

composer require hans-thomas/sphinx

Then, publish config file

php artisan vendor:publish --tag sphinx-config

Setting up

Model

First, use Hans\Sphinx\Traits\SphinxTrait trait on your model and then implement the abstract methods. Next, inside your model, make sure to call the hooks method in your booted method.

use SphinxTrait, SphinxTrait {
    SphinxTrait::hooks as private sphinxHooks;
}

protected static function booted() {
    self::sphinxHooks();
}

Auth configuration

First of all, define the provider.

'providers' => [
    // ...
    'sphinxUsers' => [
        'driver' => 'sphinx',
        'model'  => App\Models\User::class,
    ],
    // ...
],

Then, add your guard.

'guards' => [
    // ...
    'jwt' => [
        'driver'   => 'sphinxJwt',
        'provider' => 'sphinxUsers',
    ],
    // ...
],

And finally, you can set the jwt guard as default.

'defaults' => [
    // ...
    'guard'     => 'jwt',
    // ...
],

All sets.

Role model

Sphinx expects the role model of your project, contains requested method on Hans\Sphinx\Models\Contracts\RoleMethods interface class. So, you should implement this contract and for your convenient, there is the Hans\Sphinx\Models\Traits\RoleMethods trait class that contains all the methods that you must implement.

use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract;
use Hans\Sphinx\Models\Traits\RoleMethods;
use Spatie\Permission\Models\Role;

class RoleDelegate extends Role implements RoleContract {
use RoleMethods;

// ...
}

In addition, your role model should have a version column.

$table->unsignedInteger('version')->default(1);

If your using Spatie/laravel-permission package, you can use this migration class.

<?php

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

    return new class extends Migration {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::table(
                $tableName = config('permission.table_names.roles'),
                function (Blueprint $table) use ($tableName) {
                    if (!Schema::hasColumn($tableName, 'version')) {
                        $table->unsignedInteger('version')->default(1);
                    }
                }
            );
        }

        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::table(
                $tableName = config('permission.table_names.roles'),
                function (Blueprint $table) use ($tableName) {
                    if (Schema::hasColumn($tableName, 'version')) {
                        $table->dropColumn('version');
                    }
                }
            );
        }
    };