1myb
1myb

Reputation: 3596

Filter pivot table on Laravel Blade

I have tables - products and categories with a pivot table. What i need to do is to display the categories on view in a tab and get the respective products.

// Product.php
public function categories(){
    return $this->belongsToMany('App\Models\Category');
}

// Category.php
public function products(){
    return $this->belongsToMany('App\Product');
}

// IndexController.php
$products = Product::where('status', StatusConstant::PT_ACTIVE)
                ->with(['joindraw' => function ($query){
                        $query->where('user_id', $this->user->id);
                }])->get();

return view('store.index', ['products' => $products, 'category' => $category]);

and I'm expecting some output like this:

<div class="row">
    <div class="col s12">
        <ul class="tabs">
            <li class="tab"><a class="" href="#tabs1">Category 1</a></li>
            <li class="tab"><a class="active" href="#tabs2">Category 2</a></li>
            <li class="tab"><a class="" href="#tabs3">Category 3</a></li>
        </ul>
    </div>
    <div id="tabs1" class="col s12">
        <div class="contents-tabs">
            <div class="table-contents default-table">
              //Products in category id 1...

May i know how can i do the filter on blade?

Upvotes: 0

Views: 546

Answers (2)

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

Instead of sending products to the view, consider sending categories with related products to the view.

Controller

$categories = Category::with(['products' => function ($query) {
    $query->where('status', StatusConstant::PT_ACTIVE);
    $query->with(['joindraw' => function ($query) {
        $query->where('user_id', $this->user->id);
    }]);
}])->get();

return view('store.index', ['categries' => $categries]);

Now you can loop through each category and access its related products.

Blade

<div class="row">
    <div class="col s12">
        <ul class="tabs">
            @foreach($categries as $category)
                <li class="tab"><a class="" href="#{{ $category->id }}">{{ $category->name }}</a></li>
            @endforeach()
       </ul>
    </div>

    @foreach($categories as $category)
        <div id="{{ $category->id }}" class="col s12">
            <div class="contents-tabs">
                <div class="table-contents default-table">
                    <!-- your code goes here -->
                    <!-- Imagine you wanna list product names. -->

                    <ul>
                        @foreach($category->products as $product)
                            <li>{{ $product->name }}</li>
                        @endforeach()
                    </ul>
                </div>
            </div>
       </div>
    @endforeach()
</div>

Upvotes: 3

ramin ashrafimanesh
ramin ashrafimanesh

Reputation: 1062

You can group products per categories:

$categoryProducts = [];
foreach($products as $product){
    foreach($product->categories as $category){
        if(!isset($categoryProducts[$category->id])){
            $categoryProducts[$category->id] = [];
        }

        $categoryProducts[$category->id][$product->id]=$product;
    }
}

Then you can show each category products in their tabs with category id.

Upvotes: 1

Related Questions