Laravel Nova 找不到某些模型

Laravel Nova doesn't find some Models

对于某些模型,当我为它们创建新的 Nova 资源时,Nova 似乎找不到模型,因为它们没有显示在侧边栏上(我也无法通过 URL 找到它们,给我一个 404)。 但这仅适用于特定模型,如果我尝试用另一个模型修改资源中的目标模型(编辑 $model 变量),它会起作用并在侧边栏中显示资源(但模型错误)。 Nova 没有向我抛出任何错误,因此调试变得非常困难。

在我的项目中不起作用的模型被命名为“产品”和“公司”。

我在 Homestead 中使用 Laravel 7.28.3、Nova 3.9.1、MariaDB 10.4.11 和 PHP 7.4.1。

产品资源代码如下:

<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;

class Product extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Product::class;

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

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

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

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

这是模型代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class Product extends Model implements HasMedia
{
    use InteractsWithMedia;

    public function visits()
    {
        return visits($this);
    }

    public function user() {
        return $this->belongsTo('App\User');
    }

    public function company() {
        return $this->belongsTo('App\Company');
    }

    public function productVariety() {
        return $this->belongsTo('App\ProductVariety', 'product_variety_id');
    }

    public function productSpecies() {
        return $this->belongsTo('App\ProductSpecies', 'product_species_id');
    }

    public function productNutrients() {
        return $this->hasMany('App\ProductNutrient');
    }

    public function baseProduct() {
        return $this->hasOne('App\Product', 'base_product_id');
    }

    public function recipes() {
        return $this->hasMany('App\Recipe', 'base_product_id');
    }

    protected $fillable = [
        'user_id', 'company_id', 'imageline_id', 'products_species_id', 'products_varieties_id', 'base_product_id',
        'name', 'scientific_name', 'production_start', 'production_end', 'production_city', 'description', 'story', 'curiosities', 'advices', 'quantity_advices', 'why_good', 'who_good',
        'is_base_product', 'show_related_recipes', 'show_related_products'
    ];

}

检查 app/Providers/AuthServiceProvider.php 上的 AuthServiceProvider 是否为此型号设置了 政策。然后在你的策略 class(可能是 ProductPolicy 绑定到 Product 模型,检查 viewviewAny 方法,这些方法必须 return true 或条件 true.