You can use the function ::firstOrCreate($data) for this, it returns the item when found or the inserted item when there could not be an item found with the inserted data. You can find it here: https://laravel.com/docs/9.x/eloquent#retrieving-or-creating-models
foreach ($product_id as $key => $value) {
$input['product_name'] = $request->product_name[$key];
$input['product_price'] = $request->product_price[$key];
$add_product = Product::firstOrCreate($input);
}
What I would do when you need to update multiple products instead of just one is checking the count of the product with the specific information. After this I would update it when the count is higher than 1.
foreach ($product_id as $key => $value) {
$input['product_name'] = $request->product_name[$key];
$input['product_price'] = $request->product_price[$key];
$productCount = Product::where('product_name', $request->product_name[$key])->count();
if ($productCount > 1) {
$products = Product::where('product_name', $request->product_name[$key])->get();
$products->update($input);
} else {
$add_product = Product::firstOrCreate($input);
}
}
Disclaimer: I did not test the code.