laravel中与关系绑定的路由模型

b5lpy0ml  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(141)
PUT|PATCH       api/v1/tweets/{tweet}/comments/{comment} tweets.comments.update › Api\\V1\\TweetCommentController@update

我有上面的这个路由,我试图绑定控制器的关系
我正在用键 * comment = This is a test comment * 传递格式数据
我在TweetCommentController中尝试了下面的代码。

public function update(Tweet $tweet, TweetComment $comment, TweetCommentRequest $request)

但这是行不通的。它只是重定向到登录页面。
我也试过下面这个

public function update(Tweet $tweet, TweetComment $comment)

这一个似乎工作,至少我可以记录$tweet和$comment。,但我不能访问表单数据。
请帮帮忙。

nkhmeac6

nkhmeac61#

我的错误,我尝试了api调用从 Postman ,我用了方法PUT。
我将方法更改为POST并在表单数据中添加一个新键_method = PUT
因此,我当前的(工作)代码如下所示

API调用

发布{{基本网址}}/推文/1/评论/2
form-data:comment =“已编辑的测试备注”
_方法= PUT

控制器

public function update(TweetCommentRequest $request, Tweet $tweet, TweetComment $comment){
    $comment->update($request->validated());
    return $this->sendResponse([
            'message' => __('Comment updated successfully.'),
            'comment' => $comment
        ]);
}

相关问题