Creating Published Filter 0:00Now that we have some more post models in our database, it becomes more and more complex to take a look at which of these posts are actually published, and to look at posts that are only in, for example, the news category. So what I want is I want to be able to only see the posts that are published, or filter the posts by their category. Luckily, Laravel Nova ships with a filter support. So let's see how we can add a filter to one of our resources. To create a filter, we can use the artisan command from Nova. So we have phpArtisan nova filter, and then just provide the name of the filter that we're going to create.So we have phpArtisan nova filter, and then just provide the name of the filter that we're going to create. So in my case, it will be postPublished. Now if I go to Sublime, we now have this Nova filters directory, and in there we have this postPublished class. And if we take a look at this, the class consists of two methods. The first one is the apply method. It receives the current request, we have a query builder, and it receives a value. And then we have this options method. And this option method will be used to display a number of options that the user can chooseAnd then we have this options method. And this option method will be used to display a number of options that the user can choose from in Nova. So what we want is, we want something like isPublished, and the value for this would be 1. And notPublished, and the value would be 0. And now in our apply method, we would apply this to the query builder by saying query where isPublished is equal to the value. So what the user chooses here will be given to this method as the value, and then we can use this to manipulate our Eloquent query. Registering Filter on Resource 1:59So what the user chooses here will be given to this method as the value, and then we can use this to manipulate our Eloquent query. Now we still need to define in our post resource that this resource knows about this specific filter. So go to our post resource, and if we scroll down, there is a method called filters. And right now it just returns an empty array. So let's change this by returning an instance of our new postPublished filter. So we're going to say return new postPublished, and import the class. And if we now go back and refresh the page, we can now go here, postPublished. And now we can filter between, okay, isPublished. Adding Category Filter 2:45And if we now go back and refresh the page, we can now go here, postPublished. And now we can filter between, okay, isPublished. Now we only have the two that are published, or notPublished. Or if we don't select anything, then we get all of them. So let's add the second filter as well. So I want to be able to filter the categories. So once again, phpArtisan, NovaFilter, postCategories. And this filter, we had a static array, so if you do this in a real application, you would probably look the category up in the database, but I just defined them as a static array in my post resource as well.would probably look the category up in the database, but I just defined them as a static array in my post resource as well. So if I go to my post resource and the fields, we had this. So tutorials and news, and that's the same I'm going to use here. So tutorials will be tutorials, and news will be news. And our query will be query where category is equal to the value. Okay, and in our post resource, we basically do the same thing. So we have the publish filter, and underneath we have the new postCategories filter. Import it, save the file, and if we go back to Nova, we now have two filters in place, so we can now filter for only published posts in our news category. Introducing Nova Lenses 4:18Import it, save the file, and if we go back to Nova, we now have two filters in place, so we can now filter for only published posts in our news category. Cool, that's great. Similar to filters, you can also define what Nova calls lenses. And lenses are basically separate views to a part of your resource data. So let's just create a lens, and then it will probably get a lot clearer what I mean. So to create a lens, we will once again use the CLI command, so phpArtisan, nova, lens. And with our lens, I want to display the posts that have the most number of tags associated to them. So let's just call this lens mostTags. Building Most Tags Lens 5:08to them. So let's just call this lens mostTags. Okay, now, back to Sublime. We now have this nova lenses directory, and in here we have this mostTags class. And if you take a look at the class, we have a query method where we can modify the query that is being used. We have a fields method where we can modify the complete output of this lens. And we can also define additional filters for this lens, and we can change the URL key. So what we want to do is, in our query, we will keep the width ordering so that people could sort by clicking on the columns.So what we want to do is, in our query, we will keep the width ordering so that people could sort by clicking on the columns. We will keep the width filters so that the filters will automatically be applied. And with the query, I want to get the count of my tags. And then I want to order by tagsCount descending. So that I have the post that has the most number of tags associated at the top. Now, for the fields, here we can totally define which fields we want to show for this specific view. So it does not use our post resource fields, but instead we can define custom fields that we want.So it does not use our post resource fields, but instead we can define custom fields that we want. So I still want to be able to show the title. So I will add a text, makeTitle. And then let's add a number field that we call numberOfTags. And this will reference to the database column, which is called tagsCount. Now let's import these classes, the number, all right. Now similar to the filters, we still need to define that this post resource has this mostTagsLens. So if we go back, here are the filters, and underneath we have these lenses. Registering Lens on Resource 7:18mostTagsLens. So if we go back, here are the filters, and underneath we have these lenses. So let's add a new instance of our mostTagsLens. So new mostTags, import the class. And if we now refresh, you can see that we have this new dropdown, which is called Lens. So if we click on it, we can see that we have most tags available. And once we go there, we have a completely new view that we defined in our Lens that now has the ID, the title, and the number of tags directly available. So lenses are a completely new view that you can use for your existing Nova resources. So with a combination of both filters and lenses that can also have filters, you haveSo lenses are a completely new view that you can use for your existing Nova resources. So with a combination of both filters and lenses that can also have filters, you have full control over how you want to display your resources and what you want your users to be able to see.