It is difficult to read into your intentions from the unformatted code; but if product_id can be null, then you can set an empty array default in the Controller:
foreach ($request->input('product_id', []) as $key => $product_id)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i'm using laravel 9, I got this error; foreach() argument must be of type array|object, null given
this my contoller:
foreach ($request->product_id as $key => $product_id) {
$invoice_item=new Invoice_Item();
$quantity=$request->quantity[$key];
$product_id=$request->product_id[$key];
$invoice_item->quantity=$quantity;
and this jquery in view: $('#addhere #div'+counter+' select').attr({'name':'product_id[]','id':'dynamic'+counter,'onchange':'fun(this)'}); $('#addhere #div'+counter+' input:nth-child(2)').attr({'id':'quantity'+counter,'onchange':'fun2(this)' ,'name':'quantity[]'}); $('#addhere #div'+counter+' input:nth-child(3)').attr('id','price'+counter); $('#addhere #div'+counter+' input:nth-child(4)').attr('id','vat'+counter); $('#addhere #div'+counter+' input:nth-child(5)').attr('id','rem'+counter);
It is difficult to read into your intentions from the unformatted code; but if product_id can be null, then you can set an empty array default in the Controller:
foreach ($request->input('product_id', []) as $key => $product_id)
What do you get if you add dd($request->product_id);. null? If you get an array, then the error is not in the code provided.
@Sinnbeck I got null
@Sinnbeck obviously it is null
foreach() argument must be of type array|object, null given
@tykus @Sinnbeck at this block I assigned to name =product_id[] and it's have value
$('#addhere #div'+counter+' select').attr({'name':'product_id[]','id':'dynamic'+counter,'onchange':'fun(this)'});
$('#addhere #div'+counter+' input:nth-child(2)').attr({'id':'quantity'+counter,'onchange':'fun2(this)' ,'name':'quantity[]'});
$('#addhere #div'+counter+' input:nth-child(3)').attr('id','price'+counter);
$('#addhere #div'+counter+' input:nth-child(4)').attr('id','vat'+counter);
$('#addhere #div'+counter+' input:nth-child(5)').attr('id','rem'+counter);
<script>
$(document).ready(function(){
let counter=1;
$('#add').on("click",function(){
$('#addhere').append("<div></div>");
$('#addhere div:last').attr({
class:"form-group form-inline mb-2",
id:"div"+counter,
});
//append
$('#addhere div:last').append(`
<select class="aya form-control " ></select>
<input value="1" type="number" min="1" class="form-control">
<input type="text" class="form-control mo">
<input type="checkbox" class="form-control mo" value="vat" >
<input type="button" class="rem btn-danger" value=" x " >
<br>
`);
$('#addhere #div'+counter+' select').attr({'name':'product_id[]','id':'dynamic'+counter,'onchange':'fun(this)'});
$('#addhere #div'+counter+' input:nth-child(2)').attr({'id':'quantity'+counter,'onchange':'fun2(this)' ,'name':'quantity[]'});
$('#addhere #div'+counter+' input:nth-child(3)').attr('id','price'+counter);
$('#addhere #div'+counter+' input:nth-child(4)').attr('id','vat'+counter);
$('#addhere #div'+counter+' input:nth-child(5)').attr('id','rem'+counter);
// $('#total-tr #div'+counter+' select').append('<option disabled selected>choose</option>');
@foreach($products as $item)
$('#addhere #div'+counter+' select').append('<option></option>');
$('#addhere #div'+counter+' select option:last').attr( 'value','{{$item->id}}');
$('#addhere #div'+counter+' select option:last').text('{{$item->name}}');
// $('#addHere #div'+counter+' input:nth-child(2)').attr('max','{{$item->stock_no}}');
@endforeach
counter++;
});
});
$(document).on('click', '.rem', function(){
$(this).closest('div').remove();
});
function fun(index){
let productID=index.value;
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
method:'GET' ,
url:"{{route ('priceTwo')}}" ,
data:{productID: productID} ,
// dataType:'json' ,//return data will be json
success: function (op) {
index.parentNode.childNodes[5].value=op;
}
});
}
function fun2(index){
let quantity=index.value;
let productID=index.parentNode.childNodes[1].value;
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
method:'GET' ,
url:"{{route ('totalPriceUpdate')}}" ,
data:{quantity: quantity, productID: productID} ,
// dataType:'json' ,//return data will be json
success: function (price) {
index.parentNode.childNodes[5].value=price;
}
});
const $btnPrint = document.querySelector("#btnPrint");
$btnPrint.addEventListener("click", () => {
window.print();
});
}
</script>
@MonaEid Is it the fun or fun2 function? In both you call it productID? I assume that is what you are using, and not a fom? And it seems to get the value of 1 item?
@Sinnbeck in those functions I use ajax and the problem is not here , it's in the first of script tag
@MonaEid that part doesn't do any server requests. Is there a form you aren't showing?
@Sinnbeck yes there is a form, and the ajax part is just for showing price
@MonaEid ok then check the rendered html after clicking add. Show us the rendered form with the appropriate attributes
F12 - > elements
<select class="form-control " name="product_id[]" id="dynamic1" onchange="fun(this)"><option value="1">coffe</option></select>
@MonaEid that does not look like a full form ?
<body class="antialiased">
<div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0">
<script>
$(document).ready(function(){
let counter=1;
$('#add').on("click",function(){
$('#addhere').append("<div></div>");
$('#addhere div:last').attr({
class:"form-group form-inline mb-2",
id:"div"+counter,
});
//append
$('#addhere div:last').append(`
<select class="aya form-control " ></select>
<input value="1" type="number" min="1" class="form-control">
<input type="text" class="form-control mo">
<input type="checkbox" class="form-control mo" value="vat" >
<input type="button" class="rem btn-danger" value=" x " >
<br>
`);
$('#addhere #div'+counter+' select').attr({'name':'product_id[]','id':'dynamic'+counter,'onchange':'fun(this)'});
$('#addhere #div'+counter+' input:nth-child(2)').attr({'id':'quantity'+counter,'onchange':'fun2(this)' ,'name':'quantity[]'});
$('#addhere #div'+counter+' input:nth-child(3)').attr('id','price'+counter);
$('#addhere #div'+counter+' input:nth-child(4)').attr('id','vat'+counter);
$('#addhere #div'+counter+' input:nth-child(5)').attr('id','rem'+counter);
// $('#total-tr #div'+counter+' select').append('<option disabled selected>choose</option>');
$('#addhere #div'+counter+' select').append('<option></option>');
$('#addhere #div'+counter+' select option:last').attr( 'value','1');
$('#addhere #div'+counter+' select option:last').text('coffe');
// $('#addHere #div'+counter+' input:nth-child(2)').attr('max','');
counter++;
});
});
$(document).on('click', '.rem', function(){
$(this).closest('div').remove();
});
function fun(index){
let productID=index.value;
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
method:'GET' ,
url:"http://127.0.0.1:8000/priceTwo" ,
data:{productID: productID} ,
// dataType:'json' ,//return data will be json
success: function (op) {
index.parentNode.childNodes[5].value=op;
}
});
}
function fun2(index){
let quantity=index.value;
let productID=index.parentNode.childNodes[1].value;
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
method:'GET' ,
url:"http://127.0.0.1:8000/totalPriceUpdate" ,
data:{quantity: quantity, productID: productID} ,
// dataType:'json' ,//return data will be json
success: function (price) {
index.parentNode.childNodes[5].value=price;
}
});
const $btnPrint = document.querySelector("#btnPrint");
$btnPrint.addEventListener("click", () => {
window.print();
});
}
</script>
<div class="col-12" style="margin-top: 40px">
<div class="card-body">
<div class="row">
<div class="column">
<form method="POST" action="/store-invoice">
<input type="hidden" name="_token" value="6etEUnX7s6uDP95slQb5FtBAZoyM3ffCCwmpdRvP">
<table class="table table-bordered border-primary">
<thead>
</thead>
<tbody>
<tr>
<td style="text-align:center">Date Of Supply</td>
<td><input type="date" format="%m/%d/%y" name="date_of_supply" id="datesupply" class="form-control">
</td>
<td style="text-align:center">تاريخ التوريد</td>
</tr>
<tr>
<td style="text-align:center">Branch</td>
<td><input type="text" name="branch" class="form-control">
</td>
<td style="text-align:center"> الفرع</td>
</tr>
<tr>
<td style="text-align:center">Salesman Name</td>
<td><input type="text" name="salesman_name" class="form-control"></td>
<td style="text-align:center"> اسم البائع</td>
</tr>
</tbody>
</table>
</form></div>
<div class="column">
<table class="table table-bordered border-primary">
<thead>
</thead>
<tbody>
<tr>
<td style="text-align:center">Invoice Number</td>
<td><input type="number" name="invoice_number" value="23" id="id_invoice_number"><input type="hidden" name="initial-invoice_number" value="33" id="initial-id_invoice_number"></td>
<td style="text-align:center"> رقم الفاتوره</td>
</tr>
<tr>
<td style="text-align:center"> Invoice issue Date</td>
<td><input type="text" name="invoice_issue_date" value="2022-03-21 18:28:37" id="id_invoice_issue_date"><input type="hidden" name="initial-invoice_issue_date" value="2022-03-21 18:28:37" id="initial-id_invoice_issue_date"></td>
<td style="text-align:center"> تاريخ اصدار الفاتوره</td>
</tr>
<tr>
<td style="text-align:center"> Page No</td>
<td><input type="text" name="page_number" class="form-control">
</td>
<td style="text-align:center"> رقم الصفحه</td>
</tr>
</tbody>
</table>
</div>
<div class="column">
<div class="card-body">
<div class="row">
<div class="col-md-6">
<table class="table table-bordered">
<thead>
<tr><th></th>
<th></th>
<th style="text-align:right">المورد</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center">Name</td>
<td><input type="text" class="form-control" value="cbc" style="height:25px" readonly="">
<input type="text" class="form-control" name="seller_id" value="1" style="height:25px" hidden="">
</td>
<td style="text-align:center">الاسم</td>
</tr>
<tr>
<td style="text-align:center">Building No</td>
<td><input type="text" class="form-control" value="6" style="height:25px" readonly="">
</td>
<td style="text-align:center">رقم المبني</td>
</tr>
<tr>
<td style="text-align:center">Street Name</td>
<td><input type="text" class="form-control" value="26 uolyo" style="height:25px" readonly="">
</td>
<td style="text-align:center">اسم الشارع</td>
</tr>
<tr>
<td style="text-align:center">District</td>
<td><input type="text" class="form-control" value="26yolyo" style="height:25px" readonly="">
</td>
<td style="text-align:center"> الحي</td>
</tr>
<tr>
<td style="text-align:center">City</td>
<td><input type="text" class="form-control" value="cairo" style="height:25px" readonly="">
</td>
<td style="text-align:center"> المدينة</td>
</tr>
<tr>
<td style="text-align:center">Country</td>
<td><input type="text" class="form-control" value="Egypt" style="height:25px" readonly="">
</td>
<td style="text-align:center">البلد</td>
</tr>
<tr>
<td style="text-align:center">Postal Code</td>
<td><input type="text" class="form-control" value="97556" style="height:25px" readonly="">
</td>
<td style="text-align:center">الرمز البريدي</td>
</tr>
<tr>
<td style="text-align:center"> Additional No</td>
<td><input type="text" class="form-control" value="566" style="height:25px" readonly="">
</td>
<td style="text-align:center"> رقم اضافي للعنوان</td>
</tr>
<tr>
<td style="text-align:center">VAT Number</td>
<td><input type="text" class="form-control" value="545" style="height:25px" readonly="">
</td>
<td style="text-align:center">الرقم الضريبي</td>
</tr>
<tr>
<td style="text-align:center">Other Seller ID</td>
<td><input type="text" class="form-control" value="4" style="height:25px" readonly="">
</td>
<td style="text-align:center"> معرف اخر</td>
</tr>
</tbody>
</table>
</div>
<div class="column">
<table class="table table-bordered">
<thead>
<tr><th>Buyer</th>
<th></th>
<th style="text-align: right">العميل</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center">Name</td>
<td><input type="text" name="name" class="form-control" style="height:25px">
</td>
<!-- <td></td>-->
<td style="text-align:center">الاسم</td>
</tr>
<tr>
<td style="text-align:center">Building No</td>
<td><input type="number" name="building_no" class="form-control" style="height:25px"></td>
<td style="text-align:center">رقم المبني</td>
</tr>
<tr>
<td style="text-align:center">Street Name</td>
<td><input type="text" name="street_name" class="form-control" style="height:25px"></td>
<td style="text-align:center">اسم الشارع</td>
</tr>
<tr>
<td style="text-align:center">District</td>
<td><input type="text" name="district" class="form-control" style="height:25px"></td>
<td style="text-align:center"> الحي</td>
</tr>
<tr>
<td style="text-align:center">City</td>
<td><input type="text" name="city" class="form-control" style="height:25px"></td>
<td style="text-align:center"> المدينة</td>
</tr>
<tr>
<td style="text-align:center">Country</td>
<td><input type="text" name="country" class="form-control" style="height:25px"></td>
<td style="text-align:center">البلد</td>
</tr>
<tr>
<td style="text-align:center">Postal Code</td>
<td><input type="number" name="postal_code" class="form-control" style="height:25px"></td>
<td style="text-align:center">الرمز البريدي</td>
</tr>
<tr>
<td style="text-align:center"> Additional No</td>
<td><input type="number" name="additional_number" class="form-control" style="height:25px"></td>
<td style="text-align:center"> رقم اضافي للعنوان</td>
</tr>
<tr>
<td style="text-align:center">VAT Number</td>
<td><input type="number" name="vat_number" class="form-control" style="height:25px"></td>
<td style="text-align:center">الرقم الضريبي</td>
</tr>
<tr>
<td style="text-align:center">Other Buyer ID</td>
<td><input type="number" name="other_buyer_id" class="form-control" style="height:25px"></td>
<td style="text-align:center"> معرف اخر</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="card-body contact-form ">
<div class="row">
<div class="col-md-12">
<div class="panel-heading">
<h4 class="panel-title"><strong>Order Summary</strong></h4>
</div>
<span>اضافة عنصر </span>
<input type="button" class="btn btn-outline-dark " id="add" value="اضافة">
<div class="panel-body">
<div class="table-responsive">
<div class="form-group row form-inline mb-2">
<span style="margin: 30px" class="form-control"> المنتج </span>
<input value="العدد" type="text" min="1" class="form-control" disabled="">
<input value="السعر" type="text" class="form-control mo" disabled="">
</div>
<div id="addhere"><div class="form-group form-inline mb-2" id="div1">
<select class="aya form-control " name="product_id[]" id="dynamic1" onchange="fun(this)"><option value="1">coffe</option></select>
<input value="1" type="number" min="1" class="form-control" id="quantity1" onchange="fun2(this)" name="quantity[]"><input type="text" class="form-control mo" id="price1">
<input type="checkbox" class="form-control mo" value="vat" id="vat1">
<input type="button" class="rem btn-danger" value=" x " id="rem1">
<br>
</div><div class="form-group form-inline mb-2" id="div2">
<select class="aya form-control " name="product_id[]" id="dynamic2" onchange="fun(this)"><option value="1">coffe</option></select>
<input value="1" type="number" min="1" class="form-control" id="quantity2" onchange="fun2(this)" name="quantity[]">
<input type="text" class="form-control mo" id="price2">
<input type="checkbox" class="form-control mo" value="vat" id="vat2">
<input type="button" class="rem btn-danger" value=" x " id="rem2">
<br>
</div></div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<button id="btnPrint" class="hidden-print">
Print
</button>
<button type="submit" class="btn btn-primary btn-block">
<i class="fa-solid fa-bookmark"></i>
Close Bill
</button>
<a href="/invoice/list/" class="btn btn-primary btn-block float_right"><i class="fa-solid fa-list"></i>
Back to list</a>
</div>
</div>
</div>
</div>
</div>
</body>
@Sinnbeck I realized now the form tag was closed in browser(I don't know why), that is why the rest of code wasn't in the form
@MonaEid sounds like your elements aren't matched correctly. All elements needs to be closed in the right scope. If not the browser will try to guess
@Sinnbeck yes it's, the html not mine, and the project I should rebuild it.
@monaeid the only data in the Request payload is quantity and productID keys (depending on the function called)
{
data:{quantity: quantity, productID: productID},
}
How does product_id get into the Request payload?
If you dd($request->all()) what do you see?
@tykus the rest of html includes form and all data in the request payload accept product_id
so unhelpful
@foreach($category as $category);
<option value="{{$category->category_name}}">{{$category->category_name}}</option>
@endforeach
my code
i want to help with this also i get the same error : public function searchFacilities() { if (!isset($_GET['query'])) { // Handle the case when the 'query' parameter is missing $response = [ 'status' => 'Fail', 'error' => 'Missing query parameter' ]; header('Content-Type: application/json'); echo json_encode($response); return; }
$query = $_GET['query'];
$query = '%' . $query . '%';
// Search facilities by facility name, tag name, or location city
$searchQuery = "SELECT f.name, f.creation_date, l.city, l.address, l.zip_code, l.country_code, l.phone_number, GROUP_CONCAT(t.name SEPARATOR ', ') AS tags
FROM facility AS f
JOIN location AS l ON f.location_id = l.id
LEFT JOIN facility_tag AS ft ON f.id = ft.facility_id
LEFT JOIN tag AS t ON ft.tag_id = t.id
WHERE f.name LIKE :query
OR t.name LIKE :query
OR l.city LIKE :query
GROUP BY f.id";
$facilities = $this->db->executeQuery($searchQuery, ['query' => $query]);
// if ($facilities === false) {
// // Handle the database query error
// $response = [
// 'status' => 'Fail',
// 'error' => 'Error executing the database query'
// ];
// header('Content-Type: application/json');
// echo json_encode($response);
// return;
// }
$response = [];
foreach ($facilities as $facility) {
$response[] = [
'name' => $facility['name'],
'creation_date' => $facility['creation_date'],
'location' => [
'city' => $facility['city'],
'address' => $facility['address'],
'zip_code' => $facility['zip_code'],
'country_code' => $facility['country_code'],
'phone_number' => $facility['phone_number']
],
'tags' => explode(', ', $facility['tags'])
];
}
header('Content-Type: application/json');
echo json_encode($response);
}
Warning: foreach() argument must be of type array|object, bool given in C:\xampp\htdocs\web_backend_test_catering_api\App\Controllers\IndexController.php on line 320
Please or to participate in this conversation.