PHP code example of open-southeners / laravel-model-permalink

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

    

open-southeners / laravel-model-permalink example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OpenSoutheners\LaravelModelPermalink\HasPermalinks;
use OpenSoutheners\LaravelModelPermalink\PermalinkAccess;

class Post extends Model implements PermalinkAccess
{
    use HasPermalinks;

    /**
     * Get permanent link for this model instance.
     */
    public function getPermalink(): string
    {
        // Here is where you return the route that all posts permalinks should use...
        return route('posts.show', $this);
    }
}



use App\Models\Post;
use OpenSoutheners\LaravelModelPermalink\GeneratePermalink;

$post = Post::find(1);

GeneratePermalink::for($post);

// or getting directly the route from returned ModelPermalink object

GeneratePermalink::for($post)->getModelPermalink();

use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use Illuminate\Support\Facades\Gate;

Gate::define('viewModelPermalink', function (?User $user, Model $model) {
    // you can replace this with whatever you like...
    return match (get_class($model)) {
        \App\Models\Post::class => $model->author->is($user),
        \App\Models\User::class => $model->is($user),
        default => false,
    };
});
bash
php artisan vendor:publish --provider="OpenSoutheners\\LaravelModelPermalink\\ServiceProvider"
bash
php artisan migrate