November 202312:5175.2k views

An Introduction to Laravel Nova

About

Nova is a beautifully-designed administration panel for Laravel. Carefully crafted by the creators of Laravel, Nova is designed to make you the most productive developer in the galaxy.

Transcript

Everything I said

00:03

I'm gonna give you a demo of how in just a few minutes you can add a powerful, robust, and beautiful back-end admin panel to your Laravel applications with Laravel Nova. Nova is the fastest and simplest way to add a back-end admin panel, uh, an admin panel that can empower your entire team, technical or not. So gone are the days of directly editing the data in the database or tinkering with scripts in production.

00:29

We're gonna expose a safe and reliable way to access and modify that data, we're gonna standardize some business processes, and we're gonna do it quickly. You don't wanna spend all day tinkering with an admin panel. You've got a business to run. So in this demo, we're gonna look at an app called Ticketly. It's just a standard, uh, Laravel application that is an event and ticketing application, and it doesn't even have Nova installed yet, and so that's where we're gonna start.

00:56

We'll start by running composer require laravel/nova, and this is gonna work for me 'cause I've already set up my Composer credentials. You can check the docs to see how to set up your credentials. If we clear this out now that that has run, we can run php artisan nova:install, and this will publish the service provider, assets, and a few other things. And then finally, we can run php artisan migrate, and that will add a few Nova specific tables to our database.

01:22

So if we switch over to the browser, we can now hit this /nova endpoint in our browser, and we'll see the beginnings of a dashboard. So if we take a look here, we see we get a lot of nice help text, but more importantly, we get this users resource over here. You can come in here and you can create a brand-new user, or you can search for an existing user and update their details.

01:44

You can come right in here and change their email address and update it right here. We're off to a pretty good start, right? We've installed Nova, we can hit it in the browser, and we already have a full CRUD interface for our users model, and we've done no work whatsoever, right? But it starts to get even more interesting when we start adapting it and molding it to our specific business, and so that's what I'm gonna show you now.

02:05

We're gonna start creating resources for our domain models, and this is gonna be a speedrun to show you what's possible. The docs are very good. This is not a tutorial. I just wanna show you a little bit of what you can do. So let's start by looking at some of our domain models here. If we look in our models directory, you'll see that we have event, order, ticket, and user, and Nova has already created that user resource for us.

02:28

So we're gonna create the other three. We're gonna create event, order, and ticket resources. Nova makes this very easy. It gives us a nova:resource command. So we'll say event, we'll say order, and we'll do ticket. Now, if we hop back over here, we see that we now have those resources down here. And a resource is a pretty fundamental concept in Nova.

02:50

You've got your eloquent models that you're familiar with, and then you have your Nova resources, and that's a one-to-one mapping. The resource is where you define what fields are available to, to the end user to see and to edit, what the rules are for creating and updating and deleting, and some other stuff like what kind of metrics do you wanna run on these, what kind of filters, what kind of actions.

03:10

All of that stuff is wrapped up in a Nova resource. Let's start with the event resource. And if we open that, we'll see that Nova has created this resource for us with just a single field of the ID. And we can confirm that by looking in the browser and clicking on the New Events link over here, and we see that just the ID has been added.

03:31

And so this is the part where we wanna start making this match our application. We're gonna start really simple. We're just gonna add a text field for the name column of this event. So this is gonna pull the name column out of the database and show it on the resource listing for events. Not only does it show it, but it also automatically added it so that we can come in here and edit it as well.

03:53

So with that one line of code, we've got the read and the update for this specific field. I'm gonna go ahead and add a bunch of the other columns just as text fields. You've got image, status, description, price, and then tickets available and sold. If we look at this in the browser, it's kind of helpful. It's not as helpful as it could be. This is kind of what it would be like just to edit it in the database, and this is not, this is not that great.

04:16

So let's start exploring some of the other fields that are available in Nova. The easiest place to get started here would be to change this image to an actual image. We'll change the description to a markdown field, and we'll make the price a currency field, which will add a little bit of formatting. So if we hop back to the browser, we see the image is now being displayed. The price does have formatting, but it seems too high.

04:39

It seems very wrong. So if we click in here, we'll see here's the markdown. Nova has dropped in this rich text editor that is markdown compatible, so we can just click there and bold the word Laracon. And not only has it added that, it's added the ability to remove the image or upload a new one so that we don't have to handle any of that file uploading code.

05:01

The question still remains, though, why is this sixty-five thousand instead of six hundred and fifty? That's 'cause we're storing the value in the database as cents instead of dollars, which is a very common thing to do. And so we can alert Nova to that fact by saying asMinorUnits. And if we look now, it'll be six hundred and fifty instead of sixty-five thousand.

05:24

And so this is a really nice illustration. This is one method on one field, but there are methods on every single field that make your life easy like this. So if you wanted to say that, uh, only certain types of files are allowed in this field, and actually, let's store it on a specific disk, and we'll have a placeholder of a certain string.

05:46

Every single field has helpers like this that allow you to tweak behavior without having to eject and do all of this stuff yourself. There are dozens and dozens of these helpers across these many, many fields. I wanna show you one more. I wanna change this status to a badge so that it stands out a little bit better in the UI.

