首页 > 解决方案 > 使用 hasMany 时的 Laravel Nova 404

问题描述

在我的 Laravel Nova 项目中,我有一个 Page 和一个 PageTranslation(模型和资源)。将 hasMany 添加到我的资源字段时,在访问页面的详细信息时,我收到 404 错误。这是我的代码

这是我的页面资源

<?php

namespace App\Pages\Resources;

use Illuminate\Http\Request;
use Laravel\Nova\Resource;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\HasMany;

class Page extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'App\Pages\Models\Page';

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

    /**
     * @var string
     */
    public static $group = 'Pages';

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

    /**
     * Eager load translations
     */
    public static $with = ['translations'];

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

            Text::make('Title', 'working_title')
                ->sortable()
                ->rules('required', 'max:256'),

            HasMany::make('Translations', 'translations', \App\Pages\Resources\PageTranslation::class)

        ];
    }

}

这是我的页面翻译资源

<?php

namespace Codedor\Pages\Resources;

use Illuminate\Http\Request;
use Laravel\Nova\Resource;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;

class PageTranslation extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'Codedor\Pages\Models\PageTranslation';

    /**
     * Hide resource from Nova's standard menu.
     * @var bool
     */
    public static $displayInNavigation = false;

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

            Text::make('Locale')
                ->sortable()
                ->rules('required', 'max:256')
        ];
    }
}

标签: phplaravellaravel-nova

解决方案


我有点晚了,但是如果有人在使用in 方法中Nova::resources的资源路径而不是资源路径时遇到此问题,请确保将相关资源添加到列表中。resourcesNovaServiceProvider

如果您希望从侧边栏导航中隐藏资源,只需public static $displayInNavigation = false;在资源文件中使用


推荐阅读