对于某些模型,当我为它们创建新的 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
是否为此模型设置了策略。然后在您的策略类(可能是绑定(bind)到 Product
模型的 ProductPolicy
上),检查 view
和 viewAny
方法,这些方法必须返回 true
或条件 true
。
关于php - Laravel Nova 找不到某些模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64048555/