Laravel 使用 Passport 更新记录

问题描述 投票:0回答:1

我的 API 遇到这个问题。 我不更新数据库中的记录。 在 Postman 中,响应是 true,但不会保存在数据库中。 在 Postman 中,我使用 PUT 方法传递并在正文名称中设置文本。

产品控制器:

public function update(Request $request, $id)
{
    $product = auth()->user()->products()->find($id);

    if (!$product) {
        return response()->json([
            'success' => false,
            'message' => 'Product with id ' . $id . ' not found'
        ], 400);
    }
    
    $updated = $product->fill($request->all())->save();

    if ($updated)
        return response()->json([
            'success' => true
        ]);
    else
        return response()->json([
            'success' => false,
            'message' => 'Product could not be updated'
        ], 500);
}

Postman Update Api

php laravel-5 token
1个回答
0
投票

您应该查看您的

Product
模型,看看名称是否设置为可填写字段:
$fillable = ['name'];
。另外,关键可能只是
name
而不是
"name"

© www.soinside.com 2019 - 2024. All rights reserved.