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

Kanchan186's avatar

Invalid argument supplied for foreach()

controller

public function store(Request $request)
    {
        
      foreach(request('sg') as $sg_id)
      {
         foreach(request('br') as $br_id)
              {

                $brands=Brands::where('brands.segment_id', $sg_id)->first();

                   if( $sg_id==$brands->segment_id)
                     
                           { 
                              MechBrand::create([
                             'mechanic_id'=>request('mechanic_id'),     
                           'active_status'=>1,
                             'segment_id'=>$sg_id,
                             'brand_id' => $br_id,
        
                            ]);
                         }
                    }
                 }  
                 
      $mechanic=Mechanic::get();
        $segment=Segment::get();
        $brand=Brands::get();
        
        array($request->get('segment_id'));
        array($request->get('brand_id'));

      //dd('hi');
        //return 'Ok';
        return redirect('mechanic/view');
    }

view

<form class="m-t-40" method="post" action="{{url('/')}}/mechanic_brands/{{$mechanic->mechanic_id}}" enctype="multipart/form-data">
                                    {{csrf_field()}}

                                
                                      <div class="form-group">
                                        <h5>Segment<span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                @foreach($segment as $sg)
                                <ul><li>
                               <input type="checkbox" value="{{$sg->segment_id }}" name="sg[{{$sg->segment_id }}]" id="{{$sg->segment_id }}" onclick="getBrand1(this.id)" class="test">

                                <label  for="{{ $sg->segment_id }}">
                                <p>{{$sg->segment_name }}</p></label></li></ul>
                                
                                @endforeach
                                        </div>

                                       
                                    </div> 

                                
                                     <div >
                                        <table id="brands" >
                                         <tr>
                                         <td>
                                         <input type="checkbox">
                                         </td> 
                                         </tr>  
                                       

                                        </table>
                                   
                                     </div>
                                     

                              
                                               
                                            <div class="text-xs-right">
                                                <button type="submit" class="btn btn-info">Submit</button>
                                                <button type="reset" class="btn btn-inverse">Cancel</button>
                                            </div>
                                </form>
                                </div>
                                </div>
                            </div>
                        </div>
0 likes
30 replies
fylzero's avatar

Try...

$request->sg

and...

$request->gr

If they are objects you'll need to type hint them as arrays or json_decode?

(array) $request->sg
1 like
Nakov's avatar

@kanchan186 it is failing on this line:

foreach(request('br') as $br_id)

You have no field in your view that has a name attribute ="br[]" so that returns null.

1 like
Kanchan186's avatar

@fylzero

i made changes

 public function store(Request $request)
  {
    dd(request()->all());
    //dd(request('sg'));
    //dd(request('br'));

    foreach((array)request('sg') as $sg_id)
    {
         foreach((array)request('br') as $br_id)
              {
                
                $brands=Brands::where('brands.segment_id', $sg_id)->first();

                     if( $sg_id==$brands->segment_id)
                     
                           { 
                          MechBrand::create([
                         'mechanic_id'=>request('mechanic_id'),   
                           'active_status'=>1,
                         'segment_id'=>$sg_id,
                         'brand_id' => $br_id,
    
                        ]);
                       }
                  }
                 }
        $mechanic=Mechanic::get();
      $segment=Segment::get();
      $brand=Brands::get();
      
        array($request->get('segment_id'));
        array($request->get('brand_id'));

     return 'Ok';
    //return redirect('employee_area_mapping/view');
  }

view

<div >
                                        <table id="brands" >
                                        <tr><td>
                                        <input type="checkbox" name="br[{{$br->brand_id}}]">
                                        </td></tr>    
                                        </table>
                                   
                                     </div>

getting output like this

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▼
    3 => "3"
  ]
  "brand_id" => "on"
]
Nakov's avatar

@kanchan186 this error happens when you don't have any checkbox selected. If you select some the array will be full..

So you can check if there is data for that key:

if(request()->has('br'))
{

   foreach (request('br') ...
}   
Kanchan186's avatar

i made changes and add br[ ]

 var markup = "<tr><td><input type='checkbox'name='br[<?php echo e($br->brand_id); ?>]'></td><td>" + value.brand_name + "</td></tr>";
                        

getting error

Undefined variable: br (View: C:\xampp\htdocs\partsanalysis\resources\views\backend\mechanic_brands\addMechBrands.blade.php)
Nakov's avatar

@kanchan186 this is fine

<input type="checkbox" name="br[{{$br->brand_id}}]">

I was just suggesting that in your first view that you have shared there was no element with that name attribute.

Kanchan186's avatar

@nakov

sir i add name="br[{{$br->brand_id}}]"

in script because checkboxes are called from script

 $.each(result, function(index, value) { 
                        //alert(value); 
                        // $("#brands").html('<input type="checkbox" />'+value ); 
                var markup = "<tr><td><input type='checkbox'name='br[{{$br->brand_id}}]'></td><td>" + value.brand_name + "</td></tr>";
                        
                        $("#brands").append(markup);

                      });

its gives error now

Undefined variable: br (View: C:\xampp\htdocs\partsanalysis\resources\views\backend\mechanic_brands\addMechBrands.blade.php)
Nakov's avatar

@kanchan186 if you use javascript for that, what is $br then.. you should use the values from the script itself and add them to the element:

 $.each(result, function(index, value) { 
                        //alert(value); 
                        // $("#brands").html('<input type="checkbox" />'+value ); 
    var markup = "<tr><td><input type='checkbox'name='br[' + value + ']'></td><td>" + value.brand_name + "</td></tr>";
                        
    $("#brands").append(markup);
});

see I changed {{$br->brand_id}} with value but I don't know which value you have to have there.. take the correct one from the results.

Kanchan186's avatar

@nakov sir, it gives output

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▼
    10 => "10"
  ]
  "br_" => null
]
Kanchan186's avatar

