creating database schema | Build Blog with Laravel, Livewire & Filament #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back on today's episode we're gonna go ahead and create other models and survival migration so let's get started so I have the code from the previous episode I'm going to start off by first creating other models so for our application we're going to need a few models so the first one is going to be our post model which is obviously going to be our blog post or articles next up we are going to have a category model you can also name it a tag if you like but I'm going to go over category and next up we need a migration which is going to be category post this is going to be what we use forever many to many relationship and then later on I'm not going to be adding those in this episode while people have one forever likes and then one for comments as well okay and then potentially one for roles and permissions which is going to be later on through article so let's start off by first creating these three migrations so I'm going to open up the terminal and type in a PHP Artisan Mage model post for our posts and then I'm going to do taxi tag M to create the migration and then tax C to also create a controller for it because we're going to be needing that and then I'm going to do Tac if to create a factory for it so we can create some fake data and if you guys haven't used factories I'll show you guys how they work in this video so let's go ahead and hit enter and this will go ahead and create four files for us after that I'm going to go ahead and do the exact same thing except I'm going to remove the controller because I don't want controllers for our categories and then go ahead and do category hit enter and then for the last part which is going to be our category post because we're going to have a many-to-many relationship between them and posts and categories so that means a single post can have many categories and then a category can belong to many or can have multiple posts so I'm just only going to create the migration so I'm going to say PHP artisan make migration and then inside here we can say create category host table and the reason I'm doing category post this is the convention in larval use the singular version of the model or the model name and then we put the one that's alphabetically first or we order them alphabetically so it's going to be C before P so it's going to be category post listen enter and now we have all of our migrations so let's go ahead and actually start off by defining these guys so on the left I'm going to go on under our migrations folder table migrations and there are obviously a few ones the users all those stuff so it's going to be the posts so let's start off with that and I'm going to actually close the terminal as well so for our post guys first thing we need is we are going to have an author which is going to be the author of the post so you can maybe name this Auto you can use a user so in my case I'm just going to go ahead and create a user first I'm going to say 4N ID user ID or alternatively you can also do for an id4 and just pass in the user model and a lot of them will take care of the rest next up we are going to need a string for the image so which is going to be the image or the thumbnail of the post you can also name it thumbnail if you like and this one is going to be nullable in some cases we may not have an image so what we will do is we will show just the placeholder image if this is null after that we are going to have our title so this one is going to also be a string which is just like this and then after that we are also going to have a slug so the slug is going to be what is used in the URL right so the title may have a lot of spaces on it might have some weird characters the slog is going to be kind of the shorthand version of that with no spaces so it's going to be a slog and then also for the slug it has to be unique otherwise we will have some issues so I'm going to go ahead and add this unique here so it's going to be unique value and then after that guys the last part is going to be the body of the article right so in this case I'm going to say string and you can name it content I'm going to name mine body I think that's going to be the body of the blog post and well not a string should be a text so what the strings usually have I think 255 characters are implorable it's a bit longer sometimes depending on your database there's also medium text and a long text so I think medium text can be useful sometimes a long text is just very big I think it's like a few megabytes 16 megabytes something like that I personally have never had to use it so I'm just going to go ahead and use text if you want your application maybe your blog posts are too big and text is not enough maybe you can then go ahead and switch it up to medium text but I think for now text should be more than enough for us so now that we have these are going to be actually all the core things we need for the blog post we're going to have some things about publishing it and unpublishing it so I'm going to go ahead and add a timestamp and this timestamp is going to be used to manage or control whether or not a blog post is published it's also going to be used to kind of schedule a post to be published in the future so I'm going to name it published at and I'm also going to make it nullable so if it is null that means the blog post isn't published right very straightforward and last but not least guys we're gonna have a Boolean this one is going to be named featured so if a blog post is featured it's going to be shown on our landing page it's going to be like the top three posts uh you know it's like a recommended post right so that's what it is it's going to be used for and I'm going to give it a default of false so by default all the blog posts are not published last but not least guys this is going to be the only thing we need this one is optional you don't have to do it but I'm also going to go ahead and give it a soft deletes okay and it should be table soft deletes and I forgot the L so I think this is just nice to have if you don't like softly so you can just ignore the step okay it's not mandatory I think it's just a cool future to have so that's gonna be our post table guys let's go ahead and also set up our categories table now categories is going to be relatively simple so I'm actually going to go ahead and copy title and image sorry title and slug from our posts over to categories and that's actually all we need in terms of information maybe you can add a description but alternative I'm also going to add a color section because I want each category to have this kind of your own unique color if it's like a PHP category be blue if it's a lot of will be red so what I'm going to do is I'm going to go ahead and add two strings to it one is going to be a text color and then it's also going to be a nullable so in case we don't have a color it's going to be default colors and then I'm also going to have a BG color okay and these are going to be maybe tailbone CSS classes you can also use primary or secondary color if you like I'm going to be go with text and BG because it's quite obvious what they mean and that's going to be our categories super simple table and last but not least for our category post we are going to have two foreign IDs for our category and our posts so I'm going to say a foreign ID F4 post class and then the exact same thing for category as well just like this and I believe you are done guys let's go ahead and try to migrate this and hopefully everything works fine let's do a PHP arts and migrate and now I believe we have successfully migrated everything the last step guys in order because it's going to be hard to create fake data for our article it's going to take quite a bit of time we're going to use a future in a larval called factories and factors are basically as the name suggests it's like a factory it creates data for our application okay so we have created two factories which was the post Factory and the category Factory and if you do the dash I it will automatically go ahead and update your model so you also need to have this use Factory trade on your models so if you manually created the factory make sure you go on your model and add this use Factory trade but if you use the command Dash I will automatically do it for you so in this Factor we can Define some criteria and a lot of will go ahead and create fake data for us so we can create fake blog posts and fake categories which makes it easier to test our application on local development so let me show you guys how this works in practice okay so inside this post Factory what you can you can actually look at the user Factory to get so many kind of an idea of what it does you're going to Define all the columns and then tell it what type of data it should be okay so let's go ahead with our post Factory we are going to have one for user ID so it's going to be the author of the post and one thing we can do is we can actually get the user Factory and just say user or personally to import the user model and then do a factory and what this will do is we'll create a new user and then assign the ID to over here right so it's going to create a bunch of fake users for us as well next up we're going to do our title and then here we can say this dot faker so this will access the faker object and then here we can say for the title maybe something like a sentence okay so this Faker object has a bunch of methods on it it will generate fake sentences for us and if you guys hover over it on vs code or a PHP store you can actually Define the number of words and I'm going to leave it as the default next up we are going to have this log for this log I'm going to also use the exact same thing we can say this dot Faker now there is a slug method over here and by default it's like six words I'm gonna make it like be three boards okay so I don't want it to be too big and next up we are going to have our image now Faker actually can generate fake images for us I'm going to say this dot Faker dot image URL so this is going to create a fake image URL one more thing we have actually let me open up our migration on the side so we can see it because I already forgot all the columns we have so let me open it up our migration and then I'm going to open up the posts so we have ever post migration on the right and then we have ever Faker on the left the next thing we need guys is going to be at the body so I'm going to go ahead and say body now for the body we can use a paragraph So this is going to be this dot Faker dot a paragraph now as far as I know there is no way to generate HTML paragraphs in Faker as far as I know I have never seen it before so and inside you can pass in the number of sentences so I'm going to say 10 10 sentences per article so the remaining parts are going to be published at and featured so for published ad we can go ahead and say published at and we can use Faker for this as well so we can say this dot faker and I believe there's something called time between your date time between so what this will do is this will generate a fake date a daytime object between a time frame you pass in so if you like that vs code this actually has minus 30 years and now so at any time between the last 30 years in our case I'm gonna do go ahead and say a minus one reach and then plus one week okay so we can have some articles that are already published over the last week and then some scheduled for the future last part mini missing guy is going to be featured so this is going to be our future post I'm gonna say uh future and then for this one we can use this dot Faker dot Boolean okay this will give us a random Boolean zero one and uh if by default it's going to be 50 50 so let's say if you generate two posts one is going to be zero one is going to be one so we can actually change the orders now future posts are supposed to be very small for example if you have 100 articles maybe five of them are featured so I'm going to set this to ten percent and I believe now we are done guys so last thing remaining is going to be our category uh Factory I'm going to copy title and slug from our post and I'm going to add that to our category Factory okay we don't need to worry about the colors I'm just going to use default colors for all the posts and I believe you are done guys so let's go ahead and test this out so one thing we can do in order to create this fake data is if you go under your database folder look on their theaters so the cedar is basically used to as the name suggests seed or create fake data so it uses your factories to create data for your application it doesn't have to use factors can also manually create them yourself so in our case it actually contains some existing code which is commented out we can actually use the first one and instead of a user we can go ahead and change this to a post so basically you get the model you call the factory method on it and you tell it how many items you want to create and then you call this create method right so just go ahead and create 10 fake posts for us so let's test it out we can go ahead actually let's make it 100 and then I'm also going to go ahead and create five fake categories so this is going to be the main database leader so in order to run this you need to go ahead open up your terminal and run PHP or send DB feed and this will go ahead and basically generate fake data for us now we don't have any way of seeing if it works if you guys have phpmyadmin or table plus you can go ahead and check it out that way if you don't there is another way you can use something called PHP Artisan thinker so you can do PHP or send Tinker is a command line tool to play around with your application so what we can do is we can actually go ahead and copy this a git our model and do count and now we get 100 right I can do the exact same thing for category as well and we get five let's do for users as well we can do user account we should have 100 users because for every category every post we are creating a new user and we actually have 201 we have more than that interesting okay cool I'm not sure actually why we have 200. so the last thing guys uh if you want to exit Tinker all you have to do is do Ctrl C and that's it you're good to go so let's exit this and that's it gonna that's gonna be it for guys for today's episode guys if you have any questions you can let me know in the comment section Below on the next episode we are going to build our home page and display all the blog posts on it so if you guys have any questions leave them in the comment section below as always make sure you smash that like button and subscribe to the channel if you're new so you get notified of the latest videos and as always I see you guys on the next episode have a great day bye
Info
Channel: Yelo Code
Views: 6,353
Rating: undefined out of 5
Keywords: Laravel, Laravel tutorial, Laravel tutorial beginners, Laravel livewire, laravel livewire tutorial, php, laravel livewire 3, livewire 3 tutorial, livewire 3 in 5 minutes, laravel crash course, livewire 3 actions, livewire 3 form, livewire 3 laravel, livewire 3, livewire datatable, laravel modal, laravel blog, laravel project, laravel blog project, laravel livewire blog, livewire blog, laravel livewire crash course, yelocode, laravel cms project, laravel cms blog, laravel 10
Id: 1uaXD6iJnWw
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Fri Sep 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.