dongqi8030 2018-05-31 15:43
浏览 66
已采纳

在Laravel中使用已创建的ID重定向

I've followed a few blog CRUD tutorials for Laravel, and I'm trying to advance to the next step by making a recipes website for myself.

The idea is that the user creates a new recipe, which contains the title, description, portions, user_id to the recipes table.

I've got this working, and it successfully saves to the db, along with the associated tags to the recipe_tags table.

I've currently got the storeController redirecting to the recipe.show view, which works fine, but I'm trying to get it to redirect to the steps.create view, and pull in the title and description from the db, and the associated id in the recipes table.

The following is my store@recipesController:

public function store(Request $request)
{
    $this->validate($request, [
      'name' => 'required|max:255',
      'description' => 'required',
      'portions' => 'required'
    ]);

    $recipe = new Recipe;

    $recipe->name = $request->name;
    $recipe->description = $request->description;
    $recipe->portions = $request->portions;
    $recipe->user_id = auth()->user()->id;

    $recipe->save();

    if(isset($request->tags)){
      $recipe->tags()->sync($request->tags, true);
    } else {
      $post->tags()->sync([]);
    }

    return redirect()->route('steps.create', ['id' => $recipe->id]);
}

And this is my create@stepsController

public function create($id)
{
  $recipe = Recipe::find($id);
  return view('steps.create')->withRecipe($recipe);
}

But it's returning the following:

Type error: Too few arguments to function App\Http\Controllers\StepsController::create(), 0 passed and exactly 1 expected

I'm a bit out of my depth here. I think it may be an issue with my routes, (both of which are Route::resource(...))

My fallback option is to use Ajax to write the recipe and steps to the database on the same page, but I'd rather learn how to do it the way I intended originally.

(EDIT: It's redirecting to the following URL in case that is of any significance.)

http://127.0.0.1:8000/steps/create?id=3

List of routes:

    $ php artisan route:list
    +--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
    | Domain | Method    | URI                    | Name             | Action                                                                 | Middleware   |
    +--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
    |        | GET|HEAD  | /                      |                  | App\Http\Controllers\PagesController@index                             | web          |
    |        | GET|HEAD  | api/user               |                  | Closure                                                                | api,auth:api |
    |        | GET|HEAD  | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
    |        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
    |        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
    |        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
    |        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
    |        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
    |        | POST      | password/reset         |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
    |        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
    |        | GET|HEAD  | recipes                | recipes.index    | App\Http\Controllers\RecipesController@index                           | web,auth     |
    |        | POST      | recipes                | recipes.store    | App\Http\Controllers\RecipesController@store                           | web,auth     |
    |        | GET|HEAD  | recipes/create         | recipes.create   | App\Http\Controllers\RecipesController@create                          | web,auth     |
    |        | DELETE    | recipes/{recipe}       | recipes.destroy  | App\Http\Controllers\RecipesController@destroy                         | web,auth     |
    |        | PUT|PATCH | recipes/{recipe}       | recipes.update   | App\Http\Controllers\RecipesController@update                          | web,auth     |
    |        | GET|HEAD  | recipes/{recipe}       | recipes.show     | App\Http\Controllers\RecipesController@show                            | web,auth     |
    |        | GET|HEAD  | recipes/{recipe}/edit  | recipes.edit     | App\Http\Controllers\RecipesController@edit                            | web,auth     |
    |        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
    |        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
    |        | GET|HEAD  | steps                  | steps.index      | App\Http\Controllers\StepsController@index                             | web,auth     |
    |        | POST      | steps                  | steps.store      | App\Http\Controllers\StepsController@store                             | web,auth     |
    |        | GET|HEAD  | steps/create           | steps.create     | App\Http\Controllers\StepsController@create                            | web,auth     |
    |        | GET|HEAD  | steps/{step}           | steps.show       | App\Http\Controllers\StepsController@show                              | web,auth     |
    |        | PUT|PATCH | steps/{step}           | steps.update     | App\Http\Controllers\StepsController@update                            | web,auth     |
  • 写回答

1条回答 默认 最新

  • douxie2007 2018-05-31 16:13
    关注

    Redirect route is passing 'id' as a GET Parameter so your controller can't understand that GET parameter as and $id variable, so you can solve it by doing this on your controller:

    public function create(Request $request)
    {
        $recipe = Recipe::find($request->id);
        return view('steps.create')->withRecipe($recipe);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装