A
A
Alexander Koregin2020-08-04 16:30:48
Laravel
Alexander Koregin, 2020-08-04 16:30:48

How to make a filter in Laravel?

I have a collection of buildings and posts associated with these buildings are attached with them. Posts have a variety of "News => 2" and "Promotions => 1" how to filter correctly.

With this method I get all the buildings

public function stocksIndex() {
        return view('posts.index', [
            'page' => Page::whereRoute('stocks.index')->first(),
            'buildings' => Building::showInPosts()->get()
        ]);
    }


And in the blade I bring them out like this

@foreach ($building->posts as $post)
    <div class="grid__col-4 grid__col_md_6 grid__col_sm_1 mb-2">
        @include('posts.card', ['post' => $post])
    </div>
@endforeach


And I have such a collection

5f29624704a11217461447.png

. There is a variable . And it turns out that I need to immediately specify which category of posts will be displayed on the page, how can I filter it correctly? I tried this way, but something doesn't work"category_id" => 1



public function stocksIndex() {
        return view('posts.index', [
            'page' => Page::whereRoute('stocks.index')->first(),
            'buildings' => Building::showInPosts()->whereHas('posts', function($stocks) {
                $stocks->where('category_id', 1);
            })->get()
        ]);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question