【问题标题】:Laravel Form Return then RedirectLaravel 表单返回然后重定向
【发布时间】:2018-10-17 14:00:52
【问题描述】:

我仍在使用 laravel,我想这有一个简单的解决方法,但我似乎找不到任何东西。 我想提交我的表单,通过创建返回数据,然后重定向到另一个页面,而不是显示我的表单结果。目前它只返回一个带有电子邮件和名称变量的空页面并将其添加到数据库中。

我在 adduser 控制器中尝试了以下操作,但它没有通过我的数据库执行 create 函数,只是重定向我。

   return Child::create([
        'name' => $request->input('name'),
        'email' => $request->input('email'),           
   ]) && Redirect::to('/anotherpage');

我是否必须对我的控制器做一些事情,或者这可以通过上述的变体来完成吗?

【问题讨论】:

  • 不需要返回create方法的结果。返回只是结束方法的执行,所以调用Child::create 然后return Redirect...

标签: laravel forms redirect


【解决方案1】:

您可以将redirect() 函数与数据一起使用

$child = Child::create([
    'name' => $request->input('name'),
    'email' => $request->input('email'),           
]);

// in route() function first parameter is the route name,
// second parameter is the data array.
return redirect(route('another_page', ['child' => $child]));

【讨论】:

  • 谢谢,最后我不需要将孩子存储在数组中,而是按照@joe 建议的方式运行 child::create 然后返回 redirect()->action('manageusersController @index');
猜你喜欢
  • 1970-01-01
  • 2018-04-21
  • 2016-06-08
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 2015-11-09
相关资源
最近更新 更多