your $data['sku'] is not an array, it's a value
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
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\ProductAttribute;
use App\Models\User;
use Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
public function addProductAttribute(Request $request, $id)
{
$fields = $request->validate([
'size' => 'required|max:255',
'cost_price' => 'required|max:255',
'sales_price' => 'required',
'stock' => 'required',
'sku' => 'required|unique:product_attributes,sku',
]);
$product = Product::select('id','product_name','product_code','product_color','product_cost_price','product_sales_price','product_image')->with('product_attributes')->where(['id' => $id])->first();
$data = $request->all();
foreach($data['sku'] as $key => $value){
if(!empty($value)){
//Prevent SKU duplicate
$skuCount = ProductAttribute::where('sku',$value)->count();
if($skuCount>0){
return response()->json([
"message"=>"SKU Already exist!, Please add a new SKU"
]);
}
//Prevent Size duplicate
$sizeCount = ProductAttribute::where(['product_id'=>$id,'size'=>$data['size'][$key]])->count();
if($sizeCount>0){
return response()->json([
"message"=>"Size Already exist!, Please add a new Size"
]);
}
$product_attribute = new ProductAttribute;
$product_attribute->product_id = $id;
$product_attribute->sku = $value;
$product_attribute->size = $data['size'][$key];
$product_attribute->cost_price = $data['cost_price'][$key];
$product_attribute->sales_price = $data['sales_price'][$key];
$product_attribute->stock = $data['stock'][$key];
$product_attribute->status = 1;
$product_attribute->save();
}
}
return response()->json([
"product_attribute"=>$product_attribute,
"message"=>"Product Attribute Added Successfully"
]);
}```
The error is coming from foreach ``"message": "foreach() argument must be of type array|object, string given",``` an help?
Please or to participate in this conversation.