php - 使用 hasMany 时的 Laravel Nova 404
问题描述
在我的 Laravel Nova 项目中,我有一个 Page 和一个 PageTranslation(模型和资源)。将 hasMany 添加到我的资源字段时,在访问页面的详细信息时,我收到 404 错误。这是我的代码
这是我的页面资源
<?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)
];
}
}
这是我的页面翻译资源
<?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')
];
}
}
解决方案
我有点晚了,但是如果有人在使用in 方法中Nova::resources
的资源路径而不是资源路径时遇到此问题,请确保将相关资源添加到列表中。resources
NovaServiceProvider
如果您希望从侧边栏导航中隐藏资源,只需public static $displayInNavigation = false;
在资源文件中使用
推荐阅读
- php - Doctrine Array 类型字段未更新
- algorithm - 算法问题:找到运送所有包裹的最少旅行次数
- python - Flask-SQLAlchemy:更改 raspberryPie 的 sd 卡后出现“AttributeError:无法设置属性”
- linux - 将引用的命令管道作为函数参数传递(并执行)
- slack - 如何使用 slack web api users.profile.set 设置用户自定义配置文件的单个字段而不清除其他现有自定义字段值?
- android - 如何使用 Google Play In-App Review API 了解已完成应用评分的用户?
- python - 对已关闭文件的 I/O 操作。在 Python 3.7 中将数据写入 excel 文件时出错
- mysql - 只有当它具有NULL值时才将数据插入到列中,否则更新它
- python - 如何按值合并两个熊猫数据框?
- amazon-web-services - 设计帮助:拆分要由 lambda 处理的作业:组合回来的问题