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

syehan/custom-relationship-field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Relationship Field

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

}

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

Releases

No releases published

Packages

No packages published

Languages

  • PHP 51.7%
  • Vue 36.8%
  • JavaScript 11.5%