Miguel Stevens
Miguel Stevens

Reputation: 9220

Laravel Nova 404 when using hasMany

In my Laravel Nova project, I have a Page and a PageTranslation (model and resource). When adding a hasMany to my Resource fields, upon visiting the detail of the Page, I get a 404 error. This is my code

This is my Page Resource

<?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)

        ];
    }

}

This is my PageTranslation Resource

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

Upvotes: 2

Views: 2151

Answers (2)

Usama Munir
Usama Munir

Reputation: 635

It's not related to relationships at all. Make sure you've included the resources in your NovaServiceProvider Also, to restrict from viewing in the sidebar based on user role, you can do something like:

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

Upvotes: 0

Mohammed Radwan
Mohammed Radwan

Reputation: 534

I'm a little bit late, but if anyone comes across this issue while using Nova::resources instead of the resources path inside resources method in NovaServiceProvider, make sure you add the related resource to the list.

If you wish to hide a resource from the sidebar navirgation, just use public static $displayInNavigation = false; inside the resource file

Upvotes: 12

Related Questions