1
votes

I want to make drop-down list filtering.

I have a web page, that shown some post with title and categories.

The page has a drop-down in nav.blade.php. I dynamically generate drop-down from categories table. But when I select an item of drop-down (for example a category name) I want page to show me the posts only of that category. Also I have created the Category and Posts model and set the relationships. I can see all posts on my main page but can't filter content with drop-down list.

What I am doing wrong? and how can I solve this issue?

My nav.blade:

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
       aria-haspopup="true" aria-expanded="false">Dropdown
        <span class="caret"></span></a>
    <ul class="dropdown-menu">
        <li>@foreach($categories as $category)
                <a href="{{URL::route('home',$category->id)}}">
                    <option value="{{$category->id}}">{{ $category->name }}</option>
                </a>
            @endforeach
        </li>
    </ul>
</li>
1
I have corrected your question please read it and confirm, because your previous formulation was not understandable? - maytham-ɯɐɥʇʎɐɯ
ok thank you so much - minchevz
no problem, you should have told me that the following solution was working since I spend some time to make for you :) good luck - maytham-ɯɐɥʇʎɐɯ
i know but i just figured it out :( sorry mate . i thought havent started it yet :( im really sorry - minchevz
no problem, good luck any way - maytham-ɯɐɥʇʎɐɯ

1 Answers

1
votes

This would get you started:

Assuming you have a route like:

Route::get('/{category_id}', ['as'=>'home', 'uses'=>'PostController@show']);

In the PostController@show method:

public function show($category_id)
{
    $categories = Category::all();
    $selected_category = Category::with('posts')->where('id', $category_id)->first();
    $posts = $selected_category->posts;

    return redirect()->back()->with(compact('posts', 'categories'));
}

You can change the redirect location.