如何使用formData更新laravel中的帖子?

时间:2020-03-23 16:09:59

标签: php laravel vue.js axios

所以我正在社交网络中工作,我希望完成一篇帖子后就可以对其进行编辑,但是我遇到了错误:

message: "This action is unauthorized."

对我来说似乎很简单,我通过一个axios调用将组件中的信息发送给它,该调用经过我定义的路径,并且仅到达控制器,再从那里到达服务,但我一定会丢失我不知道是什么东西。任何提示都非常感激,因为我有点生气...

这将是组件中的功能:

 editPost() {
      let formData = new FormData();
      let headers = { headers: { "Content-Type": "multipart/form-data" } };
      let updatedPost = this.post_to_update;
      formData.append("post", updatedPost.description);
      formData.append("file", this.file);
      formData.append("video_link", updatedPost.video_link);
      axios
        .post("/posts/update/" + updatedPost.id, formData, headers)
        .then(response => {
          updatedPost.attach_file = response.data.attach_file;
          updatedPost.id = response.data.id;
          serverBus.$emit("post_edited", updatedPost);
        });

      this.file = "";
      this.post_to_update = {
        id: "",
        index: null,
        description: "",
        attach_file: "",
        video_link: "",
        name: this.profile_info.name,
        surname_1: this.profile_info.surname_1,
        surname_2: this.profile_info.surname_2,
        nick: this.profile_info.nick,
        picture: this.profile_info.picture,
        id_rol: this.profile_info.id_rol,
        time_ago: "0 minutes",
        code: this.profile_info.code
      };
      $("#EditModal").modal("hide");
    },

这是web.php:

########################################################################################################################
# Post Routes
########################################################################################################################
Route::post('/posts/recommend', 'PostController@recommend')
    ->name('recommendPost');

Route::post('/posts/update/{id}', 'PostController@update');

Route::post('/posts/report', 'PostController@report')
    ->name('reportPost');

Route::get('/posts/{post}/comments', 'PostController@retrieveComments')
    ->where('post', '[0-9]+')
    ->name('commentsByPost');

Route::resource('/posts', 'PostController')
    ->only(['index', 'store', 'update', 'destroy']);

Route::get('/reportedPosts', 'PostController@reportedPosts');

控制器:

    public function update(StorePostRequest $request, $post_id)
    {
        return $this->postService->updatePost($request, $post_id);
    }

和服务:

    public function updatePost(StorePostRequest $request, $post_id)
    {
        $post_to_update = Post::where('id', $post_id)->first();
        $post_to_update->description  = $request->post;
        $post_to_update->attach_file  = $request->file ? $filename = sha1(time()) : null;
        $post_to_update->file_name    = $request->file ? $request->file->getClientOriginalName() : null;
        $post_to_update->file_type    = $request->file ? $request->file->getClientOriginalExtension() : null;
        $post_to_update->save();
        if ($request->file) {
            Storage::disk('s3')->putFileAs('storage/user_uploads/post_files/' . $post_to_update->id, $request->file, $filename, 'public');
            Storage::disk('local')->putFileAs('public/user_uploads/post_files/' . $post_to_update->id, $request->file, $filename, 'public');
        }
        return $post_to_update;
    }

1 个答案:

答案 0 :(得分:0)

我认为StorePostRequest类会导致用户授权受阻。

尝试在StorePostRequest处添加以下行以通过授权检查。

public function authorize()
{
     return true;   //Default false;
}