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

Alewa's avatar
Level 2

Laravel 10 Api, foreach array error

I am having this error "message": "foreach() argument must be of type array|object, string given", when i try to run my api products database

public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('product_name');
            $table->string('product_code')->unique();
            $table->string('product_color')->nullable();
            $table->float('product_cost_price');
            $table->float('product_sales_price');
            $table->string('product_discount')->default(0);
            $table->longText('description')->nullable();
            $table->string('group_code')->nullable();
            $table->string('meta_title')->nullable();
            $table->string('meta_description')->nullable();
            $table->string('meta_keywords')->nullable();
            $table->mediumText('product_image')->nullable();
            $table->enum('best_seller', ['Yes', 'No'])->default('No');
            $table->boolean('status')->default(1);
            $table->integer('product_category_id')->foreign('product_category_id')->references('id')->on('product_categories')->onDelete('cascade');
            $table->integer('admin_created_id')->foreign('admin_created_id')->references('id')->on('users')->onDelete('cascade');
            $table->integer('admin_updated_id')->foreign('admin_updated_id')->references('id')->on('users')->onDelete('cascade')->nullable();
            $table->timestamps();
        });
    }```

product_attributes database

Schema::create('product_attributes', function (Blueprint $table) { $table->id(); $table->string('size')->nullable(); $table->float('cost_price'); $table->float('sales_price'); $table->integer('stock'); $table->string('sku')->unique(); $table->boolean('status')->default(1); $table->integer('product_id')->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); $table->timestamps(); });```

Product.php Model

public function product_attributes()
    {
        return $this->hasMany('App\Models\ProductAttribute');
    }

ProductController.php file

0 likes
8 replies
krisi_gjika's avatar

your $data['sku'] is not an array, it's a value

1 like
Alewa's avatar
Level 2

@krisi_gjika please don't understand what you are saying, can you explain to details and give me example?

krisi_gjika's avatar

@Alewa the thing you are trying to loop through $data['sku'] is not something "loopable" it's a single value,

you have validated it yourself as such in 'sku' => 'required|unique:product_attributes,sku' an array would not pass this validation. You can dd the value to check for yourself dd($request->all()) or $request->dd(). I would suggest to follow a basic php course first, before jumping into MVCs and frameworks.

1 like
amitsolanki24_'s avatar

@alewa

 'sku' => 'required|unique:product_attributes,sku',

Are you sending sku field as an array from your api or as a string.

Alewa's avatar
Level 2

My code is ok, there is no problem with the code, the problem was in my thunder client software for testing my api, i needed to add [ ] to my field, since I am adding multiple array of data, so example in my thunder client field this is how i wrote it to correct the error product_cost_price[ ] or sku [ ], if you don't add the bracket example product_cost_price, you will get this error "message": "foreach() argument must be of type array|object, string given", Thanks to you all.

jlrdw's avatar

What does a dd($data['sku']); show in network tab?

Please or to participate in this conversation.