使用hasMany时的Laravel Nova 404

时间:2019-02-19 08:04:47

标签: php laravel laravel-nova

在我的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)

        ];
    }

}

这是我的PageTranslation资源

<?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')
        ];
    }
}

2 个答案:

答案 0 :(得分:2)

我来晚了一点,但是如果有人在使用Nova::resources而不是resources的{​​{1}}方法内的资源路径时遇到此问题,请确保添加相关内容列表中的资源。

如果您想从边栏导航中隐藏资源,只需在资源文件中使用NovaServiceProvider

答案 1 :(得分:0)

它根本与关系无关。确保您已将资源包括在NovaServiceProvider中 此外,要根据用户角色限制在边栏中查看,可以执行以下操作:

 public static function availableForNavigation(Request $request)
 {
   return $request->user()->isAdmin();
 }