@nakov

var markup = "<tr><td><input type='checkbox' name='br[]' id='brand_id'></td><td>" + value.brand_name + "</td></tr>";

it gives output

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▼
    3 => "3"
  ]
  "br" => array:2 [▼
    0 => "on"
    1 => "on"
  ]
]
Nakov's avatar

@kanchan186 you need to add the id of the brand in the array, so not just empty br[] because that one uses the incremented index.. you see 0 and 1.. and on just says that it was checked which is good, but you will have to add the brand id there from your results array.

Nakov's avatar

@kanchan186 something like this:

var markup = "<tr><td><input type='checkbox'name='br[' + value + ']'></td><td>" + value.brand_name + "</td></tr>";

I really don't know what you have in your results there. Just try it out.. see which id you need to use instead of value above.

Kanchan186's avatar

@nakov

if i add name='br['+ value +']' gives

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▶]
  "br_" => "null"
]

if i add name='br['+ brand_id+']' gives

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▶]
  "br_" => "on"
]
Nakov's avatar

@kanchan186 omg, can you please tell me what you have here:

 $.each(result, function(index, value) { 

    console.log(result, index, value);
 
    var markup = "<tr><td><input type='checkbox'name='br[' + value + ']'></td><td>" + value.brand_name + "</td></tr>";
                        
    $("#brands").append(markup);
});

Open your browser console, and you will see the result.

Nakov's avatar

@kanchan186 so what do you have in your result then? Why don't you use regular blade ? Passing the brands with the view, the same as you do for the segment?

Kanchan186's avatar

@nakov

alert(result);

gives

[object Object],[object Object],[object Object]
Nakov's avatar

@kanchan186 as long as you are not following what I am asking, then please try it yourself.

Kanchan186's avatar

@nakov please sir help me! i will follow you.. but i cant understand what are you saying?

Nakov's avatar

@kanchan186 I am saying add this in your code:

 $.each(result, function(index, value) { 

    console.log(result, index, value);
 
    var markup = "<tr><td><input type='checkbox'name='br[' + value + ']'></td><td>" + value.brand_name + "</td></tr>";
                        
    $("#brands").append(markup);
});

and open the browser console using Inspect element, and share the result. What do you have in the result object, it is not visible for me, it is for you..

Kanchan186's avatar

in console gives output

(3) [{…}, {…}, {…}]
 (3) [{…}, {…}, {…}] 0 {brand_name: "Bajaj", brand_id: 6}Add:996
 (3) [{…}, {…}, {…}] 1 {brand_name: "Honda", brand_id: 8}Add:996
(3) [{…}, {…}, {…}] 2 {brand_name: "Piaggio", brand_id: 9}Add:996
Nakov's avatar

@kanchan186 so you see use this then:

var markup = "<tr><td><input type='checkbox' name='br[" + value.brand_id + "]' value='" + value.brand_id + "'></td><td>" + value.brand_name + "</td></tr>";
Kanchan186's avatar

using above code gives output

(3) [{…}, {…}, {…}] 0 {brand_name: "Bajaj", brand_id: 6}
(3) [{…}, {…}, {…}] 1 {brand_name: "Honda", brand_id: 8}
 (3) [{…}, {…}, {…}] 2 {brand_name: "Piaggio", brand_id: 9}

using dd(request()->all());

output

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▼
    3 => "3"
  ]
  "br" => array:2 [▼
    6 => "on"
    8 => "on"
  ]
]
Nakov's avatar
Nakov
Best Answer
Level 73

@kanchan186 I edited my answer above to add the value attribute as well, try this:

var markup = "<tr><td><input type='checkbox' name='br[" + value.brand_id + "]' value='" + value.brand_id + "'></td><td>" + value.brand_name + "</td></tr>";
Kanchan186's avatar

@nakov

it gives

array:3 [▼
  "_token" => "EuwIVF9VQ05E75nhFjO6zzf4Bfan9vVkYv9wobkv"
  "sg" => array:1 [▼
    3 => "3"
  ]
  "br" => array:2 [▼
    8 => "8"
    9 => "9"
  ]
]

Please or to participate in this conversation.