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