PHP code example of friendsofhyperf / model-hashids
1. Go to this page and download the library: Download friendsofhyperf/model-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/ */
friendsofhyperf / model-hashids example snippets
use Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
use FriendsOfHyperf\ModelHashids\Concerns\HashidRouting;
Class Item extends Model
{
use HasHashid, HashidRouting;
}
// Generating the model hashid based on its key
$item->hashid();
// Equivalent to the above but with the attribute style
$item->hashid;
// Finding a model based on the provided hashid or
// returning null on failure
Item::findByHashid($hashid);
// Finding a model based on the provided hashid or
// throwing a ModelNotFoundException on failure
Item::findByHashidOrFail($hashid);
// Decoding a hashid to its equivalent id
$item->hashidToId($hashid);
// Encoding an id to its equivalent hashid
$item->idToHashid($id);
// Getting the name of the hashid connection
$item->getHashidsConnection();
use Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
class Item extends Model
{
use HasHashid;
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['hashid'];
}
use Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
use FriendsOfHyperf\ModelHashids\Concerns\HashidRouting;
class Item extends Model
{
use HasHashid, HashidRouting;
}
Route::get('/items/{item:slug}', function (Item $item) {
return $item;
});
use Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
use FriendsOfHyperf\ModelHashids\Concerns\HashidRouting;
class Item extends Model
{
use HasHashid, HashidRouting;
public function getRouteKeyName()
{
return 'slug';
}
public function getRouteKey()
{
return $this->slug;
}
}
Route::get('/items/{item:hashid}', function (Item $item) {
return $item;
});
Route::get('/items/{item}', function (Item $item) {
return $item;
})->withTrashed();
Route::get('/user/{user}/items/{item}', function (User $user, Item $item) {
return $item;
})->scopeBindings();