首页 > 解决方案 > 在 Laravel Nova 中创建新资源的异常 - 找不到类“App\Post”

问题描述

我有一个全新的 Laravel Nova 安装。仪表板很好。但是,当我使用并重新加载仪表板添加新资源时php artisan nova:resource Post,它会引发错误。当我从 Nova 文件夹中删除有问题的模型时,仪表板再次工作。我完全按照 Nova 文档中的分步说明进行操作。我想不通。

截屏 截屏

导航.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

我看到 Blade 正在调用@foreach($resources as $resource),这是我假设代码失败的地方。文档说:

“自动注册——默认情况下,app/Nova 目录中的所有资源都会自动注册到 Nova。您不需要手动注册它们。在您的 Nova 仪表板中可用资源之前,您必须向 Nova 注册它们。资源获取在您的 app/Providers/NovaServiceProvider.php 文件中注册。该文件包含与您的 Nova 安装相关的各种配置和引导代码。

但是当我查看app/Providers/NovaServiceProvider.php没有列出资源时:

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

不幸的是,当我粘贴建议的代码以手动注册资源时,它仍然不起作用。

<?php

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

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

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

标签: phplaravellaravel-routinglaravel-novalaravel-resource

解决方案


是的,错过了带有标题和正文的 App\Post 模型创建和迁移:

php artisan make:model Post -m

如果您来自 LaraCast 教程,还可以添加到帖子迁移:

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

执行迁移 Artisan 命令:

php artisan migrate

在 App/nova 文件夹中创建资源后:

php artisan nova:resource Post

推荐阅读