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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,