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

littlemiaor/custom-relationship-field

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Relationship Field

Latest Version on Packagist Total Downloads License

Laravel Nova 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 they having a real bound though the standard relationship in laravel.

Installation

You can install the package via composer:

composer require digital-creative/custom-relationship-field

Next add the CustomRelationshipField to the resource you wanna display the relation.

use DigitalCreative\CustomRelationshipField\CustomRelationshipField;
use DigitalCreative\CustomRelationshipField\CustomRelationshipFieldTrait;

class Client extends Resource
{
    
    use CustomRelationshipFieldTrait;
   
    public function fields()
    {
        return [
            Text::make('Name')->rules('required'),
            // ...
            CustomRelationshipField::make('Clients with similar name', 'similarName', self::class),
        ];
    }

    public function similarNameFields(): array
    {
        return [
            ID::make()->sortable(),
            Text::make('Name'),
            Text::make('Age'),
            //...
        ];
    }

    public static function similarNameQuery(NovaRequest $request, $query, Model $model)
    { 
        return $query->where('name', 'SOUNDS LIKE', $model->name)->whereKeyNot($model->getKey());
    }
    
    public function similarNameActions(NovaRequest $request) 
    {
        return [];
    }

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

}

License

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

About

Emulate HasMany relationship without having a real relationship set between resources

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 50.0%
  • Vue 44.0%
  • JavaScript 6.0%