PHP code example of perfect-drive / referable

1. Go to this page and download the library: Download perfect-drive/referable 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/ */

    

perfect-drive / referable example snippets


return [
    /*
     * The directories to search for Referable classes.
     */
    'directories' => [
        app_path('Enums'),
        app_path('Models'),
    ],

    /*
     * The middleware array to use for the Referable routes.
     */
    'middleware' => ['api', 'auth:sanctum'],

    /*
     * The key name to use for the referable key in the json response.
     */
    'key_name' => 'value',

    /*
     * The value name to use for the referable value in the json response.
     */
    'value_name' => 'title',

    /*
     * The base url for the referable routes.
     */
    'base_url' => 'spa/referable/',
];



declare(strict_types=1);

namespace App\Models;

use App\Interfaces\ReferableInterface;
use App\Traits\ReferableModel;
use Illuminate\Database\Eloquent\Model;

class ProjectType extends Model implements ReferableInterface
{
    use ReferableModel;



declare(strict_types=1);

namespace App\Enums\User;

use App\Interfaces\ReferableInterface;
use App\Traits\ReferableEnum;

enum UserStatus: string implements ReferableInterface
{
    use ReferableEnum;
bash
php artisan vendor:publish --tag="referable-config"

#[ReferableScope]
public function scopeActive(Builder $query): Builder
{
    return $query->where('active', true);
}

#[ReferableScope]
public function active(): bool
{
    return in_array($this, [
        self::FIRST,
        self::SECOND,
    ], true);
}