در حال بارگذاری ...

Defining Nova Resources0:01

Okay, before we jump in to the episode, there's a question I'd like to tackle, and that is, what is a resource? If you think about it, we use resources in Laravel all the time. Resource controllers, for example. Now, what are you trying to do when you create a ResourceController? You have operations that you need to execute on a model, create, index, show, update, delete, and you need some way to define how all of those actions should take place. Now, Nova is no different. We have to inform Nova how it should display a book, for example, display an index of books, create a new book, update an existing book, delete a book. We need to tell Nova how it should handle those situations. And Nova's resources allow us to define that logic in a very simple and clean manner. So, in this episode, I'm going to show you the resource that Nova ships with out of the box,

Touring User Resource0:48

And Nova's resources allow us to define that logic in a very simple and clean manner. So, in this episode, I'm going to show you the resource that Nova ships with out of the box, that's the User resource, and then we'll create our very own resource for a Book. Let's dive in. Alright, so in our project, if we go to App, Nova, there's a file called user.php, which was created when we ran php artisan nova:install, and you'll see that this backs the User model here. I'm going to scroll down to the fields method, which is where I want to quickly highlight how a Nova resource is put together, and you'll see there are various fields that Nova automatically ships with for a User. An ID field, where obviously every user in our database has a unique ID.

and you'll see there are various fields that Nova automatically ships with for a User. An ID field, where obviously every User in our database has a unique ID. A Gravatar, if you've not heard of Gravatar, I imagine most of you have, but Gravatar is a service that allows you to globally tie a picture, an avatar, to your email address. This is a field that Nova provides to very quickly set up that functionality without having to store images yourself for avatars. We have a name field, we have an email field, and then we have a password field. And you can see all of this inside Nova itself. Inside the User's resource, here's the ID, the Gravatar, the name, the email, and of course the password shouldn't show on the index or show routes,

Generating Book Resource2:06

Inside the user's resource, here's the ID, the Gravatar, the name, the email, and of course the password shouldn't show on the index or show routes, but when we click edit, there is the password. So all of this comes together to form the User resource, and with just those few fields, we're able to define all of this information in the Nova UI. Let's move on to creating our very own Book resource. To create a new resource from the terminal, you can type php artisan nova:resource followed by the name of the resource you want to create, and I recommend copying the name of the model the resource represents, in our case Book. If we go into the IDE now and head into that Book resource that we've just created,

and I recommend copying the name of the model the resource represents, in our case Book. If we go into the IDE now and head into that Book resource that we've just created, you'll see something very similar to the User resource. Nova fills out a lot of detail for you. It already guesses that we want to use the Book model, but if that's incorrect in your case, you can obviously change that to the fully qualified class name of the model you want to represent. It uses ID as title. Well, what's the title? The single value that should be used to represent the resource when being displayed. So let's imagine you're globally searching for a Book.

Configuring Title and Search3:14

The single value that should be used to represent the resource when being displayed. So let's imagine you're globally searching for a book. Currently, it will use the ID of the book to display the result. That's not very good. Thankfully, a book has a better property in our database, the title of the book. That makes much more sense. So we'll update that here and now. The search array defines which columns should be searched on when you're using global or localized search. And when I say global or localized search, by the way,

when you're using global or localized search. And when I say global or localized search, by the way, I'm referring to this search box here and this search box up here. This is our local search for a resource. This is our global search across all resources. In the case of a book, yeah, we want to search on an ID, but it would make sense to also search on title. If I jump into this little route that I've created that displays all of the fields a book has, maybe blurb also makes sense. So in our IDE, we can add blurb.

maybe blurb also makes sense. So in our IDE, we can add blurb. And you can add as many fields as make sense in this search array. Just be aware that obviously the time it takes to perform the search query will increase if you add more and more fields. Let's now go down to the fields method. And note that by default, it just has the unique identifier for our book resource. However, this works. If I go into Nova and refresh, we automatically have a books item inside resources. By default, Nova will auto-register any new resources in the menu.

If I go into Nova and refresh, we automatically have a books item inside resources. By default, Nova will auto-register any new resources in the menu. You're free to tweak that, and we will be tweaking that in a later episode. But if I click into books, here is a list and index of all the books in our database. I can even search on the title. I have a book called 1984, and it searches and filters down to 1984. But it only shows the ID because that's the only field that we've displayed inside our fields method here. Now, what fields should we do in this episode? Well, looking at our JSON again, it would make sense to do title.

