think.studio/nova-thinkit

Laravel Nova 小型工具包,用于加快开发。

1.7.1 2023-12-14 14:36 UTC

This package is auto-updated.

Last update: 2024-09-14 16:18:58 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Laravel Nova 小型工具包,用于加快开发。

安装

您可以通过composer安装此包

composer require think.studio/nova-thinkit

# optional publish configs
php artisan vendor:publish --provider="NovaThinKit\ServiceProvider" --tag="config"
# optional publish translations
php artisan vendor:publish --provider="NovaThinKit\ServiceProvider" --tag="lang"

用法

操作

登录到不同的守卫

public function actions(NovaRequest $request)
{
    return [
        (new \NovaThinKit\Nova\Actions\LoginToDifferentGuard(
            route('dashboard.overview'),
            'owners_web',
            __('Login to owner dashboard'),
            __('Are you sure you want to continue?'),
        ))
            // optional callback how to find correct user
            ->findIdUsing(fn (Contact $model) => Owner::query()->where('contact_id', $model->getKey())->first()?->getKey())
            // other default method actions...
            ->canRun(fn ($request, Contact $model) => $model->role === "owner"),
    ];
}

发送重置密码通知

use NovaThinKit\Nova\Actions\LoginToDifferentGuard;

public function actions(NovaRequest $request)
{
    return [
    ( \NovaThinKit\Nova\Actions\SendResetPasswordEmail::make('contacts', __('Send reset password'), __('Are you sure you want to continue?')))
            // optional callback how to find correct user
            ->findIdUsing(fn (Contact $model) => Owner::query()->where('contact_id', $model->getKey())->first()?->getKey())
            // other default method actions...
            ->canRun(fn ($request, Contact $model) => $model->role === "owner"),
    ];
}

过滤器

动态布尔过滤器

\NovaThinKit\Nova\Filters\DynamicBooleanFilter::make([
    'Active' => 'active',
    'Paused' => 'paused',
]  /* options */, 'status' /* column to filter */, 'Status'  /* title */),

// Useful with HumanReadable enums:
\NovaThinKit\Nova\Filters\DynamicBooleanFilter::make(array_flip(CompanyStatus::options()), 'status', 'Status'),

为关系使用过滤器。例如,为作者添加过滤器以筛选帖子

\NovaThinKit\Nova\Filters\DynamicBooleanFilter::make(array_flip(CompanyStatus::options()), 'status', 'Posts Status')->forRelation('posts'),

空或null字段过滤器

\NovaThinKit\Nova\Filters\EmptyFieldFilter::make('post_title' /* column to filter */, 'Title'  /* title */),

为关系使用过滤器。例如,为作者添加过滤器以筛选帖子

\NovaThinKit\Nova\Filters\EmptyFieldFilter::make('posts.post_title', 'Posts Title'),

BelongsTo过滤器

通过相关belongsTo关系进行过滤。

use NovaThinKit\Nova\Filters\BelongsToFilter;

public function filters(NovaRequest $request)
{
    return [
        // type - this is belongsTo() relation method name
        new BelongsToFilter('type'),
        // or
        (new BelongsToFilter('type'))->setTitleKeyName('title'),
        // or
        (new BelongsToFilter('type'))->setFilterName('Filter by type'),
    ];
}

BelongsToMany过滤器

通过相关belongsToMany关系进行过滤。

public function filters(NovaRequest $request)
{
    return [
       \NovaThinKit\Nova\Filters\BelongsToManyFilter::make('tags')
                ->setTitleKeyName('name' /* label key name */)
                ->setFilterName('By tag'),
    ];
}

元数据表

MetaFieldUpdater

如果您有一个包含元值的独立表,您可以使用MetaFieldUpdater从主要资源更快地更新这些值

public function fields(NovaRequest $request)
{
    $metaFieldUpdater = new \NovaThinKit\Nova\Helpers\MetaFieldUpdater(
        'metaData' /* hasMany relation method name */,
        'key' /* key name in neta table */,
        'value' /* data name in neta table */
    );

    return [
        $metaFieldUpdater->field(
            Select::make('University', 'university')->options(University::options())
        ),
        // ALso works with flexible
        $metaFieldUpdater->field(
            Flexible::make('Ethos list', 'cf-numeric_list_with_team')
                ->limit(20)
                ->useLayout(EthosItemLayout::class),
        ),
    ];
}

特色图片功能

向模型添加接口和特质

class Page extends Model implements \NovaThinKit\FeatureImage\Models\WithFeatureImage
{
    use \NovaThinKit\FeatureImage\Models\HasFeatureImage;
    
    // Optionally you can change default storage directory
    public function featureImageManagerDirectory(): string
    {
        return 'page/' . $this->getKey();
    }
    
    // Optionally you can change default image-manager
    protected function createFeatureImageManager(?string $tag = null): ImageManager
    {
    if (!$this->featureImageManager) {
            $this->featureImageManager = FeatureImageManager::fromConfig([
                'disk'                 => 'feature-images',
                'immutableExtensions' => [ '.svg', '.gif' ],
                'original'             => [
                    'methods' => [
                        'fit'      => [ \Spatie\Image\Manipulations::FIT_CROP, 2800, 1800 ],
                        'optimize' => [],
                    ],
                    'srcset'  => '2800w',
                ],
                'deletedFormats'       => [],
                'formats'              => [
                    'thumb' => [
                        'methods' => [
                            'fit'      => [ \Spatie\Image\Manipulations::FIT_CONTAIN, 450, 300 ],
                            'optimize' => [],
                        ],
                        'srcset'  => '450w',
                    ],
                ],
            ]);
        }
        
        if($tag === 'fooBar') {
            $this->featureImageManager->disk = 'baz';
        }

        return $this->featureImageManager;
    }
}

向资源添加特质

class Page extends Resource
{
    use \NovaThinKit\FeatureImage\Nova\HasFeatureImage;
    
    // ... other methods
    
    public function fields(NovaRequest $request)
    {
        return [
            // other fields
            $this->fieldFeatureImage(),
        ];
    }
}

页面模板(基于模板字段的动态字段)

创建模板字段包装器

namespace App\Nova\ResourceTemplates\Pages;

use NovaThinKit\Nova\Helpers\MetaFieldUpdater;
use NovaThinKit\Nova\ResourceTemplates\ResourceTemplate;

class HomePageTemplate extends ResourceTemplate
{
    public function fields(NovaRequest $request): array
    {
        $metaUpdater = new MetaFieldUpdater('meta', 'key', 'value');

        return [
            Text::make('Some Custom text', 'some_custom_text')
                ->hideWhenCreating()
                ->hideFromIndex()
                ->showOnPreview(),
            $metaUpdater->field(
                Text::make('Custom text', 'custom_text')
                    ->hideWhenCreating()
                    ->hideFromIndex()
                    ->showOnPreview(),
            ),
        ];
    }
}

添加映射

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        \NovaThinKit\Nova\ResourceTemplates\TemplateFinder::templatesMap(Page::class, [
            'home'           => HomePageTemplate::class,
            'contact'        => ContactPageTemplate::class,
        ]);
    }
}

最后,更新资源

class Page extends Resource
{
    use \NovaThinKit\Nova\ResourceTemplates\HasTemplate;

    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),

            ...$this->templateFields($request),
        ];
    }
}

鸣谢

  • Think Studio