PHP code example of pmochine / laravel-nova-hashids

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

    

pmochine / laravel-nova-hashids example snippets


   use Pmochine\LaravelNovaHashids\LaravelNovaHashids;

   public function cards(): array
   {
       return [
           new LaravelNovaHashids,
       ];
   }
   

(new LaravelNovaHashids)->connection('users'),

use Laravel\Nova\Http\Requests\NovaRequest;
use Pmochine\LaravelNovaHashids\LaravelNovaHashids;

public function cards(NovaRequest $request): array
{
    return [
        (new LaravelNovaHashids)->connection('users')->onlyOnDetail(),
    ];
}

use App\Support\SqidsConverter;
use Pmochine\LaravelNovaHashids\Contracts\Converter;

public function register(): void
{
    $this->app->bind(Converter::class, SqidsConverter::class);
}

namespace App\Support;

use Pmochine\LaravelNovaHashids\Contracts\Converter;
use Sqids\Sqids;

class SqidsConverter implements Converter
{
    public function __construct(protected Sqids $sqids = new Sqids(minLength: 8))
    {
    }

    public function connections(): array
    {
        return ['sqids'];
    }

    public function defaultConnection(): ?string
    {
        return 'sqids';
    }

    public function encode(string $connection, string $modelId): ?string
    {
        // Sqids encodes PHP integers. This check rejects ids above PHP_INT_MAX.
        $id = filter_var($modelId, FILTER_VALIDATE_INT);

        return $id === false ? null : $this->sqids->encode([$id]);
    }

    public function decode(string $connection, string $hashId): ?string
    {
        $numbers = $this->sqids->decode($hashId);

        // Sqids decodes some ids that it never creates. Encode again to reject them.
        if (count($numbers) !== 1 || $this->sqids->encode($numbers) !== $hashId) {
            return null;
        }

        return (string) $numbers[0];
    }
}
bash
   php artisan vendor:publish --provider="Vinkla\Hashids\HashidsServiceProvider"