我已经为Laravel提供了一些博客CRUD教程,我正在尝试通过为自己制作食谱网站来推进下一步。
这个想法是用户创建一个新配方,其中包含title
,description
,portions
,user_id
到recipes
表。
我已经完成了这项工作,并且已成功保存到数据库,以及关联的tags
到recipe_tags
表。
我目前已将storeController
重定向到recipe.show
视图,该视图工作正常,但我尝试将其重定向到steps.create
视图,并从数据库中提取标题和描述,以及id
表中的关联recipes
。
以下是我的商店@ 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]);
}
这是我的 create @ stepsController
public function create($id)
{
$recipe = Recipe::find($id);
return view('steps.create')->withRecipe($recipe);
}
但是它返回了以下内容:
Type error: Too few arguments to function App\Http\Controllers\StepsController::create(), 0 passed and exactly 1 expected
我有点超出我的深度。我认为这可能是我的路线问题(两者都是Route::resource(...)
)
我的后备选项是使用Ajax在同一页面上将配方和步骤写入数据库,但我宁愿学习如何按照我原定的方式进行操作。
(编辑:如果有任何重要意义,它会重定向到以下网址。)
http://127.0.0.1:8000/steps/create?id=3
路线列表:
$ 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 |
答案 0 :(得分:1)
重定向路由将'id'作为GET参数传递,因此您的控制器无法将GET参数理解为$和$ id变量,因此您可以通过在控制器上执行此操作来解决此问题:
public function create(Request $request)
{
$recipe = Recipe::find($request->id);
return view('steps.create')->withRecipe($recipe);
}