IxLOVExLOLIPOP
IxLOVExLOLIPOP

Reputation: 1

laravel my foreach only return me the last record in array

here is my code to store data into an array, but its not working, the page only show one element, which is the last element in the array. please help ty.

  $modProduct = array();
  if($product->product->subcategory->category->id==1){
    $modProduct[1] =  $product->product;
   }



  if($product->product->subcategory->category->id==4){
    $modProduct[4] =  $product->product;
   }


  if($product->product->subcategory->category->id==7){
    $modProduct[7] =  $product->product;
   }


  if($product->product->subcategory->category->id==10){
    $modProduct[10] =  $product->product;
   }


  if($product->product->subcategory->category->id==13){
    $modProduct[13] =  $product->product;
   }


  if($product->product->subcategory->category->id==16){
    $modProduct[16] =  $product->product;
   }

here is my output:

@foreach($modProduct as $index => $modProducts)
<div  ><a href="#tab-{{ $index }}" class="tab-nav" data-toggle="tab">
{{$modProducts->subcategory->category->name}}</a></div>

@endforeach

Upvotes: 0

Views: 470

Answers (1)

Suniti Yadav
Suniti Yadav

Reputation: 403

Please try with this -

$modProduct = array();

foreach($product->product as $key => $value)
{
  $product_arr = array('1','4','7','10','13','16');

  if (in_array($value->subcategory->category->id, $product_arr))
  {
        modProduct[$value->subcategory->category->i] =  $value;
  }

}

Hope this will work for you.

Upvotes: 1

Related Questions