dongqi8030 2018-05-31 07: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 08: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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部