how to fix message: "Undefined index: product_id"
public function postAssignProduct(Request $request)
{
$old = apiRequest('tag/showtag', ['id' => $request->id, 'store_id' => $request->store_id]);
$response = apiRequest('tag/post-assign-product', $request->except('_token'));
$auth = AuthUser()['id'];
$data2 = $request->except('_token', 'id');
$data2['product_assigned'] = [];
foreach ($data2['product_id'] as $value) {
$data2['product_assigned'][] = getProductName($data2['store_id'], $value);
};
$data2['product_assigned'] = implode(", ", $data2['product_assigned']);
unset($data2['product_id']);
$tag = "TagAssign: ".$old['tag']['name'];
$auditing = auditCreate('App\TagAssign', $data2, $auth, $tag);
return $response;
}
I am getting this error.When try to use empty function is says invalid argument for foreach loop.Please help
@nuna does your error give you a line number where its erroring?
also it looks like you should be calling:
$this->getProductName($data2['store_id'], $value);
not
getProductName($data2['store_id'], $value);
inside your loop, assuming your getProductName method is in the same class as 'postAssignProduct' method.
message: "Undefined index: product_id", exception: "ErrorException",…}
exception: "ErrorException"
file: "/ob1/domains/dCommerce/app/Http/Controllers/TagController.php"
line: 373
message: "Undefined index: product_id"
trace: [{file: "/ob1/domains/dCommerce/app/Http/Controllers/TagController.php", line: 373,…},…]
line 373 starts at the foreach loop.
@nuna what does a dump of $data2 look like, if you dump after
$data2 = $request->except('_token', 'id');
array(1) {
["store_id"]=>
string(1) "1"
}
{
"message": "Undefined index: product_id",
"exception": "ErrorException",
"file": "/ob1/domains/dCommerce/app/Http/Controllers/TagController.php",
"line": 374,
"trace": [
{
"file": "/ob1/domains/dCommerce/app/Http/Controllers/TagController.php",
"line": 374,
"function": "handleError",
"class": "Illuminate\Foundation\Bootstrap\HandleExceptions",
"type": "->"
},
{
"function": "postAssignProduct",
"class": "App\Http\Controllers\TagController",
"type": "->"
},
It looks like this
@nuna then your data2 array doesn't have the 'product_id' in it, which is what your error is telling you.
in that case,
foreach ($data2 as $value) {
$data2['product_assigned'][] = getProductName($data2['store_id'], $value);
};
Thank you.I taught must do a check with empty function.
Please or to participate in this conversation.