我正在创建调查,为了创建问题,我需要从调查表中获取survey_id
并将其放置在表单中的隐藏字段中创建问题视图,并将其传递给问题控制器。
答案 0 :(得分:0)
由于您通过了survey_id之后,您所要做的就是在store方法中使用正确的模型(问题),其他所有内容都正确。
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = $request->all();
Question::create($input); //<==== You need to use Question insteed of Survey
return redirect('survey');
}
答案 1 :(得分:0)
您可以尝试以下代码,希望对您有所帮助:
$input = $request->all();
$survey = new Survey ();
$survey ->fill($input)->save();
if (isset($input['survey_id']) && !empty($input['survey_id'])) {
$survey_id= $input['survey_id'];
foreach ($survey_id as $index => $value) {
Question::create([
'survey_id' => $survey ->id,
'title' => $value
]);
}
}
答案 2 :(得分:0)
@Rachid的评论是正确的,但这不是错误的原因...
问题在于资源的route -> controler@action
映射不包含/create
路由的任何参数,正如我们看到的那样……
...,并且您的QuestionController::create
函数需要参数$id
。
//...
class QuestionController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($id) // <---- Right here...
//...
因此,要将外键绑定到此资源,我需要执行以下操作……
Route::resource('survey.question', 'QuestionController');
这样,您就可以将路线映射成这样……
GET /survey/{survey}/question survey.question.index
GET /survey/{survey}/question/create survey.question.create
然后从以下位置更改模板
:{!! Form::open(array('action' => 'QuestionController@store', 'id' => 'Question')) !!}
收件人:
{!! Form::open(array('action' => 'QuestionController@store', 'id' => 'Question'), ['survey' => Request::route('survey')]) !!}
然后您可以像这样使用QuestionController::create
函数...
public function create(Survey $survey)
{
return view('question.create')->with('survey', $survey);
}
答案 3 :(得分:0)
您还可以在资源丰富的路由中使用如下参数:
char **ft_strsplit(char const *s, char c);
t_gnl *ft_store(char *file, t_gnl **list, t_env **fdf)
{
int fd;
char *line;
int height;
int ret;
fd = open(file, O_RDONLY);
if (fd < 0)
return (NULL);
height = 0;
while ((ret = get_next_line(fd, &line)))
{
if (ret == -1)
break ;
ft_append(list, ft_strsplit(line, ' '));
height++;
ft_strdel(&line);
}
if (line)
ft_strdel(&line);
if (ret == -1 || *list == NULL)
{
close(fd);
return (NULL);
}
*fdf = ft_init_env(*list, height);
close(fd);
return (*list);
}
static t_gnl *ft_new(char **tab)
{
t_gnl *node;
//node = NULL;
if (!(node = (t_gnl *)malloc(sizeof(t_gnl))))
return (NULL);
node->tab = tab;
node->next = NULL;
return (node);
}
void ft_append(t_gnl **head, char **tab)
{
t_gnl *list;
t_gnl *elem;
elem = NULL;
elem = ft_new(tab);
if (*head != NULL)
{
list = *head;
while (list->next)
list = list->next;
list->next = elem;
}
else
*head = elem;
}
int main(int ac, char **av)
{
t_gnl *list;
t_env *fdf;
list = NULL;
fdf = NULL;
list = ft_store(av[1], &list, &fdf);
if (fdf == NULL || ft_error(&list, fdf->map->width))
{
ft_putstr("Error\n");
exit(0);
}
ft_fdf(list, fdf);
}
然后在您的控制器中可以使用:
Route::resource('survey/{survey}/question', 'QuestionController');
您也可以使用路径模型绑定来避免在控制器中手动进行Survey检索:
class QuestionController extends Controller
{
// ...
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($id)
{
// $id will be the {survey} id from the route parameter.
}
// ...
}
请记住也要更新表单操作以包括参数
小心! 如果您像我已经显示的那样在资源中注册参数,则显示,编辑,更新和销毁(接受在URL中作用的资源ID)的方法将收到两个路由参数,其中一个将是调查ID ,第二个是问题ID (您的资源)。 记住这一点!