Skip to content

Emulate HasMany relationship without having a real relationship set between resources

License

Notifications You must be signed in to change notification settings

dcasia/custom-relationship-field

Repository files navigation

Custom Relationship Field

Latest Version on Packagist Total Downloads License

Custom Relationship Field in action

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.