Introduction to Backend Filters
In the previous episode we created lists for our movies and in this episode we are going to tackle a subject that is very closely related to that and there are filters. So we are going to create filters that are going to be right here on our list so we can filter that list by maybe genres or created the date or if the movies published or not and so on. So first of all we are going to populate this database and then I'm going to show you how you can create those filters.
Extending the Database and List Columns
Okay, so let's expand our database a little bit. So we are going to go to plugins, movies, and then we are going to go to this watch learn movies database and I'm going to add two new fields right here. One is going to be created_at and it's going to be a timestamp and it's going to be nullable because we want to fill it in. Then I'm going to create a published field and it's going to be a boolean field. So it's going to be 'is the movie published or not' and it's also going to be nullable and we just click Save. Okay, save and apply.
Now we go to a model, to our columns. I'm going to remove this poster column because we don't need it. We already have the genres. Let's add another column for created_at. And I think that this is going to be it. Okay, let's add another column and it's going to be published. The type of this column is going to be switch because it's going to be either true or false and that's it. Save this.
Now of course if we go to our movies we can see those columns right here. So created_at and published is defaulting to no. So we can see those columns but there are nothing there, nothing in it.
Populating Data with a Faker Route
So to correct that, I'm going through to this URL, populate movies, and I'm going to click enter. Now if we go to our movies and refresh this, we can see that now all the genres field, created_at is also filled and published is sometimes yes and sometimes no. So how did we achieve this? So we already did the episode where we used routes with October CMS to populate our database, and behind the scenes I actually created one of those routes. So you go watch learn movies and then in the routes file, I first use this genre method right here and then I set the route of populate movies which is going to use a faker plugin. And with that faker plugin it is going to add some genres to our movies and also populate created_at and published fields in our database, and that's about it.
Of course all of this is going to be available to you on Github. I didn't want to write all of this because we already did the episode about populating your database. So if you want to check out this code, just go to the Github, joint will be in the description below.
Okay, one more thing I did behind the scenes, I just add some genres: thriller, romance, drama, documentary, comedy, and action. And that's about it. So now we have our database filled and now we can create some filters and test them out.
Configuring the First Filter: Genres
So how do you create filters? Well first of all you go to your plugin, so watch learn movies and then you go to controllers, not models like we did in the episode before. So you go to controllers, movies, and then you have this config_list.yml file. You click on it and right here you add filter. You don't actually add a filter, you add the reference to the filter file. And our filter file is going to be called config_filter.yml. Okay save it. And now of course we have to create this file. So I just do this and now we have this file setup.
So first of all, let's try to add a filter for filtering our genres. So you start the yml file with scopes and then we set the name of the filter. So this is a machine readable name, genres. So the label for that is going to be genre. Okay. Now we have to define the model class for our genres. So that's going to be watchlearn\movies\models\Genre. And now you define a condition. So the condition is going to be the ID of the movie that has the movie ID in our movies_genres pivot table. So you would write it something like ID, so select movie_id from this pivot table. So we have this pivot table right here. So builder, if we go watchlearn_movies_movies_genres, so this is the relationship between the movie and the genre of the movie. So we can just copy the table name, paste it in. So find that movie that also has where genre_ID is in filtered. So 'is in filtered' means you would get a widget that you can click and choose the genres. So it has to find that ID in this data table and also that ID should be connected to the movie ID and then we display the. So this ID means the movie that has that specific genre. So it may be a little bit complicated to comprehend but this is just an SQL query that you write in this YAML file to get the movies that you need. And then you just get the name from, so the name from is going to come from genre_title field. Okay and that should be it. So that this is just the name of our genres.
Debugging and Using the Genre Filter
So if we go to movies right now, you can see this genre widget right here and if I click on it, I get all the all genres that we have. So if I click on the action and click here. So I probably forgot something in the query. It should be 'from' right here and also this should be 'in'. I don't know why I written 'end'. So this should be 'in'. Let's try to save it. Click okay. Reset this.
Okay, now we get all the movies. So let's try to filter out just action movies. Now as you can see we get all the movies that have action. Okay, we can also set a few conditions, so we can set the drama. Now we get all the action movies and also all the drama movies and so on. So this is the way this type of filter would work. We can of course remove all of them and that's it.
Implementing a Date Range Filter
Okay, next thing we want to do is, let's try to create a date range for the movie, so for this created_at date. So what we can do, we go right here and do created_at. Then we set the label to be date, so date range actually.
Okay, next thing we set the type of the widget to date_range and we set the condition. So it's going to find dates that are after and before some data that we choose. So these are the fields after and before as you will see right now. So if we save this, go right here, refresh this page, now we get this another widget, and date range is all period. If we click right here we can set the after date, so this is one right here, and before date. So let's say it's going to be after March 8th of 2012 and before today, so 26th of March 2017. Filter. And now as you can see we get all the dates that are between March 8, 2012 and March 26, 2017.
Adding a 'Published' Checkbox Filter
Let's try to create one more filter and that is going to be the published. So we have this 'published yes/no' and so on. We can reset this date. So we want to set a filter that will display all the movies that are published. So you may want to have a published switch on your posts, movies, or whatever. And then you can in the front end say something like if the movie is published then display the movie, if it's not then don't display it. So, published. And now the label is going to be 'Show published'. The type is going to be checkbox and the condition is going to be that published is equal to 1.
Okay, save it. Now if we refresh this, now we should get published yes, yes, yes, no, yes, no, and so on. So if you click this 'Show published' then you would get only the movies that are published. As you can see, we just get all the yeses right here. If we turn it off, then it will be yes, no, yes, no, and so on.
Conclusion and Resources
Okay so these are just few examples of backend filters. Of course there are more of them. You can check some examples right here. You can even create your custom filters if you need something complicated filtered in your back-end. So this has been it for this episode. Remember everything we did here will be available for you on github. Also if you want to ask me questions you can do so on Twitter, Facebook, Github, YouTube, my site and so on. If you maybe want to throw some money my way you can use the Patreon page for that. And of course don't forget to like the video if you liked it. If you like the content I put out, maybe you should subscribe to my channel. So this has been it guys. Thank you for watching and I will see you in the next episode.