Filtering in Laravel

I have a drop down menu as such:

<div class="">
              <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Dropdown link
              </a>
              <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                <a class="dropdown-item" href="/filter">Active</a>
                <a class="dropdown-item" href="/filter">Inactive</a>
                <a class="dropdown-item" href="{{url('Energy')}}">Something else here</a>
              </div>
            </div>

I wish to select an option from the first 2 items and it should filter the table to show the records based off that selection. In this case I have a a field called “Person” and “Person” can be active or inactive(which is depicted by 0 or 1- tinyInt). When I select Active from the dropdown list, only records that have a 1 in active should show. I am new to Laravel so I was hoping if you guys could tell me if I am on the right path.

my controller:

public function filter(){
            $energy = Energy::where('Person', 1)->get();
            return view('admin.add', ['energy' => $energy]);
                        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Ok so there’s for sure a bunch of ways to implement this, all with different considerations. A simple way to get you going here is to use url parameters:

// Your link could look like:
<a href="?active=1">Active</a>

Clicking this link in the browser will take the current url and add the params to it: http://example.com?active=1. You can also use the route helper to output the full url:

// This
<a href="{{ route('energy', ['active' => 1]) }}">Active</a>

// Becomes
<a href="http://example.com?active=1">Active</a>

Then in the controller method you can grab the parameters from the $request object:

// Make sure you import the request class
use IlluminateHttpRequest;


// In our method we check the request for the
// url parameter to use in our query
public function filter(Request $request)
{
    // Here we default to '0' if the request param isn't found
    $energy = Energy::where('person', $request->input('active', 0))->get()

    // Alternatively you could conditionally add the where clause by using the when helper
    // Here if $active is not set the where clause will not be added
    $active = $request->active;
    $energy = Energy::when($active, function ($query, $active) {
        return $query->where('person', $active);
    })->get();
}

Check the Laravel docs for conditional clauses here for more info on that when helper 👍🏼

Also worth noting avoid using uppercase characters in your table column names to save yourself some pain e.g. person not Person 😉

Like I said there’s a load of ways to go but hopefully that helps.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x