Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel隐式模型路由绑定和Vue JS_Php_Laravel 5.2_Vue.js - Fatal编程技术网

Php Laravel隐式模型路由绑定和Vue JS

Php Laravel隐式模型路由绑定和Vue JS,php,laravel-5.2,vue.js,Php,Laravel 5.2,Vue.js,我想知道是否有一种方法可以自动绑定vuejs和laravel中的模型和路由,就像只绑定laravel一样。我的意思是,假设我想用给定的id更新某个帖子,在laravel中,我会这样做: public function edit(Request $request Post $post) { $post->update($request->all()); // then I will return some view or do some redir

我想知道是否有一种方法可以自动绑定vuejs和laravel中的模型和路由,就像只绑定laravel一样。我的意思是,假设我想用给定的id更新某个帖子,在laravel中,我会这样做:

  public function edit(Request $request Post $post)
  {   

      $post->update($request->all());
    // then  I will return some view or do some redirect here

   }
请注意,我不必指定post id,因为laravel会在幕后自动执行该操作

根据我到目前为止所了解的情况,使用与vuejs中的一些post数据相同的函数,我首先通过执行以下操作获取id:

  public function edit(Request $request)
  {

   $post = $request->input('id');
   $post =  Post::where('id', $id)->first();
  // Then I perform other logic  here
  // then  I will return some view or do some redirect here

  }
请注意,该函数不再接受任何post模型。如果我尝试将模型添加为输入参数,并在vuejs中调用某些编辑功能,但到达某个控制器中的编辑路径,则会出现内部服务器错误500。 我使用的代码是这样的:

  public function edit(Request $request Post $post)
  {


  // Then I perform other logic  here
  // then  I will return some view or do some redirect here

  }


  //vuejs method
  editPost(post){
  var postData = {id: this.post.id};
  this.$http.post('edit/'+ postData).then((response)=>
  {
     //Some logic goes here including catch part

  });
 }

您可以将post id作为路由参数传递:

Route::get('posts/{post}/edit', 'PostController@edit');
因此,它将自动将模型实例注入到
post
参数中

public function edit(Request $request, Post $post)
{
    $post->update($request->all());
}
然后在vue中,您可以执行以下操作:

editPost(post) {
    let postId = post.id;
    let postData = {
        title: '',
        content: ''
    };

    this.$http.post('posts/' + postId + '/edit', postData).then((response) => {
        //Some logic goes here including catch part
    });
}

这很有效。我还有一个问题,我正在使用授权方法检查只有帖子所有者才能删除帖子,但我得到了403,当你得到403时,这意味着你没有授权这样做。您必须确保登录的用户是帖子的所有者。