PHP code example of novius / laravel-linkable

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

    

novius / laravel-linkable example snippets


namespace App\Models;

use \Illuminate\Database\Eloquent\Model;
use Novius\LaravelLinkable\Traits\Linkable;

class Post extends Model {
    use Linkable;
    ...

    public function getLinkableConfig(): LinkableModelConfig
    {
        if (! isset($this->linkableConfig)) {
            $this->linkableConfig = new LinkableModelConfig(
                routeName: 'post_route',
                routeParameterName: 'post',
                optionLabel: 'title', // Required. A field of your model or a closure (taking the model instance as parameter) returning a label. Use to display a model instance in the Linkable Nova field
                optionGroup: 'My model', // Required. The name of the group of the model in the Linkable Nova field
                optionsQuery: function (Builder|Page $query) { // Optional. To modify the default query to populate the Linkable Nova field  
                    $query->published();
                },
                resolveQuery: function (Builder|Page $query) { // Optional. The base query to resolve the model binding
                    $query->where('locale', app()->currentLocale());
                },
                resolveNotPreviewQuery: function (Builder|Page $query) { // Optional. The query to resolve the model binding when not in preview mode
                    $query->published();
                },
                previewTokenField: 'preview_token' // Optional. The field that contains the preview token of the model 
            );
        }

        return $this->linkableConfig;
    }


// In your routes file
Route::get('post/{post}', function (Post $post) {
    // ...
})->name('post_route');

$post = Post::first();
$post->url();
$post->previewUrl();



use Novius\LaravelLinkable\Nova\Fields\Linkable;

class MyResource extends Resource
{
    public static $model = \App\Models\MyResource::class;

    public function fields(NovaRequest $request)
    {
        return [
            // ...

            Linkable::make('Link', 'link'),
        ];
    }
}


use Novius\LaravelLinkable\Facades\Linkable;

$model = MyResource::first();
echo Linkable::getLink($model->link);

return [
    // Laravel Linkable will autoload all Model using Linkable trait in this directory
    'autoload_models_in' => app_path('Models'),

    // The guard name to preview model, without using the preview token
    'guard_preview' => null,

    /*
     * Sometimes you need to link items that are not objects.
     *
     * This config allows you to link routes.
     *     For instance: 'contact' => 'page.contact'
     *
     * "contact" will be the parameter of the laravel function route().
     * "page.contact" will be the parameter of the laravel function trans().
     */
    'linkable_routes' => [
        'my_route' => 'My route',    
    ],

    /*
     * If you want to add specifics models using the Linkable trait that are not in your autoload directory
     */

    'linkable_models' => [
        Provider\Package\Models\Model::class,
    ],
];
bash
php artisan vendor:publish --provider="Novius\LaravelLinkable\LaravelLinkableServiceProvider" --tag=config
php artisan vendor:publish --provider="Novius\LaravelLinkable\LaravelLinkableServiceProvider" --tag=lang