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.
You can install the package via composer:
composer require digital-creative/custom-relationship-field
- Add the
CustomRelationshipFieldTrait
to the resource (A
) that will be related to, and implement the required: (instance)relationFields
, (static)relationQuery
methods and optional:relationActions
,relationFilters
instance methods on it, whererelation
is the name you choose for your custom relation. - Next, add the
CustomRelationshipField
to any resources (B
) that you want the custom relation to be listed on the details page of.
Example:CustomRelationshipField::make('Relation Label', 'relation', RelatedResourceA::class)
whererelation
is the name you choose for your custom relation. Make sure you use the same relation name passed to the field to name the methods you implemented on the related resource in the first step.RelatedResourceA
is the Nova resource class you are relating to which has theCustomRelationshipFieldTrait
.
In the example below, the related resource is the same one that is being related to.
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 [];
}
}
The MIT License (MIT). Please see License File for more information.