digital-creative/custom-relationship-field

Emulate HasMany relationship without having a real relationship set between resources

v1.1.3 2024-07-18 12:26 UTC

This package is auto-updated.

Last update: 2024-12-18 13:24:29 UTC


README

Latest Version on Packagist Total Downloads License

This field works just like as the default HasMany relationship field from nova but without requiring a real relation with the resource.

That means you are free to show resource A into the details page of resource B without having to create a real relation between them.

Installation

You can install the package via composer:

composer require digital-creative/custom-relationship-field
use DigitalCreative\CustomRelationshipField\CustomRelationshipField;
use DigitalCreative\CustomRelationshipField\CustomRelationshipFieldTrait;

trait UserWithSimilarNameTrait
{    
    public static function similarNameQuery(NovaRequest $request, Builder $query, User $model): Builder
    { 
        return $query->where('last_name', $model->last_name)->whereKeyNot($model->getKey());
    }
    
    public function similarNameFields(NovaRequest $request): array
    {
        return [
            ID::make(),
            Text::make('First Name'),
            Text::make('Last Name'),
        ];
    }
    
    public function similarNameActions(NovaRequest $request): array 
    {
        return [];
    }

    public function similarNameFilters(NovaRequest $request): array
    {
        return [];
    }
}

class User extends Resource
{    
    use CustomRelationshipFieldTrait;
    use UserWithSimilarNameTrait;
    
    public function fields(NovaRequest $request): array
    {
        return [
            ...
            CustomRelationshipField::make('Users with similar name', 'similarName', User::class),
            ...
        ];
    }
}

⭐️ Show Your Support

Please give a ⭐️ if this project helped you!

Other Packages You Might Like

License

The MIT License (MIT). Please see License File for more information.