novius/laravel-linkable

用于可链接资源的 Laravel Eloquent 模型特性

1.0.1 2024-08-07 14:13 UTC

This package is auto-updated.

Last update: 2024-09-07 14:21:11 UTC


README

Novius CI Packagist Release License: AGPL v3

介绍

用于管理 Laravel Eloquent 模型可链接资源的包。

提供可链接 Nova 字段。

要求

  • PHP >= 8.2
  • Laravel 10.0

安装

您可以通过 composer 安装此包

composer require novius/laravel-linkable

可选地,您也可以

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

使用方法

Eloquent 模型特性

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();

Nova

如果您使用 Laravel Nova,您现在可以使用可链接字段

<?php

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,
    ],
];

测试

composer run test

CS Fixer

使用 Laravel Pint 对您的代码进行 lint 检查

composer run cs-fix

许可

此包受 GNU Affero General Public License v3 或(根据您的选择)任何后续版本许可。