Skip to content

Relatable attribute methods not triggering #5608

@feeh27

Description

@feeh27
  • Laravel Version: 10.13.1
  • Nova Version: 4.25.1
  • PHP Version: 8.2.6
  • Database Driver & Version: SQLite
  • Operating System and Version: Windows 11 Pro 64 bits
  • Browser type and version: Google Chrome - Version 114.0.5735.110
  • Reproduction Repository: https://github.com/feeh27/nova-issues-5608

Description:

Relatable attribute methods are not working, I need to filter the belongsTo options, because I have a field called "parent" with self-reference and when I edit a resource, I don't want it to appear as an option.

Official doc: https://nova.laravel.com/docs/4.0/resources/authorization.html#relatable-filtering

I have the following resource classes and none of the relatableParent or relatableParents methods are called:

Resource:

// Fields

public function fields(NovaRequest $request)
{
    return [
        ID::make()->sortable(),

        BelongsTo::make('Parent', 'parent', static::class)
            ->sortable()
            ->nullable()
            ->withoutTrashed(),

        Text::make('Name')
            ->sortable()
            ->rules('required'),

        Select::make('Condition')
            ->sortable()
            ->rules(['required', 'in:and,or'])
            ->displayUsingLabels()
            ->options(fn () => [
                ['value' => 'and', 'label' => 'And'],
                ['value' => 'or', 'label' => 'Or'],
            ]),

        Select::make('Operator')
            ->sortable()
            ->rules(['required', 'in:eq,ne,in'])
            ->displayUsingLabels()
            ->options(fn () => [
                ['value' => 'eq', 'label' => 'Equals'],
                ['value' => 'ne', 'label' => 'Not Equals'],
                ['value' => 'in', 'label' => 'In'],
            ]),

        Text::make('Field')
            ->sortable()
            ->rules('required'),

        Text::make('Value')
            ->sortable()
            ->rules('required'),
    ];
}

// Relatable attribute methods

public static function relatableParent(NovaRequest $request, $query)
{
    $resourceId = $request->route('resourceId');

    return $query->whereNotIn('id', [$resourceId]);
}

public static function relatableParents(NovaRequest $request, $query)
{
    $resourceId = $request->route('resourceId');

    return $query->whereNotIn('id', [$resourceId]);
}

Model:

<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class QueryBuilder extends Model
{
    use SoftDeletes;

    protected $table = 'query_builder';

    protected $fillable = [
        'parent_id',
        'name',
        'condition',
        'operator',
        'value',
    ];

    public function parent(): BelongsTo
    {
        return $this->belongsTo(
            static::class,
            'parent_id',
            'id',
        );
    }
}

Even with the methods configured according to the documentation, the method is not called and the "Filter 3" record is still available for selection, as shown in the image below:

image

Detailed steps to reproduce the issue on a fresh Nova installation:

  • Login
  • Go to Query Builder page
  • Create one or more records
  • Select a record and go to its edit page
  • Click in Parent field and all records will be present

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions