无法通过邮递员

时间:2019-03-21 06:13:13

标签: json laravel postman

这是我的物品表(laravel迁移):

Schema::connection('mysql')->create('item', function (Blueprint $table) {
            $table->increments('id')->unsignedInteger();
            $table->unsignedInteger('icd');
            $table->unsignedInteger('itypeid');
            $table->json('mandatory_prop');
            $table->unsignedInteger('parentId')->nullable();
            $table->foreign('icd')->references('id')->on('itemClass')->onDelete('cascade')->onUpdate('cascade');
            $table->foreign('itypeid')->references('id')->on('itemType')->onDelete('cascade')->onUpdate('cascade');
            $table->foreign('parentId')->references('id')->on('item')->onDelete('cascade')->onUpdate('cascade');
            $table->timestamps();
        });

这是错误: link to error message picture in postman

路线:

   Route::post('item','ItemController@store');

控制器:

public function store(Request $request)
  {
    //input a new role
    $item = $request->isMethod('put') ? Item::findOrFail($request->item_id) : new Item;
    $item->id = $request->input('item_id');
    $item->icd = $request->input('icd');
    $item->itypeId = $request->input('itypeId');
    $item->mandatory_prop = $request->input('mandatory_prop');
    $item->parentId = $request->input('parentId');

     if($item->save()) {
       return new itemResource($item);
       }
  }

2 个答案:

答案 0 :(得分:1)

根据图像,您的问题似乎不是json列,而是public class MyComponent : IMyComponent { private readonly IHttpContextAccessor _contextAccessor; public MyComponent(IHttpContextAccessor contextAccessor) { _contextAccessor = contextAccessor; } public string GetDataFromSession() { return _contextAccessor.HttpContext.Session.GetString(*KEY*); } } 列,事实是icd而不是您传递的值2。确保在您的null模型中,您已经在Item数组中列出了icd列。

然后对于JSON,您使用错误的引号来区分键或字符串。因此,尝试:

fillable

您甚至可以尝试在json对象周围不使用"mandatory_prop": "{'size': '35mb', 'Speed': '2.86Hz'}"

答案 1 :(得分:0)

尝试一下

将此添加到您的Item Model

protected $casts = [
     'mandatory_prop' => 'json',
];

并且$request->input('mandatory_prop')必须是 Array

您可以使用$request->input('mandatory_prop')$request->mandatory_prop