Adding Book Fields5:16

Now, what fields should we do in this episode? Well, looking at our JSON again, it would make sense to do title and maybe number of pages and number of copies. And over the course of the next few episodes, we'll be filling this book resource out further. Let's make a start with title. And I think it makes sense to use Laravel Nova's text field for this. We'll call make, and I'm just going to insert title there as the option. We'll jump back into the browser, refresh, head to books, and sure enough, here is the title of the book. We can go and view an individual book.

and sure enough, here is the title of the book. We can go and view an individual book. Note that it's lowercase. We'll fix that in a moment. And we can edit a book, and you can see we have the field for editing there as well. Now, the lowercase issue is caused because the first parameter you pass to a field is actually the display value. So if we want to display title, I want to display it with an uppercase T. Now, thankfully, Nova is smart enough to auto-convert this to lowercase when searching our model for the title property. So I don't need to add anything else to this.

when searching our model for the title property. So I don't need to add anything else to this. If I jump back and refresh, I have uppercase title, but it's still able to go and grab the value for us, and this works also on the index and on the show route. So that's how easy it is to add a text field. What other fields did we want to add? Well, we had numberOfPages and numberOfCopies. So let's go ahead and add those. For numberOfPages, it makes sense to use the number field that ships with Nova.

So let's go ahead and add those. For number of pages, it makes sense to use the number field that ships with Nova. We'll call the make command again, and this was number of pages. Notice I've written that out as I want it to be displayed, and now if I jump into the browser again and refresh, here is number of pages. What if you don't want the title here to be the same as the column in your database? Well, Nova allows for this by specifying the title, the display value, as the first parameter and the database column as the second parameter, in our case, number of pages. If we do that, note that we have pages as the display name now,

in our case, number of pages. If we do that, note that we have pages as the display name now, but we still show the correct value inside the output. For number of copies, we can do very much the same thing. We just want it to say copies in the UI, but it needs to grab the number_of_copies column from our database. If we jump back into our UI and refresh, here is copies. So how quick and easy was it to create and build an index of books, but not just an index, a show page of books, an edit of books, the ability to create a new book?

but not just an index, a show page of Books, an edit of Books, the ability to create a new Book? All of this you get for free by simply filling out the required fields inside your Nova resource. That's the power of Nova. That's why it's so quick to be able to build out these very professional administration panels because Nova is put together in such a way that in a few minutes, you can have all of the fields necessary to construct your resource, all of those CRUD operations with a single method filled out.

Demonstrating Global Search8:17

you can have all of the fields necessary to construct your resource, all of those CRUD operations with a single method filled out. Now that we have this in place, I just want to show off the title and search properties that we created earlier. If I head back to our books index and search for 1984, this works. You can see we are able to filter down to 1984, but I can also globally search for 1984. Note that the representation is the book title. This is where this title property comes in. If I was to change this to blurb, not a great idea,

This is where this title property comes in. If I was to change this to blurb, not a great idea, but for purposes of demonstration, and then refresh and search for 1984 again, yeah, now I'm showing the blurb for each book that returns as a search result for 1984. That's why it's so important that you set this title property and don't leave it at the default of ID because ID wouldn't be very useful when searching across all resources in your application.

because ID wouldn't be very useful when searching across all resources in your application. In this case, title makes a lot of sense, and that's what we'll leave it at. However, because I've set ID and blurb as search items, I am able to search on those things in global search. If I search for one, you'll see that it returns 1984 as a book, but it also returns Luke Downing because that user has an ID of one in our database. So using the search and title fields,

because that User has an ID of one in our database. So using the search and title fields, you're able to very quickly navigate around your resources in the Nova administration panel, and it takes very little setup to get to that point. So within just a few minutes, we have a comprehensive index of books. We're in a great position, but it's a bit bland. Note that on the ID field, we're able to sort by the ID, but we can't do that with title or pages or copies,

Previewing Field Customization9:59

Note that on the ID field, we're able to sort by the ID, but we can't do that with title or pages or copies, and there are a few other things that would be nice. I don't necessarily want to see the number of pages in each book on the index. That's going to be information that I don't need all the time, but I do want to see it on the show route. Thankfully, Nova provides methods that we can apply to fields in order to customize them to our heart's content. So in the next episode,

in order to customize them to our heart's content. So in the next episode, why don't we add a few methods to the fields we've created here in order to achieve our desired outcome?

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