Skip to content

Search for relationship fails with a 404 when a MorphTo field when it's added to a Pivot #5173

@danutavadanei

Description

@danutavadanei
  • Laravel Version: v8.83.27
  • Nova Version: v4.19.5
  • PHP Version: 8.1.13
  • Database Driver & Version: MySQL 8.0
  • Operating System and Version: Docker PHP FPM 8.1
  • Browser type and version: Firefox Latest
  • Reproduction Repository: https://github.com/###/###

Description:

I have a relationship defined in a Pivot model. I then add it as a field to the nova relationship. When I try to search in the interface for a related model it fails with a 404.

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

Have two basic models:

<?php

namespace App\Models;

use App\Models\Pivots\CampaignProfile;
use Illuminate\Database\Eloquent\Model;
class Campaign extends Model
{
    protected $guarded = [];

    public function profiles(): BelongsToMany
    {
        return $this->belongsToMany(Profile::class)
            ->using(CampaignProfile::class);
    }
}
<?php

namespace App\Models;

use App\Models\Pivots\CampaignProfile;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
    protected $guarded = [];

    public function campaigns(): BelongsToMany
    {
        return $this->belongsToMany(Campaign::class)
            ->using(CampaignProfile::class);
    }
}

Add a pivot model:

<?php

namespace App\Models\Pivots;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class CampaignProfile extends Pivot
{
    public $incrementing = true;

    public function participant(): MorphTo
    {
        return $this->morphTo(__FUNCTION__, 'participant_type', 'participant_id');
    }
}

In any of the two resources, define the field with the pivot relationship:

<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphTo;
use Laravel\Nova\Fields\BelongsToMany;
use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Http\Requests\NovaRequest;

class Campaign extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<\App\Models\Campaign>
     */
    public static $model = \App\Models\Campaign::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),

            BelongsToMany::make('Profiles')->fields(function () {
                return [
                    MorphTo::make('Participant')->types([
                        User::class,
                        Team::class,
                    ])->required()->searchable(),
                ];
            }),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function lenses(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [];
    }
}

I have resources for both User and Team and I used the MorphTo field before (not behind a pivot).

The error is:
image

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