我正在使用Laravel-Nova-Nested-Form,但是在创建新记录时却遇到了以下相同的错误。任何解决此问题的建议将非常有帮助。
{
"messages" : "The given data was invalid.",
"errors" : {
"answers[0][question]" : ["The question field is required."]
}
}
这可能是由于答案资源中的原因造成的:
BelongsTo::make('Question')
->searchable()
->rules('required'),
删除该部分将使我可以更新答案,但是当我尝试创建答案时,确实会收到另一条错误消息(逻辑):
{
"message": "SQLSTATE[HY000]: General error: 1364 Field 'question_id' doesn't have a default value (SQL: insert into `answers` (`title`, `value`, `status`, `follow_up`, `updated_at`, `created_at`) values (dd, aa, 0, 1, 2019-04-25 07:16:35, 2019-04-25 07:16:35))",
"exception": "Illuminate\\Database\\QueryException",
"file": "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
"line": 664,
...
}
因此,我没有从答案资源中删除该部分,而是添加了一些内容:
BelongsTo::make('Question')
->searchable()
->rules('required')
->hideWhenUpdating(),
这再次允许我更新Answers,但是一旦我想添加新的Answers,我又会收到第一条错误消息(逻辑):
{
"messages" : "The given data was invalid.",
"errors" : {
"answers[0][question]" : ["The question field is required."]
}
}
我确实尝试了更多,但是我始终被困在无法创建新答案的地方。
这是我的Nova资源
<?php
namespace App\Nova;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Select;
use Jackabox\DuplicateField\DuplicateField;
class Answer extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Answer';
/**
* 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', 'title', 'value',
];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
BelongsTo::make('Question')
->searchable()
->rules('required'),
Text::make('title')
->sortable()
->rules('required', 'max:255'),
Text::make('value')
->sortable()
->rules('required', 'max:255'),
Select::make('status')->options([
'0' => 'System',
'1' => 'User',
])
->displayUsingLabels()
->rules('required'),
BelongsTo::make('Follow up', 'follow_up_question', 'App\Nova\Question')
->searchable()
->nullable(),
];
}
/**
* 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 [];
}
}
任何帮助将不胜感激