PHP code example of codezero / laravel-route-key-exists

1. Go to this page and download the library: Download codezero/laravel-route-key-exists 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/ */

    

codezero / laravel-route-key-exists example snippets


public function getRouteKeyName()
{
    return 'id';
}

public function getRouteKey()
{
    // "encode" the route key
    return "foo-{$this->id}";
}

public function resolveRouteBinding($value)
{
    // "decode" the route key
    $id = (int) str_replace('foo-', '', $value);

    // resolve from the database
    return $this->where('id', $id)->first();
}

request()->validate([
    'model_id' => RouteKeyExists::model(Model::class),
]);

request()->validate([
    'model_id' => RouteKeyExists::model(Model::class)->replace(),
]);

$id = request('model_id'); // actual ID

request()->validate([
    'model' => RouteKeyExists::model(Model::class)->replace('model_id'),
]);

$id = request('model_id'); // actual ID
//$id = request('model'); // null

request()->validate([
    'model_id' => RouteKeyExists::model(Model::class)->add('actual_id'),
]);

$id = request('actual_id'); // actual ID
$key = request('model_id'); // route key