在我的商店方法中保存模型后,我将重定向到确认视图。我想知道如何在保存后将新创建的模型传递给确认视图。感谢。
public function store(Request $request)
{
// validate incoming data
$this->validate($request, array(
'title' => 'required|max:191',
'body' => 'required',
'category_id' => 'required|integer',
'slug' => 'required|alpha_dash|min:5|max:191|unique:posts,slug',
'image' => 'sometimes|image'
));
// store in database
$post = new Post;
$post->title = $request->title;
$post->body = Purifier::clean($request->body);
$post->category_id = $request->category_id;
$post->slug = $request->slug;
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->save($location);
$post->image = $filename;
}
$post->save();
$post->tags()->sync($request->tags, false);
// Set flash success message
Session::flash('success',' The blog post was successfully saved!');
// I need to pass new model in here
return redirect()->route('confirm.posts');
}
答案 0 :(得分:0)
最简单和最常见的方法是在您的路线中包含模型ID。
Route::get('/posts/{post_id}/confirm', 'PostController@getConfirm')->name('post.confirm');
然后在您的控制器中,您将重定向到post.confirm
路由并加载模型。
// ...
public function store(Request $request)
{
// ...
return redirect()->route('post.confirm', $post->id);
}
public function getConfirm($postId)
{
$post = Post::findOrFail($postId);
// ...
}
// ...