PHP code example of intervention / eloquent-hashid

1. Go to this page and download the library: Download intervention/eloquent-hashid 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/ */

    

intervention / eloquent-hashid example snippets


use Intervention\EloquentHashid\HasHashid;

class Item extends Model
{
    use HasHashid;
}

$item = App\Models\Item::find(123);

// access hashid attribute
$hashid = $item->hashid

// query model with scope
$item = App\Models\Item::hashid('Ma93ka')->firstOrFail();

// scope can also select multiple items by array
$item = App\Models\Item::hashid(['Ma93ka', 'Op92ae'])->get();

// scope query in one call
$item = App\Models\Item::findByHashid('Ma93ka');

// scope query in one call, throw exception if no model was found
$item = App\Models\Item::findByHashidOrFail('Ma93ka');

use App\Models\Item;

Route::get('/items/{item:hashid}', function (Item $item) {
    return $item;
});