Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rob_utopano's avatar

Create model with json field with API

Hey guys,

I have a model wich has a json field inside the migration and I´ve build a crud-controller for the api.

Now I want to create a new model via the api, but I have problem creating it.

This would be the document-body

{
    "field1":"value1",
    "field2" : {
        "field3" : {
            "field4":"value4"
        }
    }
}

When I am trying to post it to my site, I am getting an error which says: "Array to string conversion". What am I doing wrong?

Is there anything special I must look around to get it working?

field2 is JSON inside the migration

Best regards!

0 likes
6 replies
farooq's avatar

How you actually showing in site ? Html code? Can I see it? May be you are getting the json fields in a string variable.

rob_utopano's avatar

@farooq thanks for your answer!

Unfortunately it´s not possible to show the website live. Sorry.

Here is the migration itself:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product_name');
            $table->string('external_id');
            $table->json('product_specs');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

In the CRUD-Controller the method looks like this (nothing spectacular):

    public function store(Request $request)
    {
        $this->authorizeForUser($request->user('api'), 'create', Product::class);

        $product= Product::create($request->all());
        return response()->json($product, 201);
    }

I hope this helps understanding my problem.

farooq's avatar

You are actually sending a json response as an array . And You are getting the response as string . So try to get json response in array format . like $a[] . Or loop it through the array and store it in string

rob_utopano's avatar

@farooq Sorry i think I misunderstanding you, so you mean I must write return response()->json($product[], 201); instead of return response()->json($product, 201); ?

rob_utopano's avatar

@farooq I´ve made now debug messages and I saw that the creation of my ` Product class failes, so the problem is earlier that the return of the response.

1 like
rob_utopano's avatar
rob_utopano
OP
Best Answer
Level 2

I found the solution, in my model I must add the protected casts array which contains the cast of the field to the desired value (in this case: array).

Please or to participate in this conversation.