Laravel Nova Resources (Lesson 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
laravel Nova resources now these things they're the backbone of laravel Nova and they are extremely powerful they get is insane so by default we get this user resource and so the higher level view what it actually does once you finalize setting up a resource is it does four things and they're based on crud loosely based on crud so it uses a model you don't have to add a controller and you can easily define a form to create an edit a user or create an update you can easily go to the detailer shell page to read about the user or see information about the user again update is the form and then it is very easy to delete so making a resource here's the index page so if we had multiple users there would be multiple rows the index page for a resource the resource in this case main users displays all of our users now if you go to this eye right here you can view the user and when you view the user what it does is it just shows the information for a specific user and not for all the users and apologies guys my machines running slow for some reason this takes too long yeah there we go this takes too long I won't make you guys watch those reloads hopefully it just speeds up so after that you know you have the details you have the index but there's also an edit button and a delete button are you sure you want to delete the selected resources nah I'm good and it says resources fees on the index page you can select multiple rows and delete multiple so that's why it says resources plural instead of resource singular even though we are on the details page and then of course if we want to edit it would say hey name email password and then if we just cancel will bring us back to our resource index so once this reloads will go oh I guess the process back to the detail once this reloads we're gonna actually create a user and this is how easy it is to create a user in Nova it is crazy and again the user is the default resource as an example out of the gate we have a user so say we want to create I don't know Jonnie Jonnie bill Bob and then you can do Johnny at build-a-bra or it's an organization and then you could define this password I'm just going to make it secret and so create a user or create and add another so no and then of course it still has the validation so we'll call it password make that eight instead of secret it is now password and then of course after we created our user we'll get this head the user was created successfully or be redirected back to our index page and then when we're redirected back to our index page we'll see a list of all of our users and gosh I keep thinking index page it redirects you to the details page after you do an individual user action like update or create and so now we have Johnny Bill Bob we have Johnny you know all the information for this user all the information for that user is shows both of them we want to look up it's just a specific user then it will and of course if we were to go Zack which is my user then we're good it also allows you to index paginate etc etc if we wanted to select all users and then we wanted to run the delete action then we could and we'll say are you sure you want to delete the selected resources I don't want to leave my user so I'm just going to delete this user but before we delete Johnny Bill Bob I just want to show you the detail page one more time it shows the specific details for this user and if we go back if we were to edit we could edit that specific user and so it sets up all of our crud pages and so that is Larry Vanover resources on a higher level I want to lead them real quick and so how do we actually go in and make our own new resource here's the home page and if I go to slash Nova we get redirected to the dashboard right I want to add a new resource because I have a list of packages and each one of these packages there's like 40 of them 40 of the 43 or something are actually packages I created so many are MPM and so if we go to NP MJS comm / package UJS form just to show you what why i need to create this resource real world example I have all of this documentation right all of this documentation it's a long package then acts like layer Bell has layer of validation and all that stuff prequel package the problem is the unpacked size of this package is really large for a JavaScript package and what I want to do is I want to be able to say hey instead of putting all this documentation inside the package itself I want to go to the github page and I want to point the user to say like hey to see the documentation go to clean code studio I want to host the documentation on the clean code studio website and I want to do that to minimize the size so we're gonna create a model we're gonna do PHP artisan make model and then we're gonna say package and then we're gonna say with a migration - yet make model with the migration you need a model on a migration alright so we created the packages table and we are creating our model so right here we're gonna have a protected fillable and within fillable you want to add a title a link and let's see let's do a body and I probably will go in later and actually add more details to this model so I can actually break out my packages and reuse documentation between the packages there's a lot of them and I don't have to retype the same thing but for now that will be good so after that we want to go to our database and if I can find my database there it is and we want to go migrations and we created a packages table right and so within this packages table I'm just going to go back to my packages copy my fillable and simply go right here and we're just going to paste those in now what I'm going to do is I'm going to go and do this I'm going to say table and then we're going to go and let's do just string right there remove the comma and put that it looks like I messed that up down there and then I'm gonna change the body and I'm actually going to make this text or even long text so after that we're going to want to migrate our database PHP artisan migrate once that's migrated we will have our my creation set up we'll have our model set up we do not need to make a controller once we have our model in our migration setup the last thing we need to do is do PHP artisan Nova resource package Nova resource package it doesn't need to be package resource doesn't need to be anything special it's like making a model and so then it will automatically point to the back slash app back slash package class the title is going to be ID and of course if we were to go back to our package real quick we called it title so I'm going to call this title this will just be the text the property that displays when you're searching for it if you're searching for it and you had ID it will show just the ID of the package that can also be a method though but we'll go into that different tutorial we want to be able to switch to search through our packages by ID and title and what else do we have we had ID title wink body and I guess why not we could go body to that way we can actually look for keywords on our body which is so cool after that we are going to want to make our actual form so before we go any further let's just see what we've got if we go back to our notebook page we're gonna have package how cool is that so we can go to the packages page and it's gonna have our table set up god this is insane we're gonna be able to set pagination to search to create package if there are no packages it just gives us this easy create package but notice notice our form is empty so how do we fix this how do we add fields you know how do we add the create field in the update field and sometimes you need to show things on create you don't need to show an update and vice versa well believe it or not it's just the fields method so check this out if we go to the fields method we're returning an array and it's an array of fields the first field right here is ID so let's scroll up and look at the namespace laravel Nova fields ID so if we click through two fields look at all the fields we have we have Gravatar morph to action has many plays ID a heading field element action field password confirmation resolved reverse relate what huh I've never seen that ok resolves reverse relation uh yeah that sounds fun also cool but we have all of these things and so remember we're trying to make the documentation for a package well something tells me I'm going to need this markdown field and so I'm just going to copy this right here and of course I'm just gonna do this slash marked out and then we're going to have to have a title so I bet you know I might have done this once or twice that there is a text field so there's that feel so we have text and we have markdown and let's just go text again for the link or URL because Nova does not have an actual link field there's plenty of packages out there for it real quick by the way cards filters lenses actions ignore those for now we will get into those in future lessons we'll also learn how to create our own fields how to create our own custom fields and a lot more but right now it's just fields search and title and so we added the markdown field and what we want to do is we want to add the title case version of our model property just remember our model property and I'm going to close this out and that out and let's just get to this our model property is title link body whenever we're making a field we want to add the title case version of our property so for the body the actual content of the documentation instead of body like that or if we're two words like content body that's how we put it in our fillable property in our migration well in our Nova field we want to just do the title case version markdown make body and if we wanted to say documentation and then a reference the body directly we can but by default if you put the title case version it will look for the snake case of that form so body will automatically look for body if we want to give it a custom name then we could do documentation body otherwise we can just do body and omit second parameter after that we want to say okay the markdown that's going to be a lot of content to show on the table so let's just do only on forms and now it's only going to show on forms and then we'll say required and yeah we'll call that good then we're going to want to do text make title and then we're going to say or required and that's going to be everywhere so we don't need a need to do anything with that after that we're gonna do text make and then link and that is also going to be required and maybe we want to specify like github link and so then we will specify the second option so right there and so what do we just do that by adding these three lines plus the imports we created they create and the Edit forms and now we have our entire markup so let's try this let's go to my github account we're going to go right here I'm gonna edit my markdown just like that copy all of this markdown and we're gonna do this I just paste a lot of content there right a lot of content notice how its formatting is got the colors all that good stuff alright so there are a few things like this image I actually have to use markdown whereas github allows me to specifically add that so it's not get help marked down it is very strict to mark down after that we're going to say okay what is the yep link that's going to be right here and the github link is just going to be this and then title is going to be view Jas form and after that thank God for a fast growing mouse great package Hey yeah oh string dad a right truck whoa so what we can do here is we can go to inspect and then the network here's a really good tip when you have some weird bugs and know of a markdown or in any nova field is check your network and we're going to say okay when you check your network it will show you the air string data right truncated data too long for column changes at row one action event oh it's too long wow that's crazy I use long text there too so for the sake of this tutorial I will use this different package [Music] but all the images are messed up and I used a lot of images in this one yeah but overall I can fix that later create package and package created okay so now we have this the package details and title you have the link only show on forms so I made a mistake and that mistake was this we never actually see the documentation even though we should I said only on forms and what it really should have been a hide from index that's what I wanted to do I just want to hide it from the index I want it to be on the detail page and not on the index page so if we reload that then we get show content and showing slash hiding fields this will be show on index show on detail show on creating and updating this is the forms this is the single resource page and this is all resources page indexed to the table detail a single creating and updating are the forms and you can hide only and then except on forms so that's how you can hide and show things and Nova and so the last thing that we really have is if we go to the package index page and we search for the package I want to show you what that a search property did so if we go like for maybe up here let's do this so this is the global search bar this is the resource specific search and so if we do four and we do a global search that will do laravel mix code splitter and it will give us that packages right but how does it know to use Leoville makes code splitter that's the title that's just a random property right would you remember when we were doing search and first off we're able to search by these properties and then secondly we set this public static title well the title is the title but what we're really referencing is the package property of title if we wanted to call it link instead or even ID we get the link and me as we change that in PHP we didn't actually have to reload our page alternatively we can actually override the public static title where we're only allowed to reference properties of our model our Nova resource is referencing and we can set a custom title that will show in the search it's gonna be a public function title and then we can do this title and then I'm gonna return this title and let's do this let's do this title and this link and I like doing those little curly so I think it looks nice and we can do that completely remove that altogether update this DAC block to get rid of those ugly lines and now if we look up laravel mix we get our custom title so guys that is setting up a resource within layer villanova from start to finish in a real world example of course we can delete this we can view it we can edit it and we can search it [Music]
Info
Channel: Zak H.
Views: 2,358
Rating: undefined out of 5
Keywords: Laravel Nova, Laravel, Laravel Nova Ressources, What is Laravel Nova, Laravel Nova Overview, Laravel Nova Examples, Laravel Models, Laravel Migrations, Laravel Nova Search, Laravel Nova Fields, Nova Resources, Php, Laravel Framework, Laravel Packages, Laravel Ecosystem, Laravel Nova 2020
Id: -ZNMy0ZS3O8
Channel Id: undefined
Length: 22min 1sec (1321 seconds)
Published: Sun Jun 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.