2017-03-28 4 views
0

私のストアメソッドでモデルを保存した後に、確認後のビューにリダイレクトしています。保存した後、新しく作成したモデルをビューを確認するために渡す方法を知りたいです。ありがとう。save();の後にモデルをビューに渡します。

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

最も簡単で最も一般的なアプローチは、ルートにモデルIDを含めることです。

Route::get('/posts/{post_id}/confirm', '[email protected]')->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); 
    // ... 
} 

// ... 
関連する問題