PHPackages                             novius/laravel-linkable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. novius/laravel-linkable

ActiveLibrary[Database &amp; ORM](/categories/database)

novius/laravel-linkable
=======================

A Laravel Eloquent model trait for linkable resource

2.1.1(2mo ago)05.7k↓46.4%6AGPL-3.0-or-laterPHPPHP &gt;=8.2CI passing

Since Aug 6Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/novius/laravel-linkable)[ Packagist](https://packagist.org/packages/novius/laravel-linkable)[ Docs](https://github.com/novius/laravel-linkable)[ RSS](/packages/novius-laravel-linkable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (20)Versions (15)Used By (6)

Laravel Linkable
================

[](#laravel-linkable)

[![Novius CI](https://github.com/novius/laravel-linkable/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/novius/laravel-linkable/actions/workflows/main.yml)[![Packagist Release](https://camo.githubusercontent.com/27d176bf4316546ec9f8717e9c94ff5fe75894e7dd68c1a91cc7ad9a0a7c2288/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6f766975732f6c61726176656c2d6c696e6b61626c652e7376673f6d61784167653d31383030267374796c653d666c61742d737175617265)](https://packagist.org/packages/novius/laravel-linkable)[![License: AGPL v3](https://camo.githubusercontent.com/c61341f63648cdd5aba4f7a073b513106a63778c27b15f96c56157642bc943b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4147504c25323076332d626c75652e737667)](http://www.gnu.org/licenses/agpl-3.0)

Introduction
------------

[](#introduction)

A package to manage Laravel Eloquent models linkable.

Provide a Linkable Nova Field.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.2
- Laravel 10.0

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require novius/laravel-linkable
```

Optionally, you can also:

```
php artisan vendor:publish --provider="Novius\LaravelLinkable\LaravelLinkableServiceProvider" --tag=config
php artisan vendor:publish --provider="Novius\LaravelLinkable\LaravelLinkableServiceProvider" --tag=lang
```

Usage
-----

[](#usage)

### Eloquent Model Trait

[](#eloquent-model-trait)

```
namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Novius\LaravelLinkable\Configs\LinkableConfig;
use Novius\LaravelLinkable\Traits\Linkable;

class Post extends Model {
    use Linkable;
    ...

    public function linkableConfig(): LinkableConfig
    {
        return new LinkableConfig(
            // To retrieve an instance url, you can define the getUrlCallback
            getUrlCallback: function (Model $model, array $extraParameters = []) {
                return route('post_route', [
                    ...$extraParameters,
                    'post' => $model,
                ], true, $model->locale);
            },

            // Or you can just define the route name and the route name parameter
            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/Filament field
            optionGroup: 'My model', // Required. The name of the group of the model in the Linkable Nova/Filament field
            optionSearch: ['title', 'slug'], // Optional. The columns used for searching in the Linkable Nova/Filament field. Default to optionLabel if it is a string.
            optionsQuery: function (Builder $query) { // Optional. To modify the default query to populate the Linkable Nova/Filament field
                $query->published();
            },
            resolveQuery: function (Builder $query) { // Optional. The base query to resolve the model binding
                $query->where('locale', app()->getLocale());
            },
            resolveNotPreviewQuery: function (Builder $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
        );
    }
```

Now you can do that:

```
// In your route file, if you choose to work with `routeName` and `routeParameterName`
Route::get('post/{post}', function (Post $post) {
    // ...
})->name('post_route');

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

### Nova

[](#nova)

If you use Laravel Nova, you can now use the Linkable field :

```
