PHP code example of danielsundermeier / laravel-model-path

1. Go to this page and download the library: Download danielsundermeier/laravel-model-path 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/ */

    

danielsundermeier / laravel-model-path example snippets


Model::indexPath(); // index, store

$model->index_path; // index, store
$model->create_path; // create
$model->path; // show, update, delete
$model->edit_path; // edit

Route::resource('movies', App\Http\Controllers\Movies\MovieController::class);

use D15r\ModelPath\Traits\HasModelPath;

class Movie extends Model
{
    use HasFactory, HasModelPath;

    const ROUTE_NAME = 'movies';
}

Movie::indexPath(); // /movies

$movie = Movie::find(1);

$movie->index_path; // /movies
$movie->create_path; // /movies/create
$movie->path; // /movies/1
$movie->edit_path; // /movies/1/edit

Route::resource('{type}/{model}/watched', App\Http\Controllers\Watched\WatchedController::class);

use D15r\ModelPath\Traits\HasModelPath;

class Watched extends Model
{
    use HasFactory, HasModelPath;

    const ROUTE_NAME = 'watched';

    public function getRouteParameterAttribute() : array
    {
        return [
            'type' => $this->watchable_type::ROUTE_NAME,
            'model' => $this->watchable_id,
            'watched' => $this->id,
        ];
    }
}

Watched::indexPath([
    'watchable_type' => Movie::class,
    'watchable_id' => 1
]); // /movies/1/watched

$watched = Watched::find(1);

$watched->index_path; // /movies/1/watched
$watched->create_path; // /movies/1/watched/create
$watched->path; // /movies/1/watched/1
$watched->edit_path; // /movies/1//watched/1/edit