Exception Creating New Resource in Laravel Nova - Class 'App\Post' not found

4.6k views Asked by At

I have a brand new installation of Laravel Nova. The dashboard comes up fine. But when I add a new resource using php artisan nova:resource Post and reload the dashboard, it's throwing an error. When I remove the offending model from Nova folder, dashboard works again. I'm following the step by step instructions from the Nova docs exactly. I can't figure it out.

Screenshot Screenshot

navigation.blade.php

@if (count(Nova::availableResources(request())))
    <h3 class="flex items-center font-normal text-white mb-6 text-base no-underline">
        <svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
            <path fill="var(--sidebar-icon)" d="M3 1h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2H3c-1.1045695 0-2-.8954305-2-2V3c0-1.1045695.8954305-2 2-2zm0 2v4h4V3H3zm10-2h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2h-4c-1.1045695 0-2-.8954305-2-2V3c0-1.1045695.8954305-2 2-2zm0 2v4h4V3h-4zM3 11h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2H3c-1.1045695 0-2-.8954305-2-2v-4c0-1.1045695.8954305-2 2-2zm0 2v4h4v-4H3zm10-2h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2h-4c-1.1045695 0-2-.8954305-2-2v-4c0-1.1045695.8954305-2 2-2zm0 2v4h4v-4h-4z"
            />
        </svg>
        <span class="sidebar-label">{{ __('Resources') }}</span>
    </h3>

    @foreach(Nova::groupedResources(request()) as $group => $resources)
        @if (count($resources) > 0)
            @if (count(Nova::groups(request())) > 1)
                <h4 class="ml-8 mb-4 text-xs text-white-50% uppercase tracking-wide">{{ $group }}</h4>
            @endif

            <ul class="list-reset mb-8">
                @foreach($resources as $resource)
                    @if (! $resource::$displayInNavigation)
                        @continue
                    @endif

                    <li class="leading-tight mb-4 ml-8 text-sm">
                        <router-link :to="{
                            name: 'index',
                            params: {
                                resourceName: '{{ $resource::uriKey() }}'
                            }
                        }" class="text-white text-justify no-underline dim">
                            {{ $resource::label() }}
                        </router-link>
                    </li>
                @endforeach
            </ul>
        @endif
    @endforeach
@endif

I see that the Blade is calling @foreach($resources as $resource), which is where I assume the code is failing. The docs say:

"Automatic Registration -- By default, all resources within the app/Nova directory will automatically be registered with Nova. You are not required to manually register them. Before resources are available within your Nova dashboard, you must register them with Nova. Resources get registered in your app/Providers/NovaServiceProvider.php file. This file contains various configuration and bootstrapping code related to your Nova installation."

But when I look at app/Providers/NovaServiceProvider.php there are no resources listed:

<?php
namespace App\Providers;

use Laravel\Nova\Nova;
use Laravel\Nova\Cards\Help;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    public function boot()
    {
        parent::boot();
    }

    protected function gate()
    {
        Gate::define('viewNova', function ($user) {
            return in_array($user->email, [
                //
            ]);
        });
    }

    protected function cards()
    {
        return [
            new Help,
        ];
    }

    public function tools()
    {
        return [];
    }
}

Unfortunately, when I paste in the suggested code for manually registering the resources, it still doesn't work.

<?php

use App\Nova\User;
use App\Nova\Post;

protected function resources()
{
    Nova::resourcesIn(app_path('Nova'));

    Nova::resources([
        User::class,
        Post::class,
    ]);
}
4

There are 4 answers

0
Wael Ben Younes On

You need to create the model before the resource :

php artisan make:model ModelName

then

php artisan make:resource ResourceName
0
Igor On

Yes, the missed up an App\Post model creation and migration with title and body

php artisan make:model Post -m

Also add to posts migration if you are from LaraCasts tutorial

$table->char('title', 100);
$table->text('body');
0
himanshu jain On

First create new Model for post (m for migration)

php artisan make:model Post -m 

After you can create resource in app/nova folder

php artisan nova:resource Post
0
Oleksandr Devhackerone On

Yes, the missed up an App\Post model creation and migration with title and body:

php artisan make:model Post -m

Also add to posts migration if you are from LaraCasts tutorial:

    $table->char('title', 100);
    $table->text('body');

Execute the migrate Artisan command:

php artisan migrate

After you can create resource in App/nova folder:

php artisan nova:resource Post