-1

I created a site by laravel and I want to show categories and relevant products names.

i have tow array and like this

$category  = [
    '1' => 'cat1',
    '2' => 'cat2'
];

$product = [
    '0' => array(
     'name' => 'product1',
     'category_id' => '2'
),
    '1' => array(
     'name' => 'product2',
     'category_id' => '2'
),
    '2' => array(
     'name' => 'product3',
     'category_id' => '2'
),
    '3' => array(
     'name' => 'product4',
     'category_id' => '1'
),
    '4' => array(
     'name' => 'product5',
     'category_id' => '1'
),
    '5' => array(
     'name' => 'product6',
     'category_id' => '2'
),
    '6' => array(
     'name' => 'product7',
     'category_id' => '2'

]; 

and i can showing them like this


<h3>cat1</h3>
<p>product4</p>
<p>product5</p> 
<hr>
<hr> 
<h3>cat2</h3>
<p>product1</p>
<p>product2</p>
<p>product3</p>
<p>product6</p> 
<p>product7</p> 

by this code


// controller


public function someMethod(){
    $category  = [
        '1' => 'cat1',
        '2' => 'cat2'
    ];

    $product = [
        '0' => array(
         'name' => 'product1',
         'category_id' => '2'
    ),
        '1' => array(
         'name' => 'product2',
         'category_id' => '2'
    ),
        '2' => array(
         'name' => 'product3',
         'category_id' => '2'
    ),
        '3' => array(
         'name' => 'product4',
         'category_id' => '1'
    ),
        '4' => array(
         'name' => 'product5',
         'category_id' => '1'
    ),
        '5' => array(
         'name' => 'product6',
         'category_id' => '2'
    )]; 

    $category_collection = collect($product)->groupBy('category_id')->toArray();

    return view('your_blade_name',compact('category_collection','category'));


// view


@foreach($category_collection as $category_id => $data)
    <h3>{{ $category[$category_id] }}</h3>
    @foreach($data as $current_data)
        <p>{{ $current_data['name'] }}</p>
    @endforeach
<hr>
<hr> 
@endforeach
}

i can't limit this product. and my code showing all products with parent category in the section.

i want limit them so that the product count never exceeds 4 be.

if you can write laravel code but if you cant just write php code

3
  • What did you do for achieving your goal? Commented Nov 27, 2019 at 11:23
  • i just create a controller and view and inserting the codes Commented Nov 27, 2019 at 11:30
  • Yeap, I know how you did that. Commented Nov 27, 2019 at 11:33

2 Answers 2

0

You can use this @break with some condition. This also can helpful https://laravel.com/docs/5.8/blade#the-loop-variable

@foreach($category_collection as $category_id => $data)
    <h3>{{ $category[$category_id] }}</h3>
    @foreach($data as $current_data)
        @if($loop->iteration > 5) // or other logic
            @break
        @endif
        <p>{{ $current_data['name'] }}</p>
    @endforeach
    <hr>
    <hr>
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

0

This will Works.

foreach($category as $ck => $catval){
    foreach($product as $val){   
      if($val['category_id'] == $ck){
        $array[$catval][] =  $val;
      }
    }
  }
  foreach($array as $k => $value){
    echo '<h3>'.$k.'</h3>';
    if(count($value) > 0){
      foreach($value as $productval){ 
        echo '<p>'.$productval['name'].'</p>';
      }
    }
  }

OUTPUT:

cat1
product4

product5

cat2
product1

product2

product3

product6

product7

1 Comment

The output is not limited. Also, this is duplicated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.