1. Go to this page and download the library: Download atldays/laravel-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/ */
atldays / laravel-hashids example snippets
namespace App\Models;
use Atldays\HashIds\Concerns\HasHashId;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasHashId;
}
namespace App\Models;
use Atldays\HashIds\Concerns\HasHashId;
use Atldays\HashIds\Contracts\HasHashIdModel;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements HasHashIdModel
{
use HasHashId;
}
use Atldays\HashIds\Contracts\HasHashIdModel;
use Illuminate\Database\Eloquent\Model;
/**
* @param Model&HasHashIdModel $model
*/
function exposeHashId(Model $model): ?string
{
return $model->getHashId();
}
namespace App\Models;
use Atldays\HashIds\Concerns\HasHashId;
use Atldays\HashIds\Concerns\HasHashIdRouting;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasHashId;
use HasHashIdRouting;
}
// GET /users/jR8k2PqL
Route::get('/users/{user}', function (User $user) {
return $user;
})->name('users.show');
route('users.show', $user);
use App\Models\User;
use Atldays\HashIds\Rules\HashId;
use Atldays\HashIds\Rules\HashIdExists;
$request->validate([
'user' => ['
$request->validate([
'users' => [''array', new HashIdExists(User::class)],
]);
// lang/vendor/laravel-hashids/en/validation.php
return [
'hash_id' => 'The :attribute must be a valid hash ID.',
'hash_ids' => 'The :attribute must contain only valid hash IDs.',
'hash_id_exists' => 'The selected :attribute is invalid.',
'hash_ids_exist' => 'One or more selected :attribute values are invalid.',
];
namespace App\Http\Requests;
use App\Models\Post;
use App\Models\User;
use Atldays\HashIds\Http\Attributes\HashIdField;
use Atldays\HashIds\Http\Concerns\InteractsWithHashIds;
use Atldays\HashIds\Rules\HashId;
use Illuminate\Foundation\Http\FormRequest;
#[HashIdField('posts', Post::class)]
class IndexPostsRequest extends FormRequest
{
use InteractsWithHashIds;
protected array $hashIdFields = [
'author' => User::class,
];
public function rules(): array
{
return [
'author' => ['nullable', new HashId(User::class)],
'posts' => ['nullable', 'array', new HashId(Post::class)],
];
}
}
namespace App\Models;
use Atldays\HashIds\Concerns\HasHashId;
use Atldays\HashIds\Concerns\SerializesHashId;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasHashId;
use SerializesHashId;
}
$user->toArray();
use Atldays\HashIds\Attributes\HashIdColumn;
#[HashIdColumn('public_id')]
class User extends Model
{
use HasHashId;
}
use Atldays\HashIds\Attributes\HashIdSalt;
#[HashIdSalt('custom-user-salt')]
class User extends Model
{
use HasHashId;
}
use Atldays\HashIds\Attributes\HashIdSaltFromClass;
#[HashIdSaltFromClass]
class User extends Model
{
use HasHashId;
}
use Atldays\HashIds\Attributes\HashIdSaltFromTable;
#[HashIdSaltFromTable]
class User extends Model
{
use HasHashId;
}
use App\Models\User;
use Atldays\HashIds\HashIdRegistry;
HashIdRegistry::make('App\\Old\\Models\\User', User::class);