请教一下写 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 )...

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
leo
最佳答案

$fieldValve 改成 $field_valve

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

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

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

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

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

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

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

$fieldValve 改成 $field_valve

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

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

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

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!