在 laravel 中的 'store' 资源控制器之后立即显示视图

displaying a view immediately after the 'store' resource controller in laravel

所以目前我的 'store' 资源控制器添加到我的数据库中的 table 并获取我的 'index' 页面(我的主页)的视图但是当我刷新它时查看它重复添加到数据库的最后一行。我注意到 URI 是“http://127.0.0.1:8000/store' instead of 'http://127.0.0.1:8000/index' can someone please explain what is going on. i apologise for the lack of technical terms i'm a new apprentice and am trying to figure this out using my own initiative but so far have had no luck. i also noticed that my log out function does the same thing although the URI is slightly different in that it still displays the CSRF token in the URI however it still says its at the 'logout' file path, here is an example of this; 'http://127.0.0.1:8000/logout?_token=bqTl4J3EZyKj7LS5ZvqfRB8k1Qg02IT1j4WlBG51&dir2login=

仅供参考,我已经尝试过 return $this->index();

我的注销控制器方法;

  public function logout(){
  Auth::logout();
  return view('index',['posts'=>$this->getTable()]);
}

我的商店控制器方法:

public function store(Request $request){
  //creates new row in database table
  //$clientIp=$request->ip();
  $post = new PostModel();
  //gets email of user currently logged in
  $post->email=Auth::user()->email;
  $post->ip=$request->ip();
  $post->content=$request->content;
  $post->company=$request->company;
  $post->rating=$request->rating;
  //saves to database
  $post->save();
  return view('index',['posts'=>$this->getTable()]);
}

你试过了吗

return redirect('/');

您在这里要做的是让用户进入索引视图。但是,与其返回不同的视图(并且必须再次定义 'posts' 的逻辑),不如将用户重定向到控制器的索引方法要容易得多。您有多种选择。我在下面列出了三个:

这是您目前拥有的:

return view('index',['posts'=>$this->getTable()]);\

您可以使用“action”(ControllerName@FunctionName):

return redirect()->action('PostController@index');

或使用“命名路线”:

return redirect()->route('posts.index');

或直接 URL

return redirect('/');