请教一下写 API 为什么 store 添加数据成功,update 更新数据失败?

整了好几天了没有发现问题所在,请教高手帮忙看看问题可能出在哪:disappointed_relieved:
database/migrations/2020_06_01_211105_create_field_valves_table.php

public function up()
    {
        Schema::create('field_valves', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('tag');
            $table->integer('user_id')->index();
            $table->timestamps();
        });
    }

app/Models/FieldValve.php

    protected $fillable = ['tag'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

app/Http/Resources/FieldValveResource.php 默认

app/Http/Requests/FieldValveRequest.php

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        switch($this->method()) {
            case 'POST':
                return [
                    'tag' => 'required|string',
                ];
                break;
            case 'PATCH':
                return [
                    'tag' => 'string',
                ];
                break;
        }
    }

    public function attributes()
    {
        return [
            'tag' => '位号',
        ];
    }

app/Http/Controllers/FieldValvesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\FieldValve;
use App\Http\Resources\FieldValveResource;
use App\Http\Requests\FieldValveRequest;

class FieldValvesController extends Controller
{
    public function store(FieldValveRequest $request, FieldValve $fieldValve)
    {
        $fieldValve->fill($request->all());
        $fieldValve->user_id = $request->user()->id;
        $bool = $fieldValve->save();

        if (empty($bool)) {
            return response([
                'code' => 20001,
                'message' => '现场阀门创建失败!!!',
            ], 201);
        }

        return response([
            'code' => 20000,
            'message' => '现场阀门创建成功!!!',
        ], 201);
    }

    public function update(FieldValveRequest $request, FieldValve $fieldValve)
    {
        $fieldValve->update($request->all());
        return new FieldValveResource($fieldValve);
    }
}

新建调试结果

请教一下写api为什么store添加数据成功,update更新数据失败?
修改调试结果

请教一下写api为什么store添加数据成功,update更新数据失败?

参考相关教程
6.2. 发布话题《L03 Laravel 教程 - 实战构架 API 服务器 (Laravel 6.x)...
6.3. 修改话题《L03 Laravel 教程 - 实战构架 API 服务器 (Laravel 6.x)...

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
leo
最佳答案

$fieldValve 改成 $field_valve

4年前 评论
firstsight (楼主) 4年前
firstsight (楼主) 4年前
leo (作者) 4年前
firstsight (楼主) 4年前
leo (作者) 4年前
firstsight (楼主) 4年前
讨论数量: 5

form 表单里添加 <input type="hidden" name="_method" value="PATCH"/> 了吗

4年前 评论
firstsight (楼主) 4年前

你把参数格式改为 form-data,或者把参数放到 Params 里面

4年前 评论
firstsight (楼主) 4年前
firstsight (楼主) 4年前

你是不是的告诉模型你要改那条数据啊?

4年前 评论
firstsight (楼主) 4年前
小宝爹 (作者) 4年前
firstsight (楼主) 4年前
leo

$fieldValve 改成 $field_valve

4年前 评论
firstsight (楼主) 4年前
firstsight (楼主) 4年前
leo (作者) 4年前
firstsight (楼主) 4年前
leo (作者) 4年前
firstsight (楼主) 4年前
leo

另外需要排除一下 FieldValveResource 是不是有问题,可以先直接 return $field_valve 而不是用 FieldValveResource 包一层

4年前 评论
firstsight (楼主) 4年前
leo (作者) 4年前
firstsight (楼主) 4年前
firstsight (楼主) 4年前