Reputation: 148
Here is my item table(laravel migration):
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();
});
Here is the error : link to error message picture in postman
Route:
Route::post('item','ItemController@store');
Controller:
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);
}
}
Upvotes: 0
Views: 322
Reputation: 14278
Based on the image, your problem doesn't seems to be the json column, but the icd
column which turns out to be null
instead of the value 2 that you are passing. Make sure That in your Item
model you have listed the icd
column in your fillable
array.
And then for the JSON, you use wrong quotes to distinguish the key or the string. So try:
"mandatory_prop": "{'size': '35mb', 'Speed': '2.86Hz'}"
You can even try without the ""
around the json object.
Upvotes: 2
Reputation: 1763
Try this
Add this in your Item Model
protected $casts = [
'mandatory_prop' => 'json',
];
And $request->input('mandatory_prop')
is must be Array
you can use $request->input('mandatory_prop')
or $request->mandatory_prop
Upvotes: 1