06:07

And with a badge, what you can do is you can map the value of the field. In our case, we're using an enum. You can map the value to one of these standard badge types. So we'll say that if it's a draft, it's a warning, active is success, canceled is danger, and completed is just info. So if we save this and go back to the browser, now that looks a lot prettier.

06:29

We're seeing... We're starting to see a little bit of visual representation of the status here, but it's still not good enough. Let's go ahead and use one of those helpers and drop down here and say With Icons. So now we'll see a badge with icons, and that is what I'm looking for. There's one more thing I wanna fix here. Right now, we can search by ID, but we cannot search by name.

06:52

And fortunately, that's a pretty easy fix. If we come back to the event resource and look at the search array, all we need to do is add the name attribute. If we pop back over here, now we can type Laracon, and it will search on the name field. I'm feeling pretty good about how this listing page looks, but I wanna focus a little bit on how this detail page looks.

07:13

Let's add some stats up at the top to show how many tickets have been sold and the total revenue so far. Nova has several different ways to create metrics. We're just gonna do a simple value metric, and we'll say that the first one is tickets sold, and the second one is total revenue. To calculate tickets sold, we're gonna go through the order model. So we'll set up a query here that's just a base order model, and then we'll restrict it to the event that the user is looking at by pulling the resource ID off of the request, and then we're also gonna scope it down by

07:43

status to say order status is completed. Now we have a good query. Instead of doing a count, we're actually gonna do a sum with this query, and the field that we're gonna pull off of that model is the quantity. So this will tell us how many tickets have been sold for this particular event. The total revenue is actually gonna be really similar.

08:06

So we'll come over here and just paste that in to start. And instead of the number of tickets sold, we want the total. But remember, this was also stored as cents in the database, and so we're gonna just transform that and divide it by one hundred. And I wanna see it with a dollar sign at the front, so we'll add that. And then finally, we'll just format it as zero comma zero dot zero zero, and this will be a localized currency view for me.

08:32

Now the only thing we have to do is come over to the Nova event and go down to the cards section. Now we just need to add those cards. So we can say tickets sold make and total revenue make. But for both of these, we only wanna show it on the detail view. So we'll say only on detail, and we'll set them both to be width of, let's just say, one half each.

08:56

Now, with any luck, we should be able to refresh, and there you go. You see our stats at the top, nice and easy. And Nova has added these dropdowns for us, which of course are customizable, but that's pretty good for now. The thing I wanna see now is we've sold these ten tickets, but we don't have any orders or tickets or anything on this page. So let's get that data on this page as well. You can actually add related models directly to this field's array.

09:20

So we'll hop down here to the very bottom, and we'll say that an event has many orders. And that should be all we need to do to see all of the orders for this event. You can see the listing down here. You can search through it. You can click over to the detail page, or you could click here and run any actions. But we don't currently have any actions.

09:41

So we're gonna fix that by creating a single action. Now, this is the last thing that we'll do together, and we've barely scratched the surface of Nova because this is a speed run. But I think actions are really important because they wrap up and standardize your business processes. What used to be a Google Doc checklist that gets forgotten pretty often, you can wrap up into a bit of code and expose that in the UI so that you can run that from the front end, and your process will always be run correctly instead of relying

10:11

on a Google Doc. So the first thing that we're gonna do here is run PHP Artisan Nova action, and we'll just call it refund order. Now, if we open up this actions folder, we see a new refund order class, and it's given you a handle method here, and it's going to pass you all of the models that the user is trying to run this action on. So what we can do is we can just enumerate over the models, and we know that we're gonna add this to the order model.

10:37

So we can go ahead and hint that and say order. And now you get to decide what is the process, what is the business process that needs to happen when you refund an order? It's probably gonna be something like you wanna send them an email to let them know their order has been refunded. You wanna process a refund on Stripe or your payment processor.

10:58

You want to void all of the tickets that were on this order, and then you probably wanna update the order status. This one we can go ahead and do. We can just say update status and set the order status to refunded. This is what the standardization of your business process could look like.

11:19

You would need to fill out all of these stubs here, and you could extract this entire thing into its own standalone class. That's totally fine. But now your business process is standardized. So if we hop over to the order resource, we need to add this action. So we'll go down to the actions, and just like we did with the cards, we're just gonna say refund order make, and now that will be there on the order resource.

11:44

So the final proof is if we go back to the browser, we come down here to this order, we click on it, we see an action, and we can refund the order. Are you sure you want to run this action? Run it, and there you go. The order has been refunded. We did it. We took what was once a manual process. We wrapped it up into an action, added it to the front end, and ran it from there.

12:07

If you wanna add another layer, a layer of authorization so that some employees can process refunds and some can't, you can do that. Nova has a lot of those convenience methods built in. But like I said, this is a speed run. This is a demo. I would encourage you to check out the docs for all of those details. But hopefully, this has shown you just how quickly you can get a back-end admin panel added to your existing application.

12:33

You don't have to spend an entire development cycle on it. You can add this to your application. You can get off the command line. You can get out of your database editor, and you can start standardizing your business processes today. I hope you will give Nova a try and let us know what you think.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues