Complete Laravel Tutorial | Laravel From Scratch | Full Laravel Course | Laravel For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to my laravel from scratch course where i will teach you all the fundamental things that you need to know to become a good laravel developer besides that i will also create a couple projects such as a complete block e-commerce and things like crm systems first of all my name is dari i'm 25 years old and i'm currently living in the netherlands i'm a back-end developer that works mostly with frameworks such as laravel and symphony some of you guys might know me from my youtube channel where i mainly create videos for web developers it is also the place where i'm most active i try to upload three to four videos per week and to answer all of your questions besides that you can obviously find me on social media and mostly on instagram i must admit that i'm not very active but i will definitely plan to do that in the near future i try not to be a programmer that shows something on the screen and to be inactive the rest of the time usually i try to help people that send me messages or emails about issues they run into so please don't think that i won't respond but just give it a try if you run into an issue and we can work on it together this course is recorded at the end of 2020 so don't think that this is a laravel 6 or 7 course where i just changed the name of my course all techniques used in this course are based on larval 8. i will definitely show you what the differences is between larval 6 7 and 8 but my main focus is mostly laravel 8. the way i like to work is to basically explain difficult subjects as easy as possible and as fast as possible usually people try to make a certain topic difficult but that's the opposite of me my main goal is to basically teach you something in quote unquote simple terms and besides that i will add source code on every single video so if you run into an issue just have a look at it and see what the differences are now laravel is a little bit more advanced than plain php so it is a good idea to start learning some other things first if you are new to coding think about the basic concepts of plain php object oriented php mostly terms like inheritance interfaces and polymorphism and the clear idea of what the mpc structure actually is laravel runs in a blade template but it's mostly based on html5 and css3 you can see laravel is an open source php framework it uses the model view controller design pattern which makes it very easy to reuse existing components of different frameworks usually the first question i get is well why should i ever use laravel if i'm good in php and to be honest i'll get that because php is a programming language and laravel is a framework that is built in php and i hope that that makes sense because well there is no laravel without php and to give you another example if we look at the motorcycle php is like the bare bones of the motorcycle and you can see laravel as the engine the exhaust frame wheels of the motorcycle so yes php is way more powerful but laravel makes it possible to create php-based projects easier and i mean a lot easier since it does most of the work for us so what are the advantages of using the laravel framework well first of all authentication authentication in laurel is very simple if you have ever created a login system in plain php you must know that it takes a while before you do all the checks and so on well larval provides a simple way to organize authorization logic and control access resources also the mail services in plain php it's terrible to use laravel provides as clean simple api over popular drivers it can make web applications way faster because in php you constantly need to remove your cache well you don't need to do that in laravel and the last and in my opinion the best point is the security laravel helps to secure the web application by protecting it against the most serious security risks think about things like sequel injection cross sight cross side request forgery for side scripting and i can name a lot more advantages but these are just a couple everything has disadvantages and so does laravel it is a new framework that makes use of composer which is not as strong as npm it has less inbuilt support compared to django and ruby on rails and some updates that come out might be problematic there are a couple very good php frameworks like symphony code igniter cake php and zen but in my opinion laravel has retained the top position at the list because of the security that it provides the mvc architecture migration of databases and the command line interface i want to quickly show you what you could build in laravel these are not the projects that we will be creating in this course but i get a lot of questions of people asking what they can create with laravel keep in mind that these things usually require more than laravel only most of them have stuff like vue or javascript for the front end obviously we could create a basic website which will ensure you to become familiar with javascript you can create a to-do list which will make you understand the core concepts of laravel you can create a login system but in laravel you can build a login system in pretty much five minutes but it is a good practice to try it out you can create a blog where you can learn how to create a crud application you can create a bookmark manager timesheet let's say a restaurant audio app a personal portfolio a content management system like wordpress or think about shopware which is built in symfony you can create a social network like twitter or instagram and you can create an e-commerce if we hop to the official website of laravel called laravel.com you can find a button on the homepage which says documentation so let's click on it whether you like reading or not or you're just like me that enjoys watching videos instead of reading long pieces of text i still think that the larval documentation is very nice i've saw a lot of comments on the internet of people saying that they do not like the larval documentation and i totally respect that since everyone has his own opinion on it i think it is necessary for me to show you how it actually works before we continue on since it's such a huge part of laravel itself now on the left hand side of the screen you can see the topics that we are going to cover in this course whenever you click on one it will open a new screen that will show you content based on that specific topic what the larval documentation does is basically the steps that we need to follow in order to accomplish something and even inside the section you can navigate so right here right now we're in the installation part where we could also go to configuration or the pretty urls if we scroll down you can see section one installation and it has the server requirements that we need and if we scroll down you can see how we could actually install laravel through composer now let's click on database just to see what happens you can see a drop down menu with a couple other options so let's click on migrations to see what will appear the database migrations are basically the version controls for your database let's say that you don't want to run your laravel on version 8. well in the top right corner you can basically change it to whatever version you want the lowest is 4.2 and the highest is 8. the x stands for well everything after the 6th point so it could be 6.1 6.2 6.3 there's also a search bar that we could use so let's write down many too many which is an eloquent relationship the first option is the one that we need so let's click on it and you can see that we have been redirected to the part of eloquent relationships hashtag many too many to get a good understanding of where the many-to-many is they have a piece of text which you can read so our many-to-many relationships are slightly more complicated and has one and has many relationships well we can see the table structure the model structure and how we could use it what i'm trying to show you is that the search bar is very accurate whenever you run into an issue just come over to the laravel documentation and try to search for your issue what i've done before this video was basically removing every necessary application that i had and what i would like to do is to reinstall everything together so we're all on the same page in order to work with the laravel framework we need to pass a few requirements so let's start off by doing that the first thing that i need to add is homebrew and homebrew can be seen as a package manager that makes it very easy to install and update applications on a macbook so let's hop to google chrome let's go to google.com and right in the search bar let's search for homebrew let's click on the brew.sh and right here you immediately see how we could install it on a macbook so let's click on the icon on the top right corner of the install homebrew you can see that it turned green what we need to do right now is to go to our terminal there are two ways how we could open our terminal the first way is basically clicking on the icon well in our name bar terminal right here and let's click on it but that's not what i want let me close it as a programmer i want to use my mousepad or mouse as little as possible and i want you to make a habit of it to do the same exact thing what i would like you to do is to hit the command spacebar and you can see that the search bar just appeared on the screen which allows us to search for whatever we want so right now let's say that we want to search for terminal and you can see that our terminal has been opened without even touching our mousepad so let's make it a little bit bigger and my terminal might look different than yours but we will install plugins for our terminal later on now in our terminal let's command v the homebrew installation that we just copied let's enter our password press enter and you can see that the script will be installed this may take a while so pause the video and come back when your installation is done all right then guys my installation has been done and i hope yours is too i won't go in depth about homebrew because that could be a course apart but i basically want to continue on right now with installing laravel on our laptop or pc before we continue on i want to see which php version we are running that can be done by writing down php space double dash version let's hit enter and let me zoom in a little bit and as you can see i'm running on php 7.3.11 if you do not see anything you can basically run brew install php which will automatically install php version 7.4 for you i'll try it to see what the output is so let's hit enter and you can see that it's downloading php and this might take a while so i will cut it out once again pause the video and come back when your download is done my installation has been done so let's clear it and let's do php dash v which also stands for version so this is more like a shortcut of the version so let's do that well mine is still on 7.3 so let's put the terminal let's open the terminal again to see if there are any differences zoom in let's say php dash v and you can see that my php is running on 7.4 right now but you might have noticed that it's not necessary to install php whenever you have an older version running so instead you could basically say brew upgrade php which will do the same exact thing but way faster alright then now that our php version is set up we can continue on by installing our mysql and this can be done in the same exact way as how we just installed our php so let's write down blue install mysql that's hit enter and once again this may also take a while so what i would recommend you to do is to pause the video once again and i will see you back in a second all right then one more thing before we continue on is well we need to tell brew that it needs to start my sequel and that can be done by saying brew surfaces start my sequel this might take a second probably not too long all right successfully started my sequel now in order to see if this works we need to write down mysql space dash u root space dash b let's hit enter let's hit our password let's hit enter and you can see that we're not in the well greater than symbol anymore so in our root directory but we now have entered my mysql and right here we're able to perform all types of sql things so let's say show databases and you can see all the databases that i have right now to exit mysql you basically need to write down exit semicolon let's hit enter and we're gone that's right now clear now that we have installed homebrew php and my sql i want to focus on installing composer and laravel if you are new to any type of php framework composer will not make sense to you yet but believe me it is an awesome feature when you start understanding it and it takes time for you to understand it like i said the next thing that we need is composer because laravel is installed through composer so let's open the browser and inside our google search bar let's just google for composer it's a guestcomposer.org so let's click on it when you get on the website you immediately see a download button so let's click on it and right here you can see the download commands that we need so what we need to do is to copy it and you can copy all four lines and you basically need to paste it right inside your terminal i haven't deleted my composer because well honestly it's a pain in the ass but i will just paste it right here to see what happens and as you can see our composure version 1.10.15 is successfully installed to see if this works we can try to install laravel on our composer by basically saying composer global require laravel forward slash installer and that's hit enter and you can see that changes are being made in our composer directory and this might take a second so pause the video and i'll see you back in a minute all right then what we've basically done is sending a message to composers saying that it needs to pull in everything of the laravel installer and as you can see our dependencies have been pulled in now the next thing that we need to do is to place composer system wide vendor bin directory in our path and this may sound difficult and i did struggle doing this a little bit the first time so just bear with me and give me a minute we need to place our vendor bin directory inside our global pad so inside our terminal let's echo out dollar sign pad and as you could see a weird patch just appeared on our screen now we need to create a pad so basically if your bash rc hidden file already exists you can write down vim till the forward slash dot rc to go inside our bash rc but i have deleted mine so what i need to do is to write down nano space tilde forward slash dot rc let's hit enter as you could see my bash rc does not exist and it just created a new file so what we need to add right here is export path is equal to well double quotes inside our double quotes variable home forward slash dot composer forward slash vendor forward slash bin colon variable path now in order to save something inside the nano we basically need to press ctrl x now you're getting a pop-up message well do you want to save it and let's basically write down y let's hit enter and we have been pulled out of our batch rc to see if this works let's write down source tilde rc again all right and let's write down laravel and you can see that our larval installation has been completed but before we continue on i want to set up a workspace where we store our projects and this can be different for anyone but i will show you my way as you can see in my terminal right now we are in our root directory so you can see the greater than sign you have probably the name of your laptop right there what i would like to do is to go inside our desktop create a folder where we can store our projects well i basically already said what we're going to do but there are two ways how we could create a folder you probably know one well we could go to our desktop create a new folder name it workspace and we're done but i don't want to do that so let's delete it let's go back to our terminal and i'll show you the way i like to work so inside the terminal let's write down clear first and in here let's write down ls which means list computer files so we will basically print out all the files that we have in the directory that we're in right now where i want to go is to my desktop so into this folder so let's write down cd desktop or you can write down cd d hit tab and you can see every word that starts with a d so let's say desk tab and you can see the autofill went straight to desktop let's hit enter as you can see right now we're inside our desktop directory by the way cd stands for change directory so what we've basically done is change the directory to our desktop inside our desktop let's create a new folder using the cli and in order to do that we need to write down mk there space followed by the name of your folder in my case it's workspace let's hit enter let's take a second to guess what mk air stands for well make directory now that we have our workspace let's see the well let's ls first so you can see that our desktop has a workspace inside of it so let's say cd workspace let's go inside our workspace and now that we have laravel installed we could use a new command that will create a fresh laravel application for us let's say laravel i want a new project so the keyword new followed by the name of our project so let's say first project let's hit enter and you can already see the laravel sign and our installation just begun pause the video again because this might take a minute again and come back when your video is done alright then my installation has been done and i hope yours is too and as you can see we're in our desktop workspace so let's say ls again to see what just has been added and as you can see a folder called first project has been added to our workspace so let's cd into our first project and right here let's write ls to see what's inside of it and whoa our first laravel project has successfully been created now before i finish up the video there's a second way how we could create a project and that is true composer itself there isn't any difference but the command is but i think it's good to show you what the other ways are as well so since we're in our first project directory let's write down cd dot dot to go back one directory so we're back in our workspace and what i want to do is to remove the file not by right clicking on the folder name but by writing down air m which stands for removes space dash rf which stands for recursive force space followed by the folder name and in our case it's first so let's write down fur tap first project that's hit enter that's ls and now you can see that our first project does not exist in our workspace anymore and let's double check that to open our workspace and you can see that it's empty alright let's go back to our terminal and what i want to do right now is to create it one more time through composer and to do that we need to write down composer create dash project space dash dash prefer dash dist space laravel forward slash laravel space followed by the project name so everything that we have right now is fixed followed by whatever we want so let's say first project let's hit enter and you can see that we're creating a new laravel project we will take a second so once again pause the video and i'll see you back in a second when you're done all right then guys my installation is done and i hope yours is still so in our workspace let's write down ls let's cd into first project let's hit ls again and you can see that the output is exactly the same most of you guys know that mac runs on linux and when working with a laravel project you need to use your command line at lot even if i rewind and think about what we've done so far in our terminal we basically installed while php mysql composer and homebrew we created a file and a folder inside our terminal but well if you look at our terminal it does not look good and don't look at my beautiful colors that i have right here well i had your terminal the basic white one with a dark background before this video i actually installed some plugins but i just don't know how to get rid of them the problem you probably have is that you cannot see any difference between a confirmed message or a deny message because everything is printed out in white now what we could do is basically style our team fonts and so on so that's something i want to do in this video i actually found a very nice repository on github that we could use so let's go to google well that went wrong let's search for kevin smith's github and the version that we need is the item 2 solarized so what i want to do is to download the complete new terminal which is way more efficient so what we want is well this is the default but what we want is to turn our terminal into well the power level 10k so we want more colors that have a specific meaning we want the branches to be shown we want the time to be shown and everything else and don't worry for now i understand that most of you guys are new or just haven't heard of some of these terms it's all right i've been there and honestly i need to learn so much too during this course and your programming career you will learn it and you will get used to it first off let's scroll down so how to install we need item 2 and this is basically a replacement for terminal and the successor to its first version of item we could go to the website of item so let's google it item item2.com we could click on the download button but now we have homebrew right now so let's copy this command let's hop to our terminal again let's clear it and let's actually get out of our first project let's go to our desktop cd all right let's paste our command in here and you can see that something is happening on the screen and that's her installation while it's downloading and the fun thing is that it's pretty much the same as the terminal that we have but it has a little bit more advanced features such as a split plane view hotkey mouseless copy instant replay paste history and well a couple more all right then my installation has been done let's go back to the browser and well what we need is all my zsh but we need homebrew installed and by now i expect you to have that but i'm just mentioning it because every once in a while i ask students to download something which they will not do and later on they'll come and ask why it doesn't work once again you need homebrew to be installed so what we need to do is install it with chrome so let's copy it let's hop to the terminal paste it right there hit enter and well i have it already installed but what i can do is to actually remove it so let's cd to go back to my users diary let's say rm rf dot o my zsh so it has been deleted so let's cd into the desktop wow that went wrong cd into the desktop again and well let's paste it one more time and it's cloning our zsh well terminal would like to have access let's say okay all right and something new has happened on our screen you can see that our terminal went from the name of our directory to well desktop git and i think this looks way cleaner so let's move on to the next steps what do we need to do now when the installation is done edit the zshrc and set the theme to something different but before we do that let's quickly install power level 10k because the eggnoster default team that you see right here looks more like the first picture over here and i want the power level 10k so let's scroll down again let's copy the git clone of power level 10k let's go to terminal paste it right here hit enter and you can see that the clone is going on right now all right then guys now in order to change our terminal we basically need to copy the zshrc so let's copy this piece of text let's go to our terminal and let's get out of our desktop by writing cd dot in our root directory let's open or well nano what we just copied we just opened a hidden file because the punctuation mark is right in front of the name which stands for hidden what we need is well let's have a look we need the theme to be changed so let's remove it to power level 10k forward slash power level 10k let's hit ctrl x do you want to save it yes so why and the file needs to be the same file name excuse me all right what we did right now was nanoing and doing it inside the terminal you could also say open that specific file and what will happen then is that it will open inside a text editor but i think that this is just way too much work i just rather work with my up and down button and you can basically change it over here all right then let's close off a terminal let's terminate it and let's actually quit it that needs to be done if you want to see your changes but what i want to do right now is to open iturn let's click on it well it's an application downloaded from the internet so let's open it allow notifications and well this is our new terminal so what is it asking install maslow nerd font well let's say yes let's install a new font why not and it's basically asking if we want to restart our terminal so the way we do that is to go to the item to name and let's say quit i term let's say okay let's open it one more time let's say these are all the installation things that we need to do does this look like a diamond rotated square well yes so why does this look like a lock well yes so let's say yes these are icons yes all right then this is pretty important for you to know how do you want your terminal to look well i like the rainbow so let's say tree the character set how would you like to have it i like the unicode so let's set one show current time which version do you like and this is all whatever you like don't do what i'm doing right now because these are my preferences but do whatever you want to do so i live in the netherlands i need a 24 hour format so that's it too so the prompt separator let's say number one i like the angles prompt tails well this is a pretty uh weird one let's say flat prompt highlight one line or two lines well i like the one line prompt spacing well let's actually add spacing icons um well i like many icons instead of you because i don't like to read this may sound very weird but i like to look at icons and know what i need to do so let's say two the prompt flow so i don't want it took five seconds so let's say one and enable transient prompt well let's see yeah let's say yes so why all right then install prompt mode let's say one yes i already have the file so i need to overwrite the current one so let's say y again apply changes let's say yes and it's fetching right now so our installation process has been done as you can see our terminal looks very good so let's say ls and all right then this looks good but before we continue on let's close it off so let's well let's quit it and let's open it one more time just to be sure what the output is this is what we need what you could do is to install fonts and you can install your icons and everything else well we basically have done that right now because we're running power level 10k if you're using 9k you need to manually install these three files let's see what we could do let's open our terminal let's go to iturn let's click on preferences let's click on profile and right here we could basically click on text and we can change our font so let's say that i want the maslow bolt now my font size have just changed to bold somewhat i like so i'll go back to the basic one you need to change your terminal to whatever you like not what i like but what you like the output will always be the same you could also change your colors because sometimes the colors might be harsh you could change your theme color so let's say color preset and let's say smooth well it's not what i like we can say dark which is more black but what i like is solarized arc what we just had there's one more thing that i would like to show you and that's how you could use someone else's color scheme because well honestly it's a pain in the ass to change the colors yourself so what i want to do is to well let's close off our preferences inside a new tab let's search for item 2 material theme and it's a github repository created by martin sealer who was kind enough to design color schemes based on google's material design color palette and on top of that he gives us instruction on how to use it so let's scroll down and yes right here how we could use it it basically says clone the report or download the file well let's just download the file by clicking on it and well this does not look a downloadable file but what we could do is command s to save it and save it somewhere you won't delete it or you won't lose it so what we need to do is to get rid of the txt save it but well that's this is a mac thing because it still adds the txt so what we could do is to open finder let's go to well the location where we saved our material design let's rename it and let's get rid of the txt all right use i term colors all right let's close it we don't need this so let's go back we'll basically need to go to preferences profile color tabs to change it so let's open our itune itune preferences profiles colors on color presets say import and right here we could click on material design colors i term colors open it well i already have it so add duplicates add it anyway and basically we could choose well material design colors and that will then import material design colors to our color scheme i would like to continue on with opening our application in the browser now laravel comes with a pretty cool package called artisan and you basically can see artisan as the command line interface of laravel itself later on you will get a better understanding how artisan is and what you should do or could do with it you could create classes without even writing a single line of code in your code editor i assume that you guys have worked with php before where you basically could go into your localhost and click on the right directory so let's do that so in our google chrome let's write down localhost hit enter and you can see that this site cannot be reached because localhost refused it well it's slightly different with a laravel project since it's not located inside our hd docs think about it what do we need in order to run php well with php we need to run xampp but for laravel we don't need xampp there's no connection between laravel and xampp now you're probably wondering well how do i even start it then besides creating classes and models in artisan there's also an option to open a project in the browser so let's go back to our terminal and right here we need to go inside the directory of the project that we would like to open so let's go to cd desktop and inside our desktop we want to cd into workspace and inside our workspace we want to cd into first project let's hit enter that's ls just to be clear that we're in the right directory all right and in here we basically need to write down php space artisan space and what we want to say to artisan is that we need to serve so basically to start running this project that we're in right now so let's hit enter and as you could see on our screen our laravel development server has started and you can see an http link right here so let's copy it you could also command click on it but then it will open in safari for me so let's hop back to chrome let's replace localhost with the pad that we just had and wow our first laravel project has been opened now if we hop back to our terminal you can see that it's not possible to do anything in the command line interface because it's serving at the moment so if you want to perform other stuff inside the command line you basically need to open a new tab so let's well click on the plus symbol right here now we have our new tab to stop it from serving we basically need to control z all right then you can see that our laravel project has been stopped let's hop back to google chrome refresh it and you can see that it won't stop refreshing because well we stopped serving it you need to be aware that you can only use the php artisan serve inside the directory of a laravel project so let's get out of our first project by saying cd dot dot we're back in our workspace and right here let's write down php artisan serve and you can see that it's not possible to open php artisan let me zoom in a little bit the reason why this is happening is because the surf command is basically a shortcut for the php built-in web server which we'll call the main of our application which then will be runs now another good thing to remember is that by default the http server will listen to port 8000 as you could see right here however there might be a case where your port number 8000 is in use and i can give you one example and this is something that happened to me before let's say that you're working with api connections later on and you have a java project running on port number 8000 next to your java project you want to run your laravel project what you basically need to do is to say php artisan server again but it won't work so we can change that we can give our own port to artisan we basically need to say php artisan surf space dash dash port is equal to whatever number you want so let's say 8081 hit enter well obviously we need to go into the right directory excuse me so let's say cd first project and let's hit the arrow up button still php artisan serve port is 8081. let's hit enter and we're starting our larval project on port number eight zero eight one let's copy it go back to chrome hit it inside the url and you can see that we're running it on 8081. i want to show you how we could change the ip in the url to a piece of text that we would like to do because if we go to our let's say desktop forward slash workspace forward slash first project excuse me cd and we say php artisan serve we will get the ip address of our local host so let's copy it let's go to chrome paste it in and this works fine but what if you want to change it now what we could do let's say control z to get out of our artisan serve um let's go back to our root directory so let's write cd and in here we could basically say sudo nano forward slash etc forward slash hosts and these are the hosts that we have we could say 127.0.0.1 has a name of first project dot test and let's get rid of the localhost that we have so let's do that all right control x save it say yes enter and let's do it one more time to see if it works all right let's copy first project test let's get out of it let's go back to google chrome let's write it inside our url let's hit enter and you can see that it works as well so instead of having the ip address and our port we could basically say firstproject.test you could use any code editor you want when working on the laravel project and i'm not here to tell you that you must work with visual studio code or you must buy a license on phpstorm i'll let you decide what you want because the output will always be the same a code editor is not only something that looks good and you can take some good looking pictures of it fundamentally code editors are developed to help developers code and if you're following this course i assume you already know what the code editor is and you have yours already set up but i want to show you a list of code editors you could use for the best laravel experience let me sum up a couple good points when you are searching for a good code editor try to find a code editor that takes care of the syntax and by that i mean that it gives you an immediate warning if something is wrong auto indentation and auto completion is an awesome future as well try to look for a code editor that speeds up the process you need a code editor that benefits you not me and you won't need it in this course but you definitely do later in your career and that's good so try to find a code editor that is compatible with git alright then i want to show you two free code editors and two paid code editors that i personally love to use but once again a paid version is not necessary so the first one is visual studio code and in my opinion and probably the opinion of ninety percent of the internet visual studio code is the best free code editor it is also available on all major platforms a couple key features that visual studio code has is cross platform it has git built in it has an in editor debugging system it has a large library of extensions and plugins it is very lightweight if you compare it to other code editors now the second free code that i recommend is adam and adam is highly customizable a couple key features are the snippets the code folding the ui is incredible highly extendable and you can open files very quick now when i work with laravel i usually use phpstorm the developer of phpstorm is jetbrains and they have lots of good code editors it is purely based for php developers and this is the code editor i use most of the time it is a full php ide so you can get debugging version control testing error checking intuitive code navigation and way more now another paid code editor that i like is sublime text and if you want a code editor that is easy to use for pretty much every programming language sublime text is your go to in the early stages of my programming career i use it a lot the ui is the best thing about it because of features like distraction free writing mode quick shortcuts split editing and much more that is added and like i said it's not a free code editor so it has a price of 80 dollars per year what i would like to do next is to install visual studio code and phpstorm for laravel developers now like i said visual studio code is incredible to use whenever you're on a mac linux or windows even though i use phpstorm a lot as well i think you can get your visio studio code quite like php sworn when adding the right extensions so let's hop to google.com and let's search for visual studio code and we need the first link which will redirect us to the visual studio code website so let's click on it all right then the download process is pretty straightforward i think the download button will change based on the operating system you are using but if it isn't click on the drop down menu and click on the operating system that you are using so i'm using mac so let me click on download for mac and alright then pause the video and i will see you back when the download process is done now that our download process is done let's open and see what happens alright in my downloads folder you can see a visual studio code application so let's double click on it well visual studio code is an app downloaded from the internet let's open it anyways and our code editor just opened if we try to use our larvae project in here from the bat it will work but it's awful to use so therefore we need to add a couple extensions which will make it a lot easier for us but before i do that let's make it bigger let's close off the release notes and the welcome screen as well i want to quickly show you how you could adjust your visual studio code to your preferences now if we go to code click on it let's open preferences and let's go to settings right here you will find a list of all the settings that you can change and if you run on windows you basically need to go to file preferences and then settings if i could give you advice try not to adjust a lot of things but mostly the basic things so think about the font size the font family tap size easy stuff rather than auto save delay or well let's say the multi-cursor modifier of course whenever you open your code you can zoom in but you could also change your font size so right now -112 let's change it to 16. i'm alright with the default settings for now pause the video take your time to adjust some settings right here and i'll see you back in a second the next thing that i would like to show you is how we could change the color scheme if your operating system is a windows go to file preferences color theme and on the mac let's go to code once again click on preferences and let's go to color theme there's also a shortcut on a mac so let's close it that's it command kt and all right right now you can see all the color themes that we could use so once again pause the video test a couple out and see which one you prefer and don't be worried the output will always be the same this is just a color scheme that you like what i like is just the default dark but more less says monokai for once and you can see that the color changes now i want to go back to my visual studio default one which is the color theme that i prefer now to open our extensions let's go to code again preferences and let's click on extensions and oh yeah it's also in the sidebar right here so let's click on it our extensions are gone and if we click on it one more time you'll basically see the marketplace right here a cool thing about visual studio code and honestly most code editors is well you can add languages debuggers and a lot of other tools to speed up your development process now what i would like to do is to search for an extension called laravel artisan it's the first option it's created by ryan neddy what laravel artisan allows us to do is to perform commands in the terminal of visual studio code what we've done so far was opening our own personal terminal i'll show you later on how we could use the terminal or visual studio code itself now let's click on the install button and our installation process is done it's that quickly so let's continue on with the next extension since we've got a lot of extensions to add the second one is called laravel blade snippets and it's the first option again it's created by winnie lin and laravel works with html well let's actually click on install and i'll tell you what it is but it comes with a powerful templating engine called blade and by installing this extension we basically have a syntax highlighter which is awesome as you can see in the screenshot on my screen now the next extension that i need is called laravel blade spacer it's the first one created by austin cameron so let's click on install you can also see the amount of downloads well this one has 100 000 which is quite a lot when working with php in html you always have to wrap your php code inside the opening and closing php tag it's a little bit different because of the blade templating now in blade as you can see in the screenshot we wrap laravel or php stuff inside a double set of curly braces as you could see right here now you obviously want a little bit more spacing around it and that's what this extension basically does it adds a space to the left and to the right of the text where you want to type in as you can see right here all right the next extension that i would like to download is called laravel go to view let me see if i can zoom in alright this is way better now it's the first one created by coding jew let's click on the install button and what this extension basically does is whenever you hover over the file name that you add so let's say you're returning a view from your controller or inside your form you can basically hover over it and go straight to the file instead of searching for it in the sidebar as you could see in the screenshot you can see a controller right here called food.bar it hovers over it and it goes straight to the view this is also handy when you have an error and you want to double check if your pet is correct you could always hover over it and see if it's correct or not now let's move on to our next extension because we got a lot more to install next one is called laravel snippets all right let's click on install and it's by the same developer called winnie lynn and what this does is whenever you're typing a word visual studio code recognizes a prefix and you can easily get an option to fill in the rest of the text so you don't need to write out every single word as you can see in the screenshot now let's continue on because we need laravel extra intellisense it's created by amir it has 167 000 downloads let's click on install you can basically see this extension as an extra laravel snippet this basically auto completes route names params views variableconfig.emv file middlewares and a lot more you could also scroll down by the way and see what it actually does if you want more information about a specific extension now the next extension is a pretty important one called let's say life says compiler larval mix is a wrapper built to work efficiently with webpack and webpack is an open source javascript module builder what we need is something to compile our cess or sdss files to css and that's what this does it's the first one created by redwood dev let's click on install and you probably don't understand right now why we need a live sas compiler when we're working with laravel but later on where we're designing our website you will definitely understand why we do this and since we're on the topic of styling we could also search for let's say beautify css and i want the second option because i want a beautifier for scss says css and less so let's click on it it has more than 700 thousand downloads let's click on install and i think the name speaks for itself you can beautify your styling since we've actually added our laravel intellisense let's go on and install our php intellisense as well so let's search for php intellisense it's created by felix becker and it has 4.5 million downloads which is quite a lot and let's install it i want to explain to you what it is because it's actually the same as our laravel extension but this one is purely for php now the next one that i need is called getter slash setter generator it's the third one created by augustine martinez ibarra let's click on install and i don't think that this is a must-have extension but it's pretty handy to have if you have a set of properties and you want to generate getters and setters of them you easily can you can do that easily instead of while copy pasting it every single time as you can see in the screenshot you'll select the properties that you have and you can generate getters and setters of them the next few extensions are not necessary but they will help you develop better laravel code the first one is called bracket pair colorizer it's created by coonrot s it has 3.8 million downloads let's click on install even though it's not necessary i think we can conclude that this is a pretty darn good extension now you've probably worked with code where you have lots of if statements loops near each other or even inside each other what this extension does is changing the curly braces color of every single set so you can detect easily if you're missing one or not as you can see right here the if statement has a blue curly brace but the entire function has a purple one so you can easily detect which curly braces pair now the next extension is called emit life and it's the second one created by yuri let's click on install and this is my go to extension when i install a new code editor whenever i work with html and php i don't want to write out every single word and with emit you can basically write down diff or any other shortcut hit tab and it will auto complete the word with a greater than and less than symbol around it let's also install github we don't need it but i'll do this for you because i know that it is very important to have it's created by knister peter let's click on install there might be a big chance in this course where we're going to use javascript so let's go ahead and write down javascript we need the second extension it has 4.2 million downloads let's click on install and what this basically does is the same as intellisense then for javascript and the last extension that i want to add is called vs code create icons and it's the first one created by emmanuel ziat let's click on install and what this does is basically adding an icon right in front of the file name so you can detect which type of file it is you can see it when you look at the file extension but if you're working on large projects it is awesome to use now this was it for installing our visual studio codes we might add a couple more extensions later on when we're deeper in the course but this is what you need to start off your laravel projects in visual studio code as you could see by the title of this video phpstorm is a paid code editor and it comes with a price tag of 195 euros per year in my opinion it's not worth it unless you're working in the company where they pay for it you could also test it out for a one month free trial so if you want to see what the difference is between phpstorm and any other code editor that you used before go ahead and try it out but i must admit php storm comes with a pretty cool feature and that's a free educational license so if you are a student you will get free access to not only php storm but every other jetbrain ide since there might be people watching this course that are students i'd like to quickly show you how you can set up your php storm for the best larval experience in my opinion phpstorm has a lot of extensions integrated that you need to manually install on any other code editor and that's awesome because it means that this video won't be too long so let's hop to google let's search for phpstorm and let's open the jetbrains.com website right here you can see a download button and a free 30-day trial so let's click on it and the download just started so pause the video and start it when your installation is done all right then guys let's unpack the dmg file it might take a second all right let's drag phpstorm into our applications and let's double click on phpstorm this might take a second because it's verifying let's open it let's allow notifications and you can see that our phpstorm is opening up we have our first laravel project set up so let's click on open let's go to our desktop and let's open our workspace and let's open our first project let's click on open all right let me go to full screen we're not going to do anything right here i'm not going to show you code but what i would like to do is to show you off how you can adjust some settings first off let me show you how to change the theme now if we go to phpstorm in the top left corner let's click on the name let's go to preferences and right here you can see our plugins that we could add but that's not what i want to do right now first off i want to focus on appearances and behavior so let's click on it and let's open appearances straight at the top you can see a section called team and if we click on the drop down you can see a list of teams that we could use whenever you want to change your theme you need to click on the team that you want so let's say darcula let's click on apply and you can see that your team has been changed now you can change any settings you want over here think about file colors material custom themes material themes but what i want to show you now is how we could add extensions because basically this is whatever you like and not what i like but i want to set my theme back to adam1 dark let's click on apply and all right this is what i like to be honest you don't need to add many plugins in phpstorm because most of them are already integrated think about getters and settings generators hovering over a pad and going straight into that file so let me show you how it works right here you can see a menu item called plugins so let's click on it you'll get a list of plugins and you could also search for plugins the one that i want to show you is called laravel so let's click on the install button let's click on restart ide so restart it right now and that's basically it this is how you add plugins in phpstorm i know it's not a lot but whenever you work with phpstorm you will understand that it's an all-in-one code editor that already has everything integrated i want to go through some important files inside the directory because every single file in here is actually pretty important but before we continue on i forgot to activate our visual studio code create icons so let's do that real quick because i want to see my folder icons right in front of the greater than sign if you're on windows go to file preferences or if you're on a mac hit shift command p now right here let's write down file icon theme let's open it and instead of disabling our file icons let's use vs code great icons right this is what i'm looking for if it actually if it does not work for you you could also go to the help navigation item right down file icon team and look this is where you could find it alright we're ready to kick off if we look at our application you might wonder where the model view and controller folders are and to be honest i was wondering the same exact thing when i started working with laravel i want to show you on a basic level how this application is built up and during this course you'll get to know the ins and outs of every little file inside of our directory to be honest there isn't a clear model view controller directory where we could create small components and use them within classes the folder names are a little bit different but don't get me wrong it's still an mvc-based framework by default the model classes are stored inside the app directory right here where you can find a models directory so let's click on it and by default there's always a user.php so let's open it you might wonder what the user.php file is because we've never created it and you're right because laravel will auto add a usage.php file later on when working with databases and modules you'll get a better understanding of this the models that we create right here can be used in our controllers to interact with the database now for the controllers you need to be in the same directory but instead of the modules directory let's open http and right here you can find the controllers directory let's click on it you can see this as the place where you are going to organize the behavior of your application so we're basically going to create some kind of model that we're going to use inside a controller in order to retrieve information from the database and then we're going to pull that data in the right location of the view which we're running in the controller if we open our controller.php file you can see that our class is extending the base controller and our base controller is basically an abstract class called controller which we honestly won't touch but we can use command click so let's click on it and right here you can see the base controller that we're using one thing is good to keep in mind is that well let actually close off the controller let's open our main controller and this controller that is extending the base controller is basically the file that our new files are going to extend so if we're going to make a usage controller we need to extend this controller file and instead of saying controller let's say class user controller extends the default controller but let me go back for now because we won't do it in this video now in order to see your views you need to open the resource directory right here where you will find the css javascript and views now let's open the views directory and in here you'll see a welcome blade.php file what are other important files in here well directories we have our database which i actually don't want to talk about right now because i want to make an entire section dedicated to the database we have our routes directory so let's see right here which has an api.php channels.php console and web now the api file is basically the location where you register api retours for our application the channels file is where we register all the events broadcasting channels the console.php is the location where we define console commands well the most important one is the web.php so let's open it and well this is the place where we are going to register routes which we will do in the next section now if you want to import products you basically need to place them inside the storage directory so let's say that you have a dot csv file well you basically need to stir them right here you can also find your locks back here right now it's empty because we haven't done anything but i'll cover this later on as well i want to talk about the emv file since it's a pretty important file now emv stands for environment and this is the place where you put all of our environment setups how this works is well you basically need to create an environment variable what we have right here so app underscore name and we need to set it equal to a value and as you can see there are lots of predefined environment variables in here even if we scroll down you can see a lot of them you have your database connection your drivers you have to read this host and way more the apple url the app key the app env and the app name everything about the server that is running in the project and many different values for different servers are set up right here and in some kind of way you can see these as constants a constant represents unique memory locations and an environment variable is a variable that is set outside of the program what i would like to show you right now is how we could use the environment variables just bear with me and follow along now if we scroll up let me close off the vendor directory let's open our routes and let's open the web.php right here you can see that we're referring to a specific view called welcome what we could do is change the return value so instead of saying return view let's delete that and right here let's say that we want to return emv parentheses semicolon so right now we're referring to our emv file so inside the parentheses let's add a set of single quotes and what we could do right here is to write down our environment variable so let's say db underscore database database now let's save it let's go open a new view let's go to firstproject.test and you can see that our database name has been printed out on the screen now let's return to visual studio code what i want to do is to do it one more time but instead of using a variable that is already defined for us let's create our own so let's open the emv file again and right above app name let's write down creator underscore name is equal to your own name so let's say dari let's save it let's go back to our web.php file and instead of saying db name let's change it to creator underscore name let's save it let's hop back to google chrome refresh it and you can see that our variable that we created has been printed out on the screen and to be honest i don't recommend you to use environment variables all the time but mostly for domain names urls authentication mail addresses or service account names you also may have noticed that there is an emv.example file inside our root directory so let's open it up let me close off the marketplace let's see what the difference is with the emv file well the variable names are all the same as you can see and what this.env.example file does is simply an example of what might be in the emv file so if you delete your emv file or make changes that you don't need you can easily rename the emv.exe and you can get started once again or let me give you another example what if you're working on a large project with multiple colleagues where you are using git does every single person needs to push the emv file to the master well no because everyone has his own database credentials and you want to keep the emv file for yourself what you could do is to send the example file to the master so you know what key and value pairs you need for the project there are two pretty important files which are the composer json and the composer log file that we have in the root directory a couple videos back i've talked about composer and i've said that it's a dependency manager for php that allows us the developers to specify project dependencies now this needs to be done inside the composer json if the structure of this file is new to you no worries this is a dot json file let's have a look and see what is happening over here we have pulled a project in with the name of laravel the type is equal to a project it has a description so the laravel framework and it has keywords it has a license of mit which is a permissive free software license well actually the next part is the most important part of composer and that's the requirement part what you see right here is the vendor name so php file developer fruitcake guzzle http laravel and laravel they are all separated with a forward slash so let's see what's going on over here let's open our vendor directory and let's write down or search for guzzle http let's open it and in here you can see another guzzle file so it's searching for the directory inside the vendor called guzzle http so it's requiring this specific file over here whatever is in the vendor directory is a library and composer makes it easier to keep vendor libraries out of the repo you might be confused right now but you'll get a better understanding of it if we move deeper in the course let's rewind for a one second the libraries that are added right here have weird numbers attached to it and basically this is the version control with a specific format so let's take the version number with three the first number stands for the patch the second number stands for the alpha and the third number stands for the beta you could also see that almost every version has a carrot right in front of it and the carrot stands for the version range so as you can see right here we have version 4.2 with a carat which is equivalent to let's say greater than or equal to 4.2 but the version is less than 5. so if fidel opera comes out with a new update and it updates it to 4.3 it's still compatible because it will look for a version that is 4.2 and it's less than 5. let me undo it all right so basically none of the releases until version 5.0 should break backwards compatibility alright right below the require section you can see a required dev right here and these are packages that are not necessary for your project to work you also don't need to add them in project version so if we delete anything inside you require our application will crash but if we delete something inside the required dev we're still able to run our project the convic and the extra options are not very necessary but the auto load might actually be right here some libraries that we add require autoload so if we go to our vendor now we scroll down to a file called autoload let's open it and you can see that it's basically requiring our composer let's go back to our composer file where you can see that we're requiring three things we have our app so well basically the entire app directory we have our database factories and we have our database seeders if you want to autoload a specific file or a specific directory you can easily add it well on the line below and it will always be autoloaded now to be honest the rest of this stuff is not very important as you can see in our root directory there's also a composer.lock i won't go too in-depth about it because it's pretty much the same exact thing as the composer json file but the point of the log file is that it basically specifies the exact version that we have installed so they can be reinstalled if we go back to our json file you can see like i just said that there is a carrot right in front of the version if we go to our composer lock you can see that there is a v you can see that the red has been replaced with a v so a definite version if you have any type of issue on the log file or you want to update some dependencies or libraries you've got you basically need to go to the terminal so let's do that let's cd into our workspace so desktop first workspace first project all right and in here we need to do a rm-rf space composer.log to remove our composer log file all right let's write down ls and you can see that our composer json file is still here but our composer lock file is gone now to recreate it you can perform either composer install or you could perform composer update there isn't a right or wrong here if you run the composer install it will check if the composer lock exists if not it will perform a composer update and create one if you perform the composer update it will check the composer.json file then it will determine the last version and it will install it and update the log file so let's do a composer install pause the video because this might take a second as you could see right here it's updating the dependencies and it's writing the log file that's ls and right here you can see that our composer.log file has been generated again the last thing that i want to show you related to composer is how we could add libraries into our composer log file with a single command if you have ever wondered how you could add apis like payment methods authentication resources or any type of library it's true composer as well whether you have worked on a training app or real life project most of the time you go to google so let's do that let's go to google chrome let's open a new tab let's go to google.com what i want to do right now is to show you how you could quickly and i mean a lot quicker than the manual installation is to install packages through composer the way people usually do it without a framework is while going to google search for let's say molly api php let's open the github repository if we scroll down to the installation process you can see that we have a composer installation right here and a manual installation part which is basically saying that we need to download it and include the vendor forward slash autoload that's not what i want to do what i want to do is to add the true composer this can be done by copying and pasting the command right here but what i would like to do is to show you a pretty cool website instead of using github let's go to packages let's click on packages.org you can see packages as the main composer package repository you can easily search for packages and use composer to manage your projects or libraries right here you can see a button browse let's click on it where you could well browse to all the packages that are available and there are a lot of packages available what i want to do is to search for molly it's the second option created by molly forward slash molly api php right here you can see a composer so let's copy it let's go back to our terminal and paste it right inside our first project directory let's hit enter and you can see that it's pulling in all the necessary libraries for molly when the installation is done you can see that it has been pulled in so what we could do is to go back to our visual studio code and in our vendor directory let's search for molly so right here and inside the composer lot let's command f and let's search for molly all right then you can see that a package with the name molly forward slash molly api php with a version of 2.24 has been pulled in from this specific github repository and the last thing i want to say is that sometimes when i do this it won't import the libraries immediately whenever i have that i basically go to the terminal and i write down composer update composer update to update the dependencies but usually it works when you install the library through composer like i said in the last video if we delete anything inside our vendor we could get it back very easy because we are requiring that specific file inside our composer lock so let's remove molly inside our vendor directory alright by doing that we cannot use mali anymore because it does not exist in the vendor directory if we go back to item and run a composer update one more time this might take a second all right let's hop to visual studio code where you can see that molly has been added once again we've touched on the vendor directory a little bit in the previous videos and specifically on the auto load file that's inside our vendor right here what this auto file does is basically including the composer dependencies and by now we all know that laravel uses a lot of libraries that are required through composer so it's good to realize that the core framework of our laravel application is located inside the vendor directory that also means that if we somehow mess the file up or delete it we can't run our larva project anymore which i also said in the last video if we take a look at our vendor directory you find a lot of folders inside of it which you probably never seen before or heard of these are all features that laravel automatically pulls in for us so it's not that you need to add them whenever you need them but they are already here when we need it if we hop back to our terminal we can perform a command called composer show dash dash tree alright right here you can see the dependency tree of our project within version numbers so let me scroll up because this is quite a lot as you could see all right take your time and see what kind of dependencies are in here and well you might see a lot of symphony dependencies and you might think well isn't symphony a framework of itself and yes you are partially right laravel uses a lot of third-party libraries which are mostly symphony components and to be precise 30 of the code inside our laravel project comes from symphony itself before we wrap up this part of the course i would like to show you the composer dump autoload and this command won't download anything but it regenerates the list of all classes that needs to be included in the autoload so let's click on enter oh my bad we need to add a dash so it's dump dash autoload and you can see that it's generating optimized autoload files as you can see right here these are the files that are required in our requirements let's go back to visual studio code let's open our composer json right here all right that was it so if the laravel vendor folder is missing or you're getting an error you just need to run composer install and everything should work fine we have finally reached the fun part of this course and that's where we are going to dive into the code and in this video i want to focus a little bit on basic routing let's open our browse directory and let's go to web.php and right here we have our route store that are assigned to the web middleware group as you can see we are calling a route class right here and we want to perform some type of request in this case we want to perform a get request and later on we will talk about other types of requests that we could perform but for now every single time the route receives a get request to the forward slash which is basically our url so our home url we want to do something now this forward slash is optional so if we remove it it will basically be the same exact thing as adding it but i prefer to use it so let me add a comment real quick all right right here if we want to go to firstproject.com we basically need to write down a forward slash but every single time you want to make a request to let's say firstproject.com slash users the get request needs to receive a forward slash users once again the forward slash is optional so the output of users or forward slash users will be exactly the same now inside our function right here we can do whatever we want and as you can see right now we're returning a method view with the name of welcome and honestly returning a video inside your web.php is not a good habit to get used to later on i will show you how we could return a controller which will then return a view if we switch to our terminal so to my i-term inside the directory we basically need to run php artisan serve all right it's serving right now let's copy the url let's go to chrome let's paste it in the url and this will redirect us to the welcome page of laravel let's go to our code editor and see what's inside the welcome.blade file so let's open our resources in our views directory and in here you can see a welcome page let's click on it all right and well let me actually see what we have inside our browser we have a button called documentation let's go to visual studio code ctrl f or command f let's say documentation and right here let's change documentation to doc save it hop back to chrome refresh it and we change the button all right let me undo that real quick because that's not what we want to do in this video i want to focus on endpoints so let's do that let's go to web.php again and let's create a new route but i basically don't want to copy paste it every single time let's just create it on our own so what we want to do is a route to users let's start off when you want a route called column we want to get request parentheses because it's a function semicolon inside our get request let's add single quotes forward slash users now outside of our first param let's add a comma let's write down function space parentheses curly brackets let's hit enter inside our curly braces let's return a piece of text so let's say return welcome to the users page let's close it off with the semicolon alright let's save it let's hop to google chrome and let's change the end point in our url so right now it's basically the forward slash and let's add forward slash users let's hit enter and you can see that we've been redirected to what basically the string that we're returning to the view so a quick recap whenever a user wants to make a get request to forward slash users which is the endpoint we are returning a piece of text of welcome to the users page whenever a user makes a get request to the home page so the forward slash we're going to look inside our views directory for a file called welcome what we have done until this point is creating a new get request and sending back a view or a piece of text but you need to keep in mind that all route should return a response to the browser of the user and a pretty cool thing in laravel is that there are multiple ways how we could return a response so what we've done so far let me add a comment is route that sends back a view we have a route to users we're returning a string and what i want to do is to create a new route but instead of sending back of view or string i want to send back an array so instead of copy pasting it again let's create it one more time so let's add a comment route to users dash array all right so we want to perform a route we want the get request we want the request to be towards forward slash users and once again comma function and we want to return set of brackets and let's add some text in here let's say php html larabel all right let's save it let's go back to google chrome let's refresh it and you can see that the output let me zoom in is an array with three elements so index zero is php and next one is html and index 2 is laravel alright let's go to visual studio code again keep in mind that laravel will automatically convert an array into a json response so let me add that as well but we could also send back a json object instead of sending back an array so let's create that one so let's say route to users but now as a json object let's say route get request and you'll get familiar to writing down the request very soon another request to the users page comma function parentheses curly braces and what we want to do right here is to return a new response which is a method so parentheses and we want to point to json which is also a method so let's add another set of parentheses and a semicolon let's go inside our json method let's add curly brackets and let's hit enter so in here we could basically say our name is equal to dari our course has a name of laravel beginners to advanced let's save it let's hop back to google chrome refresh it and you can see that we have a json object right here with a name and the course there's also a possibility to return functions so let's go to visual studio code and right here let's create another route to route to users but right now with a function so this is the last one so let's say route get we want forward slash users we want a function vertibraces parentheses and currently braces and we want to return a function called redirect where do we want to redirect the user to well let's say that whenever a user gets to forward slash users redirect them to the home page let's save it let's head back to chrome let's refresh the page and look we're back at the home page there are lots more return options but for now i want to keep it very simple and later on as we proceed in this course i want to show you how we could return a download file or redirect them with flash messages now that i've showed you a couple responses i want to show you how we could create a new page and link it right to our new route first off let's hop back to visual studio code and let's get rid of all of our routes except for the first one let's get rid of the comment as well so instead of saying welcome let's return a view called home if we save it and go back to our google chrome let's test it actually out to see what our error message is well our field is not found so let's create that one let's go to visual studio code and inside our views directory we need to create a new file and since the html portion of laravel is running on blade we don't need to add the html extension but instead we need a blade.php so let's do that let's create a new file let's call it home.played.php hit enter and you probably might wonder why and later on i will go in depth about views and blade but bear with me for this video let's save our page let's go back to chrome to see if our arrow will disappear refresh the page and nice this looks good let's go back to visual studio code in our home.play.php let's write down doc and hit tab inside our body let's create an h1 which says this is our new homepage let's save it let's hop back to chrome again refresh the page it's cool isn't it i remember how stoked i was when i did this the first time we've basically created a new view inside our views directory we went into our blade.php we said well whenever someone goes to our landing page or home page return a view called home and that has been done right now we've created our new file we have set up a route let's structure it a little bit with the controller in between we don't want to do logic inside the web.php file so what we could do is to create a new controller and there are two ways how we could do that we have a super fast way or the manual way and to show you the differences let's start off by doing it the manual way so in plain php we would go to the controllers directory so let's open app http controllers we will right click create a new file let's call it product productscontroller.php and then we would start off with php codes or php opening tag in laravel we need to define our namespace so let's do that it's app forward slash http forward slash controllers it's basically the path to well this directory and then we could create our class called products controller which extends the core controller and don't get me wrong this works fine but that someone timer did because it took me way too long instead of doing this we could use php artisan to create a controller for us to be honest not only a controller but a lot more things so let's hop back to our terminal i need to cd again inside my desktop city into my workspace and then i need to cd into my first project and by the way it might look longer now because i'm explaining it but when you get used to php artisan you'll understand that using artisan is so much more easier before i show you how we could create a controller let's write down php artisan with a flag called double dash help all right right here you can see a couple commands that we could perform in order to list all commands the one i use is called php artisan list so let's do that let's write down php artisan list what you've seen on the screen right now is a complete list of all the php artisan commands with examples so let's scroll up there are a lot of stuff that we could use whenever you want to perform some kind of artisan command and you don't know the name use the php artisan list command to list them all down they're all written down alphabetically by section name right here you have a command to clear your cache you have your authentication you have your database you have your convict you have your events you have your make and well that's actually what we need so what do we want to do we want to make a controller and the description is create a new controller class all right that's good so let's see what we could make let's write down php artisan make colon controller space and we're not done yet because we need to define the controller's name and before we do that let's hop back to visual studio code let's remove our products controller well let's save it first let me scroll up let's delete it move to trash let's go back to our i-term and let's say we want to create a new controller called products controller and let's hit enter as you can see on my screen we have created a controller successfully so let's see what the output is in visual studio code you can see that our products controller has been created our namespace has been set and it's using the request which is also good and it created a class for us isn't that cool if we remove our controller one more time so let's delete it actually if you were on a mac you could also press command backspace which will remove it and under windows it's probably control backspace so let's do that i'll use this way from now on so if you don't see me right click on a file name you know how i'm deleting a file all right let's hop back to our terminal let's set the arrow up but instead of hitting enter let's add a space and let's write down dash help so dash help flag alright so what are we seeing right now we have two types of flags you have a short version and a longer version first version is well basically the shortcut whenever you use dash i the output will be exactly the same as dashed as invokable and all these options have some meaning so if we try to create a controller to artisan that already exists it won't let us because it's already there let's do that let's hit the arrow up without the help so let's make a controller all right let's hit the arrow up one more time let's hit enter and you can see that we're getting a message because the controller that we're trying to create already exists what we could do is to add a new flag which is the let me scroll up dash dash force so let's do that let's hit arrow up again let's set space that's it dash dash force which will basically overwrite the controller that already exists so let's hit enter and as you can see it has overridden the controller that we had and it has created a new one and to be honest there are lots of more options which we will learn during this course we could even make a model and attach it immediately to our controller with a dash m flag as you could see right here which is making a model for us for now let's keep it simple with the basic commands that we just performed like i've said before we don't want to send back a view inside the web.php right here but instead we want to return a controller and then send back a view so first off in our web.php we need to add a new use of the right controller that we would like to use so let's scroll up and right here at the top let's say use and it's basically the path of our products controller so let's write down app forward slash http forward slash controller controllers excuse me forward slash with the class name so let's say products controller what's next we need to do something with the route to send back a controller so let's get rid of our route that we have right now and let's create a new one what we want to do is to create a new route with a get request all right the first param is basically what the endpoint needs to be so let's say forward slash products comma and now we need to define the class so to do that we need to add brackets followed by the class name so let's say products controller double colon class all right then we need to add another comma and that's the function name that you want to use inside your projects controller right now we don't have one but let's add in single quotes index what's going on right here we're saying that we have an endpoint called forward slash products we want to pass in a controller so the products controller and perform whatever is inside the index function in our products controller class so we don't have our index function so let's go to our products controller let's get rid of the comments let's create a public function index and in here let's just return a view and well we don't have a view right now so let's do that first insert our views directory let's create a new folder let's call it products and inside our products let's create a new file called index.blade.php all right let's write down doc hit tab go inside the body create the h1 called products let's save it let's go to our products controller we want to search inside our views directory for a folder called products with the file name of dot index the dot is basically a forward slash so products forward slash index let's save it and let me actually save my web.php as well let's go back to chrome and let's change the endpoint to forward slash products and as you can see products has been printed out on the screen now this is option number one there is also a second way that is new since laravel 8. so let's go back to our web.php file and right below our route let's create a new one well let me add a comment first which says laravel 8 this is a new way all right so let's add a new comment laravel 8 also new the second method is basically to use a string syntax and i actually think that this is pretty much a long way of doing it but since it's new to laravel 8 i want to show it to you so what we could do is to add a new route so route column column get all right let's go inside the parentheses let's add an endpoint of forward slash products second argument so comma is single quotes the entire route to the product controller so once again it's app forward slash http backslash oh excuse me that's a backslash controllers backslash products controller but instead of adding a new parameter we could basically say add so search for a function or method called index all right let's save it well let's actually comment out the first route i know it works because it will override it but just to show you that this works as well in our forward slash projects endpoint let's refresh it and the output is still the same now before i wrap up this video i want to show you how this was done before laravel 8 because if you google on route issues you mostly run into old versions and it's a little bit different since lro8 came out so let's go back to our visual studio code let's uncomment it and let's create a new route so before laravel 8. i just want to show you what the difference is the beginning of the route is the same so the route common column get inside our parentheses we still need to add our endpoint so forward slash products we also need to add a second param but this is a little bit different in here we basically need to add the controller name so products controller add followed by the method name so in our case let's write down index again if we save it and hop back to google chrome refresh it and we're getting an error message because the old method is not working anymore like i've said i prefer to use the php callable syntax so the first method that we did just to give you one more example let's do it one more time let's create a new route to a get request to forward slash products forward slash about then we want to add a comma with the controller class so bracket products controller column column class and the method is about we don't have it right now but we will do that in a second i'll basically want to show you that in our products controller we could send back whatever we want so let's say public function about and instead of returning a view let's just return a basic stream so let's say return about us page let's save it save our web.php as well let's go back to google chrome let's change the endpoint to forward slash products forward slash about and the about us page has been printed out on the screen now that we've created our controller and we're able to return a view i want to pass in some data to the view there are a couple ways how we could do it and i want to show them all so let's start off with the first method which is the compact function and what the compact function does is basically creating an array of the variable with its value so let's go to our controller right above our view let's basically create a new variable called title and let's set it equal to welcome to my laravel 8 course all right let's go right inside our view method right after projects.index let's add a comma so we need to add a second parameter view and we want to pass in is compact which is a function and inside the parentheses of our compact function we need to pass in the name of the variable so let's say in single quotes title so basically this title right here is referring to the title variable that we have let's hop to our view so our indexblade.php in the products folder right below our h1 let's create a paragraph and to add variables from the controller inside the views we basically need to add a double set of curly braces the next section is dedicated to view so we'll go over it in a bit in here let's write down our variable name called title let's save it let's hop back to google chrome let's change our endpoints to forward slash products oh i made a mistake right here let's go back to our web.php and well let me get rid of the last two and the products slash about as well and we don't need to comment for now save it let's go back to google chrome alright this is what we need welcome to my laravel 8 controller has been passed in through the controller it is also possible to send multiple variables back to the view so let's create a second one called description and let's set it equal to well created by dari let's save it and right after title let's add a comma and let's add description well let me outline this actually all right let's go back to our view let's create a new paragraph let's call this one description save it now back to google chrome refresh it and create it by dory has been printed out the second way how we could pass in data is with the width method so let's go back to visual studio code to our controller let me actually comment out the compact method oh yeah and what i just did was selecting every line that i want to comment out hit pro or command forward slash and let me add one more comment this is the compact method and the next method that i want to show you is the width method so what do we want to do right here we basically need to return a view again and inside our parentheses we need to write down the path so once again it's products.index and right after our parentheses our closing parentheses we need to create a pointer so dash greater than and we want to point to a width function then we need to pass in the name that we want to give it as a string so let's say in single quotes title followed by the variable name so that's separated with a comma title so we're passing in title with the width method let's save it let's hop to google chrome let's refresh it oh yeah of course we need to get rid of the description inside our view let's do that save it let's go back to google chrome and welcome to my laravel 8 course has been printed out i like the compact method and the width method but keep in mind that the names of the variable and the placeholder name needs to be identical the width function is good to use if you want to pass in one variable so you cannot add the description either it's also possible to pass in an array with the width method so right below our description let's create a new data array so let's set it equal to brackets go inside our brackets hit enter and let's say that product one is equal to iphone we're creating an associative array right here comma let's add a second value of product two and the value is samsung well let me actually change the name for the iphone lovers all right let's copy paste our view let's comment out the first one because it's a string let's change the title to data and the title to data as well let's hop to our index.play.php view because title won't work right here so let's get rid of it and just like with any other programming language we need to loop through an array to print out the values and to create it for each loop in laravel let's write down the add sign followed by 4 each set of parentheses and we need to close it off as well so let's say at and for each all right so let's go inside the parentheses because we need to do something right here we know that we have passed in our data right here so let's do that let's say that we want to loop through data as a single item insider for each loop let's add a paragraph and inside the paragraph let's write down curly braces followed by the item so one single item every single time save it go back to google chrome refresh it and you can see that our data iphone and samsung has been printed out on the screen there's also another option to create it for each loop because we have emmet let's write down for each let's hit tab and you can see that our for each loop has been created and this is basically what i'll be doing from now on all right the next way is well not the way i prefer but i'd like to show it to you let's go back to our products controller let's comment out the view and let's create a new one because right now we're going to pass it directly in the view let's create a new view so return view to products.index let's add a comma but instead of adding a variable name or the width or compact method i want to pass in our array right here so let's add brackets hit enter let's write down the key which is equal to data let's add a value so let's point it to the variable data that we have right here let's save it let's go back to the browser let's refresh it and the output is well exactly the same what we've done so far was doing a request to a single page right now i want to focus on route params this might sound difficult but trust me it isn't sometimes you want to pass in some kind of data to a specific file to render dynamic data the first thing that i want to do is to clean up my page a little bit so let me get rid of all the comments that i have and all right this is good for now let's hop to our web.php whenever a user does a request to forward slash products it will display all products but the user might also have an option to click on a button that then shows data of that specific product if you have ever noticed in the uri of a shop you basically have seen a well let's say forward slash products forward slash product name or a forward slash products forward slash product id what i'm showing you right now is basically a name param so let's create a new request let's say route get to the endpoint of products forward slash and right now we need to define our route param and in laravel they are always encased within a set of curly braces so we're opening and closing curly braces and this should consist of alphabetic characters so what i want to do is to say to id i think it looks better and it's a little bit easier to read and we need to pass in the controller so products controller class comma and a method that we haven't created but we will do called show which will show a specific product alright let's hop to our controller let's scroll down right below the about function let's create a new public function show that we just mentioned in the web.php think about it do we need to add something in here well yes we've added an id in the web now we need to use that same id inside our controller so let's pass in variable id now if we go to the browser well if we add something after our endpoint so let's say an integer of 24 nothing has happened well we're not getting an error message and that's good so we're missing something we need to perform some kind of action to get the id in the url alright let's return to our view and inside our function let's return that specific id which we defined in the web.php so let's save it let's go to chrome refresh it and you can see that 24 has been printed out on the screen which is equal to the endpoint that we have if all this is new to you let me explain you one more time what we just did in our web.php we created an endpoint we've said that if there is a request in the uri to anything after products grab it and set it equal inside the controller to an id variable and basically return that specific id let's do something else right now instead of using ids let's pass in a product name and again let's start at the route so instead of using id let's say name let's go to our show method let's get rid of the return id and let's change it as a param to name let's actually copy the array that we have inside our index let's paste it right inside of the show method let's change the keys to iphone and the second one to samsung all right right outside of our array we need to return a view of products dot index brackets and let's send back an associative array so we have our products to our data array brackets and we want to see if there is a match between the name that has been passed in in the uri with the key of our array so let's say variable name all right before we go back to our chrome let's hop to our views let's get rid of before each loop because it will cause an error let's create a new paragraph again for the braces variable products let's save it let's go back to google chrome but instead of saying 24 let's write down iphone and you can see that iphone has been printed out if we change it to a value that does not exist in our array so let's say u1y we're getting an error because huawei is not defined as an index so what we could do is to hop to our controller and right after our array we could add a nuclear listing operator and we can do that by adding a double question mark add a string so let's say product concatenate variable name does not exist actually need to word wrap it in a second all right let's save it the double question mark returns its first operand so first what's on the left hand side if it's a no it will return the second operand which is a piece of text that we have let's save it let's go back to chrome let's refresh it and product 1y does not exist has been printed out on the screen working with route params is pretty much a must when you start to develop bigger websites and especially e-commerce sites when you have a lot of products or web pages this is the way to go what we've done until this point was creating a route program where we expect an id or a string in the url and it works but is that really what we want if we expect a string from a user in the url why would we give them the opportunity to write down a string when we expect an integer or vice versa well probably just like you i've watched the videos of laracast as well and the one thing that stood out to me was when he said users are guilty until proven innocent so what we could do is to prevent users to write down the wrong data type in the url with the where method on a route instance so let's hop back to our web.php file let me open my routes let's open the web all right let's start off with the id that we're expecting inside our route let me add a comment which says the pattern is an integer what i want to do right now is to only let the user add an integer in the url let's analyze this on the second line now after the closing parenthesis let's add a point to the where function and inside the where function we need to pass in two params the first one is the name of the param and in our case in single quotes we want to refer to the route param called id which is equal to the id in the get request as a second param we can pass in a regular expression which defines how the parameter should be constrained if you have worked with login systems in regular php before you might have used a pattern to validate phone numbers or email addresses but for now let's say comma let's add single quotes brackets and inside the brackets we want the id to be in between the number 0-9 all right let's save it and test it out let's go to google chrome let's write down 9 so project 9 does not exist and this works fine let's try out a string so let's say iphone welp 404 the page does not exist this is pretty cool isn't it but all right there's one issue right here in the web.php we basically said that the number or the integer needs to be in between 0 and 9. so if we enter 10 in the url we're still getting the 404. and in order to fix this we need to make a little change so let's go to visual studio code in our where function we basically need to go outside of the closing bracket and write down a plus sign save it refresh the browser and this works so right now we're basically saying that it needs to be a number but it can be more than one number with a plus sign so a number is zero until nine but ten is two characters so with the plus sign we're saying that it can be multiple integers right next to each other whether you're an advanced programmer or not this is something you will use a lot and the same thing can be done for strings so let's copy paste our method and comment let me get rid of the comments right here all right let's say that the pattern is a string let's comment out the first pattern because we don't need it right now all right so we're doing a get request to projects forward slash id well let's actually change it to name referring to the products controller class the method show where not the id but the name has a pattern of well not 0 till 9 but a until z lowercase a dash z uppercase let's actually turn on word wrap so let's go to help let's write down word so let's toggle the word prep all right so right now we allow a pattern as a route param with letters in capital and lowercase but if you only want to have lowercase you could basically get rid of the a until z and uppercase but it doesn't matter when you work with urls now let's go back to the url and change it to let's say iphone all right iphone has been printed out let's test an integer out so two two three four and we're getting a 404 so it actually works the last thing that i want to show you is how we could add multiple patterns so let's say that we have an endpoint of well iphone forward slash 12. so right now we need a pattern for iphone and empty for 12. and this is easier than it might look because we could easily pass in an array in the where method so let's do that let's go back to visual studio code let's actually get rid of everything anywhere all right let's change the end point so we want the name forward slash id all right in the where method let's add a set of brackets and let's hit enter let's set the name equal to well single quotes brackets a dash c closing brackets plus sign outside of the single quotes let's add a comma and then let's set the id equal to brackets 0-9 bracket plus sign let's save it all right let's go back to google chrome refresh it this works fine but if we change the end points to 12 forward slash iphone we're getting a 404 because the pattern needs to be in the same order as the end points let's talk about another interesting topic called name droughts and honestly main browse is something i use a lot when i work with laravel it lets you refer to our route when generating redirects or urls more comfortably and this may sound weird so let's test it out well first off let's get rid of our last two routes because we only need the basic first one which is referring to the index what we want to do in simple terms is basically to assign a nickname to this specific route with the name method let me actually align it first all right right after our route let's point to a method called name and inside the method we could write down any name that we want and this could be identical to the endpoint products but it could also be something else but what i actually recommend is to keep it identical to the endpoint so let's say in single quotes products now let's save it let's go to our index function in our products controller all right and actually let's clean it up because we don't need our variables in array let's get rid of the data that we have inside our return view all right let's save it let's clean up our view as well products index and let's get rid of our paragraph all right so in our products controller excuse me before we write down any code it's good to know that the route method is a global method which basically means that we could use it anywhere in our code now whenever we want to specify the index page of our products directory we basically could say well we want to print underscore r a route which is a method that refers to the name of products let's save it well let's change the endpoint to forward slash products and you can see that our entire url has been printed out this is also pretty handy when you work with buttons so let's hop to our views so our index.played right below our h1 let's create an entry let's name it well let's say products and the href needs to be curly braces we want to say that route is single quotes products save it up to google chrome refresh the page let's click on products and the endpoint is still the same this is very useful when you want to link stuff easier to each other and you can easily change the url without changing all of its references like i've said larval comes with a blade templating engine which lets us design a unique layout called blade two of the primary benefits of using blade are template inheritance and sections just like in php most of the web applications will have the same layout across the web page think about the navigation their footer or the head in addition to template inheritance blade comes with lots of shortcuts of common php control structures instead of constantly adding the well let's say the opening php tag and the closing php tag and inside of it to create an if statement we could basically well let me get rid of it say php hit tab which will create a php directive for us we could also say if go to the if block hit tab and we got an if statement directive and the cool thing inside of the directives of lara fill itself is that we could add html elements inside of it at the time i started recording this course i was also working on a youtube miniseries where i was creating a personal portfolio if you click on the link resources on this specific video you will see a github repository a link in the description it will redirect you to my github repository where i have a personal portfolio for beginners so let's open it i will also add the code inside the resources by the time you're watching this video there will be much more pages available but at this moment i just finished the home page and that's exactly what we need for now what i want to do is to migrate this project in our current project so let's click on code that's downloaded as a zip file this might take a second before we migrate my html and css code i want to clean up my code a little bit so what i want to do is to get rid of my products controller because well we don't need it right now so let's delete it let's move it to this rash let's get rid of our products in the view so let's well let's delete it all right and let's go into our web.php and let's change up the route let me zoom in a little bit let's change the route to forward slash and let's change the controller to pages controller let's also get rid of the named route that we have because we don't need it and let's put it on one line all right let's hop to our terminal to create our pages controller all right in here let's say php artisan make a controller for me called pages controller let's hit enter and our controller has been created successfully let's hop back to visual studio code we can see our pages controller right here let's open it and what i want to do on my pages controller is to create our basic pages so let's create our first function called public function index and let's just return a view with the name of index which we will create right now so inside our views directory let's get rid of the home and welcome file we don't need it all right now inside our views directory let's create a new file called index.blade.php and what i want to do is to paste our project in here alright so let's hop to the file that we just downloaded called portfolio for beginners let's open the index.html in visual studio code all right let's copy everything and let's paste it right inside of the index.blade.php if we save it right now and go to the browser the output won't be good because the stylesheet hasn't been linked but don't worry about that for now we actually forgot to do one more thing so let's hop back to our web.php and let's change the use of products controller to pages controller otherwise it won't work alright let's hop to the browser let's change the endpoint to forward slash so not to forward slash products and this works fine well let's scroll through it because it does not look good but it's all right no worries whether it's the home page the about page or the contact page all of our pages will have the same header and footer so let me show that to you so at the top of our page we have our html tag and our entire head tag we have our body which will be different on every single page and we also have our footer at the bottom let's see it's right here which will also be the same so what do you prefer do you want to copy paste it every single time into every single page well no right you only want to change the actual content inside the body let's go to the web.php to create a new route and on the line below let's say route get to forward slash about comma pages controller click on colon class and the method is about all right let's save it let's hop to our pages controller let's create a new method called about and let's return a view called about let's save it let's create the about page in our views directory called about.blade.php and let's copy our entire index page inside of our blade all right let's save it and this is what i mean you don't want to repeat yourself by adding the footer and the header inside the about page as well so what do we need to do right here let's hop to the index page now as you could see everything inside the head and footer tags are redundant because we've got it inside the index page too now what we could do is to create a new directory inside our views or we are going to store redundant stuff like the header the head and footer so let's create a new directory inside our views called layouts and inside our layouts let's create a new file called app.blade.php if you have worked with php before you usually had a footer.php a head.php it's a little bit different with laravel we could basically paste our entire page right here and remove everything that is actual content and that content will be changed on every single page one more time the structure of our application will be inside the app.blade.php but the actual content will be stored inside the index or about page so let's do that let's get rid of everything above the footer until the header let's do that so until the header let's get rid of it now in here we could write down an add sign followed by the keyword yield parentheses and yield is used to define a section in a layout and it's constantly used to get content from a child page into a master page so the app blade is our master page and the index page is the child page we need to give our yield a name so it's basically a section so let's say in single quotes content let's hop to google chrome refresh the page and well nothing is happening right here because we need to do something with the content inside the index.php file alright so let's hop to the index file instead of getting rid of the content let's get rid of the footer all right and let's get rid of the head and header so right here so we only want to have our actual content inside the index.blade.php file the first thing that we need to do is to say that we want to extend the layouts.app file so let's say add extends at the top of our page parentheses single quotes now right here we need to write down the path to the f.blade from the views directory so let's say layout.app now we still haven't done anything with the content from the app.played so let's do that let's copy everything that is content for a second all right let's hit backspace and right here we need to wrap the entire content that is different on pages inside something which is called a section now this section needs to receive a name and it needs to be identical to the yield name that we have in our f.blade so it needs to be content so let's do that let's say single quotes content a good thing to know is that you always need to close off your section so right afterwards section let's say the couple enters and let's say add and section now let's save it let's hop back to google chrome refresh the page and as you can see we only have our header so our logo and menu items and our footer so what we could do right now is to place our actual content inside the section all right let's save it let's hop back to grow refresh it let's scroll up and you can see that we have our content so the body we have our header and we have our footer but we have separated from each other so let's rewind for a second what did just happened here so let's go back to visual studio code in our app.blade.php which is basically the core html we separated the head header so let's see we separated the head the header which is our navigation and our footer from the actual content in our page we have linked the actual content with a yield to a section called content then we went into our index.blade.php file and said hey extend everything that we have at the top from layout set app and fill in the yield section with this actual content section we can even go deeper than that we could also separate the navigation with another method called include so let's create a new file in our layout directory called header.blade.php and in here let's store our entire navigation so let's grow up all right we have our header but this is optional you could either take your header tags or you could only copy the actual content inside of it for now let's only copy the nav oh that went wrong oh my god all right let's copy it let's go back to the header.blade let's paste everything in here let's save it and let's go back into the app.played let's get rid of the nav and insert our header text we can use another tag called include so let's say add include parentheses and inside the parentheses we need to write down the path which is layout.header let's save it let's hop back to our google chrome refresh it you can see that our navigation is still here so let's do the same thing for the footer let's create a new file called footer.blade.php let's go back to our app.played let's copy the entire content inside of the footer let's say that we want to add include in single quotes a file called layouts well inside the layouts directory with the name called footer let's go inside our footer.blade and let's paste it right here actually forgot to copy the last div oh yeah i did let's close it off excuse me let's save it let's go back to google chrome refresh it and our footer is still here i quickly want to show you how we could easily change the active link in a menu since it's a pain in the ass to do it in php i also think that this is one of the most common operations on every website that you work on so there are a couple ways that you could use but i want to show you two ways that i prefer so let's go back to our code editor inside our header.blade.php file let's create a new answer so let's copy one let's paste it oh this went wrong my bad let's change the first one to home let's change the href to forward slash second one to about third one to portfolio and the last one to contact oh actually let me show you one more thing that we could do you can see that we're two tabs to the right which is pretty annoying and instead of having a backspin on every line we could copy it press ctrl or command and hit the left bracket to go to the left and the right one so the closing bracket to go to the right and i think this looks way better alright but let's continue on the way i usually add dynamic classes is with the request method right after the href let's create a class set it equal to nothing at the moment and in here we need to use laravel so let's add double set of curly braces and then we need to call a method called request then we want to call a property well a method called is so the pointer means that we're calling a property or a method and inside the is method we need to see if there is a match with the forward slash in the url because well that's the home page let's go outside of our is method and we need to use a ternary operator so a space question mark space if it is true so if the request is equal to forward slash so basically nothing after the page name give it a class so in single quotes of active else so colon if it's false keep it empty so only a set of single quotes let's do the same thing one more time for the about page so let me align it you don't need to do it once again it's optional all right so we want to give our answer a class or not obviously in laravel code we want to see if the request is in single quotes equal to about if it is so ternary operator again we want to say the class equal to active and else keep it empty all right let's save it because we don't have our portfolio on contact page but we need to do one more thing well we need to do something with our active class so let's go to the app.played and inside our ad let's create a style tag let's style the class dot active and let's give it a color of red nothing special right now let's save it and one more thing that we need to do is to copy everything inside the index page to the about page because while the about page is the static page that we had so we don't want that all right let's save it let's hop back to the browser let's refresh it all right we have no endpoint so we're on the home page you can see that our home link is red at the moment let's click on about let's scroll down and the about link has been highlighted it is also possible to check if a url uses a particular pattern let's say that we have about forward slash a user so dari or john but you still want the about page to be highlighted well let's hop back to our visual studio code to our header and this is actually very easy the only thing that we need to do is to go inside the is method after the about add a forward slash asterisk and the about page will still be highlighted we have the option to show images directly from the public directory in laravel 8. and basically you can save files or wherever you want within your application since we've got the permission to create directories and files anywhere that we want there are two ways that i recommend in order to output images on your website the first way is to store it inside the public directory right here and there's also another way which a lot of other people use and i prefer it when i work on large applications with a lot of images and that is to link the storage directory to the public directory if we hop to the folder that we just downloaded for my github repository you can see a folder called img in here so let's click on it and you can find three images so let's go back to visual studio code let's right click on the public directory and say reveal in finder all right so let's go inside of republic directory and in here let's create a new directory called images what we need to do is to go inside our images directory let's copy the images that we have for my github repository and let's paste it right inside of the images folder of our laravel project now let's all back to visual studio code let's open our public directory you can see the image directory that we have and the three images alright let's open our index page because we have a couple images right here so let's scroll down well this is the first one which is the icon box let's actually get rid of everything inside the source and here let's create larval tags where we want to use the url function and inside the url function we need to write down the path to our image from the public directory so let's say images forward slash icon dash box dot jpg before we save it and test it out in the browser let's go right above our div and let's create laravel text and let's do one print underscore r just to see what's in url so let's say that we want to print our url single quotes save it and let's hop back to the browser all right let's refresh it well we need to go to the home page let's scroll down as you can see right here the url method is a helper that is used to generate urls for your application and it's exactly the same as the url that we have right here you can see that our image has been printed out so this works fine let's get rid of the print underscore r and let's change it one more time or the second well we could copy it actually for all the other images let's replace them all right we have another one let's scroll down all right this is a different one so let me align it get rid of everything in the source curly braces url method inside the url method we need to have the path till images forward slash well what was the name actually all right team dash section dot jpg i guess let's see oh that's home my bad let's save it now back to google chrome refresh it and all of our images have been printed out the second approach that i would like to show you is to create a symbolic link and this can easily be done in the command line so let's hop back to the terminal and inside the terminal let's write down php artisan we want to link the storage directory so let's write down storage colon link let's save it and as you can see right here a symbolic link has been created from the public storage to the storage app public directory all right let's go back to visual studio code and right here you can see a storage directory let's select every picture that we have and let's drag it right inside of our storage directory well are you sure that you want to move the following four files obviously let's delete our image folder all right now if we save it and hop back to google chrome it won't work because we have the wrong path so let's go back to visual studio code and let's change it up a little bit let's scroll down and let's start off with the first one right now it's not stored inside the image directory anymore so let's change it to storage let's copy storage and change it on every single place all right images and that was the last one yes save it go back to google chrome refresh the page let me scroll up and we have all of our images printed out you might wonder which one is the best and in my opinion if you want a little bit more security on your images use the storage way if you want to keep it simple create a folder in the public directory and it's fine as well but just know that using the storage method you are saving all private images and documents in the storage directory which will give you full control over files and later on you can even allow certain type of users to access the images or documents and oh yeah before i wrap up the video instead of using url that we have right here we could also use a method called asset let's save it let's go back to google chrome let me scroll up and we still have four images right here the asset is a method that is specifically used to include css javascript and image files but you need to remember that it needs to be stored inside the public directory so that's the only place where the asset method will look either way is fine whether you use zero l or asset i want to quickly show you how we could easily write control structures in laravel since blade provides a very convenient syntax for it so i don't want to touch the index page but let's go to the about page and that's working there let's get rid of everything all right every control structure in laravel begins with the add sign followed by the type of structure so there are multiple and we will go over all of them we also need to end it by going down and say add and structure so if we have an if statement it's and if we have a four it's ant 4 and so on inside the section we can write down normal html tags so we could use h1s or h2s or even diffs anything that you want it's also possible to include variables right here with the blade syntax so the double curly braces and let's say variable name now what i want to do is to start off with the if statement let's say that we have an add if parentheses and on the line below you need to close it off with end if inside the parentheses we could add a condition to see if something is true or not so let's say that if 5 is less than 10 if that is true print out a paragraph called 5 is lower than 10. at this moment it is actually true because 5 is less than 10. so let's save it let's hop back to chrome let's change the end point to forward slash about and let me zoom in because 5 is indeed lower than 10. but what if 5 is greater than 10 which is not true so let's save it and as you can see nothing has been printed out in order to fix this we could create something which is called an else if statement so if the if statement is not true check the else if and for that we need to go right below our paragraph let's say at else if which has a condition so parentheses so let's say that if 5 is less than 10 print out a paragraph of 5 is indeed lower than 10. let's save it let's hop to chrome refresh it f5 is indeed lower than 10 has been printed out now if the if and elseif are not true we can print out any piece of text that we want with an else statement but before i do that let me show you what i mean so let's say that if 5 is double equal to 10 save it google chrome nothing has been printed out and for that we could use an else statement so if the if and elseif are not true we can print out another piece of text in the else statement and it's the same process right below or else if let's create an else which do not have a condition because it will print it out if the if and elseif are wrong let's say h2 all conditions are wrong let's save it let's go back to google chrome refresh it and all conditions are wrong has been printed out there's also something pretty cool in laravel that php does not have and that's the unless direct riff but before i show it to you let's hop to our controller because we need a variable for it so inside our pages controller let's say in the about function that we have a variable name let's set it equal to dary well let's change it to john let's say with name and let's pass in variable name all right let's save it let's go back to the about page and let's create the unless directive since i've got emit installed and i think i've said this before we could write down unless hit tab and our syntax has been created now as a condition let's say the function empty we want to see if name is empty or not and if it is let's create an h2 which says name isn't empty let's save it let's hop back to google chrome well right now you can see that the paragraph is printed out so what's going on right here because name is not empty right because we just have set it equal to john well the unless directive is the opposite of the if statement so in php we would have done something like this we would have said that if it's not empty variable name print out name isn't empty save it refresh the browser and this works as well let's get rid of the if statement and i've actually touched a little bit on the empty function how we used to do it in php but larval comes with a pretty cool directive which is the empty directive so let's write down empty and hit tab and this empty directive will check if a variable or input field is empty or not so what do we want to check well to see if name is empty or not so if it is empty let's say name is empty save it hop back to google chrome and well it's gone because name is not empty and if we add the explanation mark right in front of it you can see that name is empty because it's the opposite if we get rid of the explanation mark and change name to let's say second name save it back to chrome name is empty has been printed out because variable second name does not exist and therefore it's empty and the last shortcut that i want to show you is the iset directive which is the same as the empty method in php this set is one that people use a lot so let's write it down let's say is set hit tab and this method does a check if a variable has been set or not it returns true if it has been set and it returned false if it hasn't been set so let's say variable name let's see if it has been set if it is let's say name has been set save it check chrome and name has been set has been printed out since laravel 7 there is an option to create a switch statement and is mostly used to perform different actions based on different conditions so before we continue on let's remove everything inside the about.php and let's add a comment because i want to add some notes right here alright i recommend using a switch statement if you are comparing multiple possible conditions of an expression so let's say comparing multiple possible conditions and sorry talking and typing is very difficult you have multiple values that may require the same code or you have some values that will require essentially all of another's value execution so let's create our which statement so let's go right outside of our comments let's write down switch hit tab you can see my switch directive and inside the switch parenthesis we can add a condition that we want to check since we got our variable called name let's just write that down alright what's next well we have cases right here and you basically can see one case as one if statement it's just one check so let's get rid of the one let's add single quotes and well let's say dory you can also see that every case ends with a break directive and this is mainly used to prevent the code from running into the next case automatically so if our variable name is equal to dory we want to somehow stop it from running and that's done with the break so let's create an h2 in here because if variable name is equal to darby let's say name is story case number two let's say if it's equal to john print out name is john now let's create a new case as well so right after the break let's create a new case to see if name is equal to well let's say michael if it is h2 name is michael let's save it let's go back to google chrome and let's refresh it and name is equal to john now what if there is no case whether we set john equal to david so let's change the h2 to david as well let's save it let's go back to chrome refresh it and we have nothing on the screen in any case even if you know that there is a match i always recommend you to use the default directive that we have right here the default directive will be executed if no constant expression value is equal to the value so right below our default let's create another h2 which says no match found let's save it let's go to google chrome refresh it and no match found has been printed out i want to quickly show you the syntax of loops and for the people that are new to coding or haven't done it in a while you can see a loop as a coding structure that will constantly repeat a sentence or instruction until a condition has been met and that condition has been provided by us so we have a couple loops let me get rid of the switch and the comments we have the for loop we have the for each loop we have the four else loop and we have the while loop and let's start off with the for loop let's say add four and let's close it off and four now inside the parenthesis we need to add three parameters the first one is the initialization which is used to initialize the counter variable so in our case let's say a variable i is equal to zero and we also need to close it off with a semicolon then we got the condition which is the evaluation if this is true the loop will continue and if it's false the loop will end so let's say variable i is less than 10. so it will loop until variable i is less than 10. well right now variable i is equal to zero and will be equal to zero every single time so we need to add one more param which will update the loop counter with a new value so let's say variable i plus plus because i want to let it increase with one every single time so inside our loop let's create an h2 i don't know why my syntax doesn't work which says the number is and what i want to do is to print out the variable name so curly braces variable i let's save it let's hop back to google chrome refresh the page and the number 0 until 9 has been printed out well right now the starting point is 0 but the last number is 9. let's set that equal to 10. so what we need to do is to loop until variable i is less than or equal to 10 so let's save it back to chrome and the number 10 has been printed out as well and this is basically it for the for loop now for the second loop which is the for each loop we need to have an array because it iterates over every element so let's go to our pages controller let's change name to names and let's get rid of john sorry john let's create brackets because it needs to be an array let's add names so we have john john's back we have michael we have david and we have jessica all right we need to return a new view but let's get rid of the width method and inside our view method let's add a comma brackets and let's pass in names as variable names all right let's save it let's hop to our about page and let's create the for each loop let's write down for each and you can see a collection as one single item now what does this mean we have the collection which is the first param and that is basically the entire array that we want to loop through so in our case it's names now we have the keyword s because we don't want to print out the entire array but the specific element as well right now it's called item but let's change it to name so we have our entire array called names but we want to loop through it and print it out as one single name so let's do that let's create an h2 let's say the name is variable name all right let's save it let's refresh the browser and the name is john michael david and jessica have been all printed out we also have the for else loop and this is not a loop that you have seen in php or well most programming languages so let's go to visual studio code and let's write down four else because this works on an array as well let's say add four else parentheses and let's close it and for else the idea behind it is pretty much the same as the for each loop but the for else loop has a built-in code that will do a compile check for an empty input so we want to loop through variable names as one single name if it can be done let's create an h2 which says the name is variable name but what if it isn't so let's say that variable names is empty well after the h2 we could create something which is called at empty so let's say h2 there are no names let's test it out let's save it let's go back to chrome let's refresh it and you can see that john michael david and jessica have been printed out just as the for each loop but if we go back to visual studio code and let's go to the pages controller and let's get rid of all the names inside our names variable let's save it let's go back to chrome let's refresh it and the empty directive has been printed out right here all right let's go back to video studio codes and let's return our names because i don't want to get rid of john michael david and jessica because we only have one loop left which is the while loop and the while loop is mostly used to evaluate expressions inside the parentheses so let's say at wow for this we can work with the array so let's create a variable called i and let's set it equal to zero this is by the way not the way to create variables in your views but let's do it for now while variable i is less than 10 is to basically print out variable i now if we save it right now it will create an infinite loop because we are never increasing variable i which is actually a problem because our application will crash so what we need to do in a while loop is to always increase it with something variable i plus plus let's save it let's hop back to google chrome let's refresh the page oh i made a typo right here my bad h2 needs to be variable i so let's save it refresh the page and this is a duplicate but well you get the point i have walked you through a lot of front-end stuff by now but the most important one is the usage of assets like javascript and css so that's what i want to do in this video by now you guys know that laravel is a php framework based on backend coding but it also has a series of components that we could use in order to generate front-end code laravel uses something which is called mix and mix is a webpack-based front-end build system which allows you to run stuff like bauble css javascript and much more now inside the root directory there's a file called webpackmix.js so let's open it up and this is basically a file that you could copy from project to project right now we don't have that many files that we're compiling but just keep in mind that there is an unimaginable number of configuration properties that you could use right here right here you can see that we're linking our resources javascript directory to our public javascript folder so we want to compile it from the left into the right and the same thing happens for our post css 2 on the line below right here we need to have our app.css or scss inside our resources directory forward slash css forward slash app.css and we want to compile it to our public directory whereas a folder called css instead of compiling css we could also compile scss like i just said and that's what i want to focus on right now because the process is exactly the same so what we need to do is to change our variable of post css to sas and let's change the path to resources forward slash scss forward slash app dot sdss all right now how does the folder structure work well the power of mix is its directory structure if you create a php project you constantly need to decide where the source and compiled assets will be with mix we could just stick to their convention and never think about it again now if we take a look at that directory you can see that every laravel application has its own resources folder right here so if we open it you can see a css js a lang and views directory right now we change the directory name from css to scss so inside our resources directory let's create a new folder called scss and insider scss let's create a new file called app.scss let's save it we're not ready to compile yet but whenever we do the files inside our resources directory will be exported into the public directory where there will be a css and javascript folder like i said before mix runs on webpack and in order to run webpack we need to set up a few tools first things first we need to have node.js installed and you might have it or you just don't know it anymore so let's check it out let's hop to itune and if you want to see if you have node installed or not you only need to run node space v so let's hit enter right now i'm running on version 12.4.0 if you don't have it installed you need to run brill install note if you're on a mac if you're on linux you need to write down sudo space apps get space install space dash y space node.js or if you're on windows i will put the link in the description where you could download note and if you've done that come back to your terminal and just write down node space v to see if it worked or not whenever you install a node something called mpm will come with it and that's what we need in order to compile our assets a good thing to remember is that you don't need to install node for every project if you install node once you can use it every single time alright what do we need to do next well in our terminal we need to run a command called npm install let's hit enter and this might take a second because we're requiring all necessary packages all right before we make it work we need to perform one more command let's write down npm run def to run our webpack once so let's hit enter this might take a second as well when it's done you need to run it one more time for it to work and right now you can see that we have compiled two files successfully the first one is the css file and the second one is our javascript file we haven't done anything in particular to compile so let's fix that first so let's hop to our style sheet inside the resources directory and let's create a variable called primary color and let's set the value equal to red so let's say ff000 now let's style the body and let's set our background dash color equal to our variable so let's say primary dash color again what we need to do next is to hop to our front end so our stylesheet so we need to link our stylesheet so let's go inside our layouts directory let's open f.blade.php let's get rid of the style that we have right here because we don't need it let's create a new link with the href to asset parentheses css forward slash app.css and let's see where the file is all right let's open our public you can see that we have a css directory right here that we didn't have before we compiled let's open it and we have a app.css which is empty right now but won't be in a second if we save it and hop to the front end refresh it nothing is happening the background color has not been changed to red now with the npm run death command that we just performed we're basically compiling it once there's another command that i like to use and that's npm run watch so let's do that let's hit enter all right larva mix compiled successfully so let's go back to google chrome let's refresh the page and you can see that our background is red but if we hop back to our terminal you can see that our npm is still running so if we remove the background color inside our visual studio code more stylesheet in the body let's save it you can see that laurel mix builds it automatically let's hop back to google chrome refresh the page and you can see that our background color is not red anymore let's hop to the project with download for my github and if you haven't i have the link in the description where you could download the files i will just go to my github right in the browser let me open repo let's open the style sheet and let's just copy everything in the style sheet all right this takes a while okay let's copy it and let's place all of our css inside our app.css all right let's save it and you can see that we're getting a lot of our mix error right now so let's see what's going on right here let me go to i term you can see that a module is not found because it can't resolve our image i wanted to show you what will happen if you get an error message so what we need to do is to change our path because we don't have our image directory so let's hop to visual studio code let's command f or ctrl f let's write down backgrounds and let's search for the background image all right as you could see we linked our storage directory so if we open our storage directory you can see an image dash banner which is equal to the image that we need right here but the folder name is different so let's write down storage instead of image but right now it will look in the public directory and that's not what we want so we need to add a forward slash right in front of storage let's save it and you can see that laravel mix has been built successfully now let's hop back to google chrome refresh the browser and you can see that our css has been compiled all right the last thing that i would like to show you is all of the front end presets that we could pull in you don't need to do this because it will mess up what we just built and we don't need to do it for now but it's good to know that what we're going to do is easily done with laravel most people like to work with react or tailwind or bootstrap instead of using the link tag inside your header you can pull them in as a front-end preset from 3rd party presets in github and the commands are very easy so let's hop to our terminal let's open a new tab let's go right inside of our desktop so see the desktop workspace first project and the command that we need to perform is very easy let's write down php artisan preset percent followed with the framework that you would like to believe if you like react write down react if you like to work with bootstrap just write down bootstrap now what i'd like to do is to use tailwind for now what i want to do is to show you a github repository that i prefer to use as well so let's hop to google chrome let's go to a new tab and let's write down laravel front end presets all right we need the first github repository so let's open it and right here you can see all the front-end presets that we could use what i want to do is to pull in tailwind so let's open it and let's scroll down a little bit because right here you can see how we could use tailwind so first off let's copy this command right here let's go to our i term let's paste it right here hit enter and this might take a second so no worries all right it has pulled in all tailwind stuff that we need but before we continue on we need to run php artisan ui tailwind that's it enter oh excuse me it's talwin css my bad let's hit enter this might take a second as well all right you can see that our tail when css scaffolding has been installed successfully but please run another command so we need to do the same exact thing as what we did with npm so we need to npm run dev so let's do that this might take a second as well and as you can see our larval mix build has been done successfully so what i want to do is to hop to visual studio code let's open the app.css as you can see right here our entire app.css file has been filled in with tailwind stuff that we could use and honestly tailwind is not something that i want to focus on right now but since css languages and frameworks are growing rapidly i thought it'd be good to show you what it actually is and how you could easily pull it in our main goal is to basically put data inside our pull it in the code and do something with it and for that laravel comes with a very very cool tool called eloquent besides the power of eloquent it's also one of the most important features when working with databases and in my opinion this is where you see the actual difference with php or any other php framework eloquent is an orm our ram stands for object relational mapper this might sound pretty scary or difficult but you will get used to it very soon orm basically allows you to write queries using the object oriented paradigm so not in a programming language but more using laravel's language of choice instead of sql before we hop right into writing queries and other stuff i want to show you how the database credentials are stored in our root directory we have a folder called convict so let's click on it and right here you see a lot of files that we might need later on but for now let's just open the database.php file we also need to open our dot emv file so let's do that as well right here you can find your database variables that are referring to the variables inside the database.php file as you can see our d file refers to our environment variable called db connection and it's mysql so if you want to change it or use another database provider change it right here but we will be using mysql for this course so let's keep it like this there are lots of other drivers that you could use we got sql lite right here we have mysql of course we have pg sql sql srv which i actually never worked with but keep in mind that you could also edit or delete these named connections or create your own a good thing to know is that you could also have more than one connection you can make up to five database connections and there are a few reasons why let's say that you have multiple types of data right or you might reach from one and right to another that's when it's very handy to use two drivers so let's create our own connection in our emv file let's fill in the fields that we have like i said our database driver is mysql our database host is well for me it has to be my localhost so play around with it the database port is all right the database name well we haven't created yet but we will do it in a second the database username is root and mydatabase password is dari1234 if you don't have a password just keep it empty all right what we need to do now is to hop to the terminal and right here let's go into my sql and to do that let's write down mysql dash u root dash b it will ask for a password and my password is darby one two three four like i just said let's hit enter and right now we're in our mysql so what we need to do first is to create our database right so let's say create database called first project let's hit enter and you can see that one row has been affected now to see if it works let's write down show databases and as you can see right here first project has been created and the way i prefer to work is to not close off my sql but just to open a new tab and let's go right into our desktop workspace first projects so we don't need to fill in our password every single time we want to go into mysql you might think that it's very difficult to set up a table with the right data types and stuff in the command line which you probably have done with phpmyadmin databases in laravel makes it a lot easier as well what we need is something called a migration which is a new term in this course and you can see a migration as the version control for your database so how in heck do we create a migration you can run it right inside of the command line but before we do that we need to set up our controller and our model so first let's create our controller so let's say php artisan make me a controller called posts controller now what we need to do is to go to our controller let's close off the emv and database file because we don't need it all right let's open the app http controllers host controller and here let's create a new function called public function index we want to do is to return a view with a path to post forward slash index all right let's create that specific view now alright so let's open resources views let's create a new folder called posts let's create a new file in there called index.blade.php and let's write down posts for a second what's next well we need to create our route so let's open routes web.php and i hope you're getting a little bit more familiar with it so let's add a new use well we could actually copy it because it's in the same directory and let's change the pages controller to posts controller now let's create our endpoint so posts endpoints which has a route get request to forward slash posts comma pass in the post controller class comma and the method is index alright let's save it let's save my in excel blade as well and my post controller to be sure alright that's up to google chrome let's change the endpoint to forward slash posts and post has been printed out alright are we done or do we need something else well if you're familiar with mvc we need to have a model right in order to interact with the database so let's create a model as well let's hop to item again let's write down php artisan make me a model called post and it needs to be singular let's hit enter our model has been created successfully and like i've said before models are stored right inside of our app directory but instead of going to controllers we have a folder called models so let's open post model what we want to do right now is to create tables and therefore we need to add migrations now there are two ways how we could do this the way i don't prefer is to use the migration command so let's hop to i term and inside our terminal is to basically say php artisan make me immigration called create posts table so we will create a migration with the name create post table and honestly this is not the best way to create a migration what you want to do is to automatically create one when we create our model because that's where a model is used for right to interact with our migrations so let's hop to visual studio code and let's delete our post model we don't need it let's hop back to item and let's get rid of the command as well let me zoom in alright and in here let's create our new model and our migration so let's say php artisan make me a model called post space let's add a dash m flag which will automatically create a migration for us let's run it alright you can see that the migration and model have been created successfully now your next question might be well dari where are my migrations stored so let's open visual studio code let me actually close off the routes and the views because we don't need it and as you can see we have a database folder so let's open it and you can see your migrations folder so let's click on it and let me actually make this a little bit bigger because you can see that we have more than one migration and we only have created one well laravel comes with a default migration of a login system on the register system create password password preset and so on this is something that we will cover later on now what we do see is the create posts table right here so let's open it and let me make it smaller again because we don't need the rest right here you see our migration well we have a up method and a down method now how this works is whenever you run the migrate command it will look for the earliest date of your migration as you can see right here and it will run the up method like it says you're migrating it up at this point and a very very cool thing is that you could also roll back your migration so you don't need to delete it inside your my sequel manually but you could do it inside the down method which you will see because it has a drop if exists and it will drop the table posts little recap the up method migrates everything inside of it and the down method will undo everything that has been migrated right now we're actually ready to create our first table whether in a create table call or a modified table call it will always use the instance of blueprint that's passed into the closure of the schema if we click true on blueprint you can see all the various methods available for creating columns and you don't need to learn them right now because it will come during this course you can create integers string binaries you can build stuff you can add fluid indexes and way more we're ready to create our first table let me open my migration again and what we don't need is our id and timestamp because i want to create everything from scratch so let's delete it what do we need when we want to create a post well we have our table variable which has a method called increments in it now inside the parentheses we need to write down the table name so in our case it's id we could pass in an integer right here instead of increments but then we need to manually increase it every single time so why should we not use our increment method that we have so what's next well every post has a tape every post has a title so what's next well every post has its own title so what we could do is to say table which is a string and the name is title so the column name is title the data type is string we have a body so table medium text and let's give it a name of body let's also create a timestamp to know when it has been created so let's say table timestamps once you have your migration defined which i have right now we are actually ready to run it it will not change it automatically when you save the file like i'm doing right now because we need to run a very easy command let's hop to item and let's write down php artisan migrate just as easy as that let's hit enter and you can see that it calls the up method of all the migrations that we had every single time you run this specific command it will check whether you're ready to run all available migrations so if there are no typos or whatsoever in your up methods and as you can see it has migrated the post table that we just created so let's hop to mysql in here let's say show tables oh let's first say use first project excuse me all right then let's say show tables and you can see that we have a users table post table that we need password reset migrations and failed jobs don't look at the others for now what we need to do is to say desk so descending our table posts let's hit enter all right and you can see that we have a column id title body created at and updated at now there are a couple other commands related to migrations that i would like to show you the first migration is well let me go to another tab php artisan migrate colon install and what this will do is keep track of which migrations you have or haven't run let's hit enter right now it won't work because it's pretty much the same as the php artist in migrate command because they will be run automatically behind the scenes when we migrate the second command is the migrate column reset which we'll call the down method inside our migration so if you run it and go to my sql try to desk our posts again you can see that our table post does not exist anymore because we have rolled it back we also have php artisan migrate refresh and this will roll back every database migration you've run on this instance and then runs every migration available and this is basically the same as the reset but it will also migrate it so right now if you hop to my sql and say deskpost again you can see that it reseted it and then it runs our migrations again there are three more commands but honestly we won't be using them a lot but i will just tell you what they do but i won't perform the commands we have the php artisan migrate fresh which will drop all of your tables and run every migration available it's the same as a refresh but this won't use a down migration it will just delete the tables and then runs the up migration again we have the rollback which will well basically roll it back and the last one is the status and we could actually run this one because it will just show a table listing of every migration whether or not it has been run yet in this environment now that we have set up our database connection and went over the basics we need to input data inside our database and if we hop to mysql we can do this by writing down our own queries inside the command line so let's say that we want to insert into posts the tables title and body and the values are this is my first post and the value of the body is body of the first post since we have set our id to auto increment we don't need to increase it automatically inside the query let's hit enter you can see that one row hasn't affected let's write down select all from posts and you can see that we have one row inside our table right now and don't look at the created that and updated that that will come later on my issue is that this took way too long and this is for only one post what we could do is to use our model factories which is basically a pattern for creating fake entries for our database tables so it's just adding dummy data inside our tables based on the data types that we've passed in insider migration by default every factory that we have is named after an eloquent class but you could also name them after the table if you're not working with eloquent so let's hop to the code because i want to show you where our factories are stored so right inside our database folder we have a factories folder so let's open it by default you could see one factory which is the user factory so let's open it right here you can see that there is a default user factory class which extends the main factory class so let's click true on the factory class let's see what's going on over here in the factory request you can find a lot of properties and static factory methods patterns that we could use let's scroll down because right here you can see a factory instance of a given number we could create a new factory instance we could get the raw attributes and way more things that we could do let's see well if i scroll down we have more than 700 lines and since i can't cover them all i just want you to take a minute and scroll through them to see the power of factories and if you're done let's continue on with this video by going back to our users factory all right let me open it again we have a function where we're defining our user model factory we're returning an array right here with a couple of fields and this is an associative array with the name of name email email verified add password and remember token and these are not randomly added right here since we want to insert dummy data inside the database the keys of this array need to be the same as the column names so what we can do is to go to i term let's desk users and right here you can see the id name email email verified ad password and the other stuff so if you go to the visual studio code we're basically adding everything that's right here inside the database now the values that we are trying to set so this faker name is faker unique are created with a faker property that we have which is the faker php library that allows us to generate random data for testing what i would like to do right now is to generate a new factory class so let's hop to the command line let me open a new tab and right here i want to use a command called php artisan and we want to make something again which is a factory called post factory all right let's hit enter our factory has been created let's hop to visual studio code and let's open our post factory which has the same structure as our user factory we're pulling in the post model and we're creating a property called model which is equal to the class of post but let's say that you don't want to use this specific model that we have so the post model you don't want to change it manually by setting it equal to let's say user for that we could use another command but before we perform it let's remove this file for a second now that's up to the terminal once again and in here let's hit the arrow up and let's add a flag to it of double dash model is equal to post we could also set it equal to user but for now but let's just set it equal to post because that's what we need alright let's save it let's hop back to visual studio code again we have our post factory again let's keep it easy for ourselves let's go to the users factory and last copy the entire return array now let's replace it inside of our post factory alright we don't need our remember token for a post and we also don't need our password all right let's change the name to the title let's set it equal to this faker but not the name but title let's change the email to body and let's get rid of unique save email and let's set it equal to a paragraph what do we have next we could also add the created underscore at which is equal to now but with the release of laravel 8 we need to change a couple things right here we need to use another import called illuminate backslash support backslash sdr and instead of saying post call and column class we need to write down backslash app backslash models backslash so we need to write down the entire path to the post model and don't forget to add the first backslash in front of app otherwise it won't start at the root directory and that's what we need now i want to use php artisan thinker right now and i won't go in depth about it because i want to make a complete video about it later on but for now just follow along we could also use seeding but that's not what i prefer to use because i think php artisan thinker is way stronger so let's save it let's hop to our command line let's write down php artisan thinker and this will basically redirect us to a shell where we could perform a lot of stuff what we could do right here is to set our entire path to the model so app backslash models backslash post then double colon because we want to call the factory global helper so that's right down factory and then we want to create a new row so pointer create let's hit enter and right now you can see that we created one row with dummy data right here we have a title we have a body that created it updated at and we have our auto increment id we could also add more than one row so let's hit the arrow up again and right after factory we could call another method which has the name of count and inside the count parentheses we need to specify the amount of rows that we want to add so let's say two hit enter and we created two more posts before i wrap up the video let's go to mysql and in here let's say select all from posts and you can see that we have added three new rows inside our posts table just as easy as that what i want to do right now is to put in our dummy data by writing down queries at the core of every laravel database there is a functionality called the query builder which is a fluent interface for interacting with several different types of databases with a single clear api let's hop to the code editor and right inside of our post controller let me show you what a non-fluent query is so let's say none fluent db let's select from our table called posts where so we want where clause our id is equal to one and this looks good if you're new but it's not beneficial you want to use a fluent interface because it primarily uses method chaining to provide a simpler api to the end user so what you want to do is to say fluent we went to db the table called posts then we want to say where the id is equal to one and then we want to get everything i want to start off very basic with raw sql and build from there let's get rid of our return value and let's also get rid of our comments so what empty function there are lots of different ways how you could handle what we're about to do you can return it directly you could create a variable or you can just dump it on the screen and that's actually what i prefer so let's create a variable posts and let's set it equal to db because we want to make a raw call to the database let's write down colin collin and as you can see a drop down appears with all the options that we could use there are some specific methods for various actions so we can delete insert select table transaction and way more right now let's just use the select method now inside the select method we can perform queries so in our case let's say that we want to select everything from the table posts right below our variable posts we can dd so die dump and we need to pass in variable posts now if we save it this actually won't work if you go to the output if we refresh it you can see that our class db has not been found so what we need to do is to hop back to visual studio code and right below our first use let's create a new one of illuminate backslash support backslash vacates backslash db so we need to pull in our db that we're using right here let's save it let's head back to chrome refresh the browser and you can see that we've got an array back with three items inside of it post number one post number two and post number three be aware that this will return an std class object later on you will understand what this is most of the times you want to use a select statement but you usually want a where clause inside of your query so something like this we want to select everything from posts where the id is equal to let's say seven save it refresh the browser we get one item back so our id which is equal to seven but since laravel's database architecture allows for the use of pdo params you are very safe for potential sql attacks so we don't want to pass in our id number 7 like this what we want to do is to set it equal to a question mark and then after a query we could add an array which is equal to the value of the question mark so let's say 7 save it refresh the browser get one item back which is id 7. now what we did right here is fine because we're using prepared statements but if you're like me and you don't like the question mark but if you're like me and you don't like to use the question mark you could also use name params so let's say that instead of the question mark we want to pass in colon id and inside our array we need to create an associative array where our id is equal to 7. save it refresh the browser and the output is still the same now what we've done so far had nothing to do with query builders but it was me showing you that you could use your own queries just like what we did what i want to do right now is start off creating our own queries with the query builder because it makes it possible to chain methods together to build a query so right above our post let's create an id and let's just set it equal to seven let's get rid of everything after the db column column and what we want to do is to build a query from the table so let's say table which is a method the table that we want is called posts now after the table method let's use another method called where so let's say we want to have a where clause we can add it right after the table but we could also add it on the line below and that's actually what i prefer since it's easier to read so what do we want to see well we want to check where our id is id and what we want to do next is to get everything and this is called a chain at the end of our chain we want to trigger the actual execution of the query that we just built by using the get method right here let's save it let's hop to chrome refresh it and we have a collection let's open it with id number seven it is also possible to get a specific column so right after our table method again let's get rid of everything and let's say that we want to select only the body and we want to get it all and this will return the body of every single row that we have in our database so we have three items back the first one is the body of number one body of number two body of number three so let's say that we want and the where method takes three params the first one is the column so let's say created underscore at comma the second one is the comparison operator so we want to see if created underscore at is greater comma and you guessed it the last param is the value now sub day before we save it it makes no sense to add the last param of now if your goal is to see if something is equal to alright let's add the get again save it hop back to chrome refresh it and we have zero items back because our created diet is not greater than now but if we say less than we're getting back all of our elements all right there's also an or where method so right below our where let's say or where and this is basically an or statement so if the where method isn't true it will check if the or where is true and still prints something out so let's say that title comma is prof point change the title right here to a title that you have in your database because it might not be the same for us let's change the where clause to greater than let's save it let's refresh the browser and you can see that we have two items the first one has a title of prof and the second one as well another cool method that we could use right here is the where between method so let's get rid of the or where andy where so let's say where between and this method allows you to scope a query to return only rows where a column is between two values and honestly we don't have the best data for this but since we have an id let's just test it out with the id right after our id let's add a comma because we need to add a second param of an array with two numbers so let's say seven comma nine so we want to see if the id is between seven and nine let's save it refresh the browser and we have three elements which is true because we have id seven eight and nine the where between method includes two values that we have put inside the array why are seven and nine here well the where method uses the two numbers that we have included inside our array so 7 and 9 are included as well and if we change it to 1 f5 refresh it we get nothing back from the database we could also select rows where a given column is no or it's not null right now we don't have empty records in our database but we could see if a value is not null so let's say where not no the title let's save it refresh the browser and since our titles are not empty so let's see they are still printed out there's also a where raw method which allows you to pass in a raw unescaped string and i actually don't prefer using this because sql queries will not be escaped and this is the right opportunity for hackers to attack your application so let's just skip this one another cool another pretty cool method is distinct so let's get rid of the where raw what this thing basically does is selecting rows where the selected data is unique now in order to do this we need to select because we want to select a specific column so let's say title on the line below let's add distinct right now we do have duplicates in our database let's go to i term you can see that we have two titles with the name prof so we should be getting back to rows let me save it let's go to chrome refresh the browser and you can see that we have two rows with two different titles there are also a couple methods to modify data such as order by group by skip take latest and in random order so let's cover them all right now the first one is order by so let's get rid of distance and select and let's say order by the title and i want it to be ascending this could also be descending but for now let's just keep it ascending save it refresh the browser open our array you can see that the m comes before the letter p the skip and take are two methods that i want to skip for now because later on i want to show you how we could easily create pagination these two methods allow you to define how many rows to return and how many to skip before starting the return we can also sort our database based on the created add column and that's very easy so what we need to do is to get rid of the order by and let's just write down the latest let's save it let's go to chrome if we refresh it and open our array you can see that it's based on the created add and descending order 2155 is the latest for the second one as well the third one is 2154. we can also order them in an ascending order but we don't need to add it as a param we just need to change the latest to oldest save it refresh the browser let's open it one more time and you can see that 2154 is the first one and the second one is 21.55 we can order them in a random order so instead of saying oldest let's say in random order save it refresh the browser now let's check the order id 7 8 9 well this doesn't make sense so let's refresh it one more time ideas 8 7 and 9. besides the get method that we're using right here there are a couple other returning methods if you don't add a returning method you will always return an instance of the query builder so i want to cover a couple other returning methods now let's get rid of everything and let's say order by created underscore at comma descending instead of saying get everything let's say get me the first one save it refresh the browser and the first one is id number eight and you can see that there is a limit of one added to it the issue that you could cover with the first method is that you will get an error message if there are no results we can use a method that will find a row with a specific id for us so let's go to visual studio code let's get rid of the order by let's replace first with find let's pass in the id that we have right there save it refresh the browser and we found a specific id but if we change it to well let's say id 10. refresh the browser we get a null back because it does not exist we could get the count of the matching results so let's say where our id is equal to id and instead of saying find we want to return the count save it refresh the browser and the output is one because our ids are unique and if we return the where method we're basically selecting every single row that we have in our database so the return value is 3. another cool method is the minimum and maximum value of in the column so instead of saying count let's say the minimum is from the table id and you can see that it's seven which is true because we have seven eight and nine and the maximum needs to be nine so let's test it out and you can see that the output is nine and the last two return methods are the sum and average so let's just test it out with our id let's say that the sum of our id is 24 so we will add up every id that we have and the average save it refresh it is 8. pretty important thing is inserting data in the database what we need to do is to get rid of the average and let's say we want to insert something inside of our database inside our method let's add a set of brackets and let's hit enter because we want to pass it in as an array so what do we want to pass in well we want the title of new post comma we have a body of new body let's save it let's go to chrome refresh the browser and you can see that the output is true which is good because this means that it worked so to test it out let's go to i term let's say select everything from posts and you can see that we added a new post inside of our database since we have inserted a new world with the id of number 15 let's update the title because that's also possible so let's go to visual studio code let's go right after our table because we need to have a where clause so we want to update it where our id is equal to 15. now we don't want to insert so let's get rid of that as well what we want to do is to update and once again we need to add a set of brackets and we want to set our title to new title and let's also change the body so the body is updated body all right let's save it chrome refresh it the output is one so this is a boolean so it's either true or false let's go to i term hit the arrow up so we want to select everything from posts again and you can see that the body has been changed to updated body and to complete our crud functionality let's perform a delete query so let's go to visual studio code we can keep the where clause but we need to get rid of the update so what we need to do is to delete so let's write that down now a cool thing about the delete method is that we don't need to pass in anything inside our parentheses because it will delete it based on the where method so let's save it let's go to chrome refresh the browser output is still one i term hit the arrow up and you can see that our id 15 has been disappeared this was it for this video about query builders if you do like my videos and you want to see more or you just want to support the channel just click on the subscribe button down below and don't forget to like the video eloquent is an orm and like i said before orm is an object relational mapper which also means that it's a database abstraction layer that provides a single interface to interact with multiple database types a single eloquent class is responsible for not only providing the ability to interact with the table as a wool but also representing an individual table row and this might sound pretty difficult to keep it simple eloquent has one primary focus and that simplicity keep that in mind whenever someone asks you why eloquent just respond with one simple word simplicity i can scream that it's simple but why just like the rest of blauraval's framework it relies on convention over configuration meaning that it tries to build something with minimal code i have created a new project called cars and i want you to do the same i want to retake a couple steps that we've done so far so let's hop to the cli and what i want to do is to require tailwind for our ui so let's say composer require laravel dash front and dash presets forward slash tailwind css space double dash dev hit enter this might take a second so i will pause the video so let's install npm so npm install and npm run dev this might take a second as well so what's next we need a controller a model and a migration we could perform them all in one single command but that's not what i prefer i usually make my model and migration in one command and my controller and another so let's say php artisan make me a model called car dash m so that's migration flag and you can see that our model and migration have been created so let's create a controller let's say php artisan make me a controller called cars controller and i want to add a pretty cool flag that we haven't talked about and that's the resource flag so let's say double dash resource whenever you want to create your parts functions you need to create them manually right you need to create a method to show the cars to update the cars to delete the cars and so on so what we could do is to create them automatically let's hit enter you can see that the controller has been created let's hop to visual studio code and let's open our controller all right and as you could see we have our index create store show edit update and destroy methods in most cases laravel resource routing assigns the correct routes to a controller now what we do need to do is to create a route so let's do that let's open routes web.php without the resource slack we use the route like this right let's get rid of it what we could do right here is to basically say route again double column and instead of saying get or something else we could say resource the idea behind it is exactly the same what is our first endpoint well forward slash comma what's our controller cars controller double column class now let's pull in our cars controller let's say use app backslash http backslash controllers backslash cars controller let's save it let's hop to item because i want to show you something pretty cool let's write down php artisan route column list and what this basically is is all our endpoints for the methods that we have in our controller we have an index store endpoint create endpoint show update destroy and edit so what we've done inside our web.php is basically saying well our first endpoint is the forward slash and then we want to pull in and then we want to pull in all the methods that we have in our cars controller so let's go to our cars controller let's scroll up to the index method let's return a view to index you see the difference we don't need to define every single route in our web.php file instead we've defined them in one single command so right now let's open our views so resources views let's delete the welcome page because we don't need it let's create a new folder in here called cars and inside our cars let's create a new file called index.blade.php in our indexed blitz let's just write down cars oh yeah and obviously we need to change our endpoint to cars so forward slash cars the forward slash was just to show you what the first endpoint could be let's go to chrome refresh it and cars have been printed out alright i want to create a ui where we could perform eloquent stuff so for that i obviously need a ui but i don't want to create anything fancy at the end of this course i will add some projects with a way better ui but for now let's hop to our visual studio code let's create a new directory inside views called layouts oh my bad let me rename it for a second now let's add a app.blade.php where we will place our core html so let's write down doc all right let's create a section inside our body so add yield with the name of content let's pull in our tailwind css that we have inside our public directory css app.css all right let me close that off actually because we don't need it let's go right below our meta let's say link the href is the asset method which goes to our public directory so css voiceless css save it and since we have pulled in our tailwind stuff let's change the background color of our body let's set it equal to a class bg dash gradient dash 2-r from this gray 100 to cray 200 and this won't work for now because we need to go inside our index plate let's get rid of cars let's extend the layout page so layouts.app let's save it let's go to chrome refresh the page and i'm not sure if you can see it but the background color has changed a little bit so right now we're able to define our section so let's say add section and section the name of our section is content inside our content we need a diff another div and an h1 and the text of our h1 is cars give it a class of text 5 xl uppercase and bold and i won't go in depth about what everything means tailwind is very straightforward and i might create a course later on so the div that's wrapped around our h1 has a class of text center and the entire div has a clash of margin dash auto so m auto width is 80 so 4 5 and the padding y-axis is 24. save it go to chrome refresh it and this looks alright now let's create a content that will eventually be inside the loop whenever we pull stuff out of our database so let's go to visual studio code right below our div let's say another div with a class of width 5 6 padding y is 10 and we have another div inside of it with a margin of auto so m-auto so i want the year that the car is founded the name of the car and i want a paragraph so let's create that let's say span found it let's say 2020 the span has a class of uppercase the text is text dash blue dash 500. font bold text is extra small and it's italic save it go to chrome let's actually wrap this inside the div with the margin auto as well so let's copy this one let's face it right here align it a little bit so what do we need next well right below our span i want an h2 with the name audi it has a class of text that's gray-700 and the text is 5 xl chrome refresh it all right right below my h2 i want a paragraph and let's create some lorem ipsum text so let's write it down with lorem hit tab let's give our paragraph a class text lg text as gray dash 700. now right below our paragraph let's create a hr so a line with a class of empty four margin top four margin bottom eight save it refresh it now let's actually create a little bit more spacing around our paragraph so let's say py is 6. all right this looks good whenever you work with eloquent you want to interact with the database so let's set up our database by creating our first migration so let's go to the code editor let's open database migrations and it's the last one create cars table what i want to do is to change my id to increments and i want to give the name of id right below my id let's create a new table which is a string with the name name i want the year when the car is founded so let's say table table integer found it and i want the description so table long text which is the description let's save it but before we migrate our database let's open the emv file let's check our database for dash tools my host is localhost my port is all right we will create our database username is root and my password is dory1234 this is the date that i need for now so let's go back to the command line because we need to go inside my sql and i already have it open what we could do is to create a database so create database cars one row has been affected and like before don't close off my sequel because we need it later on as well let's go back to the other tab because now we're ready to migrate let's say php artisan migrate and we have a connection issue so i probably haven't saved my emv file all right let's run it again yes and it has been migrated as you could see so what do we need to do next well we need dummy data inside our database so let's go back to my sql let's create an insert query so we want to insert into cars the columns name founded and the description and the values are let's say audi it's founded in 1909 and let's give it a description of this is the description for audi let's close it off with a semicolon let's hit enter and one world has been added let's change the second one to mercedes and the value of the name two and mercedes has been found in 1926 and the last one is let's say bmw bmw is found in 1916 and the name is bmw as well alright so that's desk cars again all right let's select everything from cars i forgot from all right we have three rows right now that we could use so my goal is to basically show you all eloquent models in order to retrieve update delete and insert data but before we do that let me show you a couple things that we could add inside our model so let's hop to our car model let's open it if you took notice we haven't defined anywhere inside our code that the car model needs to interact with the card table in our database that's because the default behavior for table names in laravel is snake cases and it pluralizes your class name there is obviously a way to manipulate this and to give it a different name and to do that we need to create a new property so protected table and we need to set it equal to a string which will overwrite the defaults table called cars we don't need to do it so what i want to do is to set it equal to cars just as it is we could also manipulate the primary key so if we hop back to my sequel and let's say desk cars you can see that the id is indeed the primary key of our table so we can change that by basically creating a new property so protect it called primary key and we can set it equal to a new table so we could say that our name is our primary key which makes no sense but we could do it now there might be a case where you just don't want your primary key in that case you can set the value of your primary key equal to false but what i want to do is to just set it equal to id just as it is so if we hop back to iter you can see that we have a updated add and created ad well if you don't want to use it you could create a new property called timestamps and you just need to set it equal to false but if you want to use it so let's say true you have the opportunity to customize the format and that can be done by saying protected date format is equal to the same as the php date syntax so let's say hours column minutes column seconds and once again these are all optional you can either remove them right now or keep it i will remove the date format because i don't want that now that we've talked about the model we're ready to talk about how we could retrieve data with eloquent and you can see this as a select query in my sql so whenever you want to retrieve all blog posts and print them out or you want to retrieve a user by checking the user's credentials or what we're going to do is basically retrieving all cards that we have in our database and send it back to a view this is something you will be doing most of the time you want to pull data from your database using the static call on your eloquent model so how do we get started let's open our controller let me actually close off everything because we don't need it yeah and we need our cars controller now the first thing that we need to do is to pull in our car model so at the top of our screen let's use app backslash models backslash car so a single model now what's next well we want to perform something like this right we want to basically say select everything from the table cars in eloquent we can create a variable let's say variable cars and let's set it equal to the model that we have in our case it's car followed by double collin and we want to get everything so we need to use a function called all before we continue let's die dump our wearable cars to see what the output is let's save it google chrome refresh the browser and we have an array let's open it and we have all cars right here now let's hop back to the code editor honestly it's not necessary to create a variable and set it equal to our eloquent model we could also pass the eloquent model right into the view but i think this looks a little bit cleaner than doing that so what i want to do is to add a comma right after our index we want to pass in an associative array cars pointer variable cars let's get rid of the dd and we don't need to select the reason why i'm passing it in as an array is because i want to loop over it in our ui so let's do that let's go to our views so our index.blade let's actually copy the entire div with the margin auto let's remove it let's go right inside of the div let's create it for each loop and that's for each over the cars that we have as a single car so let's paste in whatever we just copied all right let me align it and what i want to do is to replace 2020 with the variable car and i want to pick out found it as to the same thing for audi let's say car name and for the paragraph as well variable car i want to grab the description save it go back to chrome refresh it and apparently our die dump is still there so let's actually save this file and you can see that our audi mercedes and bmw has been printed out obviously there's also a way to filter through all cars so as you can see we have our variable cars which is equal to all cars but instead of saying all let's get rid of it and let's use the aware method so what do we want to achieve we want to see if there is a value in the column name that's equal to let's say audi and then we want to get all cars so let's go on the line below and let's say get let's save it let's hop to chrome refresh it and only audi has been printed out now this is how you get a specific value right now we're writing down audi right here let's say that a user wants to log in well you can grab their id and use it right here or whenever a user clicks on a specific product your endpoint will change and you can use that specific endpoint right here you may have noticed that everything that we're doing is basically the same as laravel's query builder and that is definitely true but you could do a lot more stuff so keep in mind that everything we did with the query builder on the db fake 8 is also possible with eloquent objects as you could see we're using the get method right here which is exactly the same as a normal query builder call we're building a query and then we're calling the get method to get the results whenever you want to process a large amount of records so actually not what we're doing right now there might be a possibility that you run into memory or locking issues and since laravel is so great to use there is a method which allows you to break your request into smaller pieces so it will chunk it down so on the line below let's create a new variable cars and let's set it equal to car common column chunk the first param is the amount of rows where you want to chunk so let's say two comma function parenthesis curly braces inside our parentheses let's pass in variable cars and let's loop to route all of our cars so let's say cars as a single car print underscore r that car save it hop back to chrome refresh it you won't see any difference right here the query has been split into multiple queries based on the number that we have passed in right now it's two so we have four rows in our database then we're performing two queries of two rows what we've done so far was printing out stuff that has been found now there might be a chance where there is no model found and you just want to throw an exception for that we could use the find or fail and first or fail method it will retrieve the first result of the query let's get rid of our second cars variable we do need our where method but instead of saying get let's just say first or fail it will retrieve the first result of the query and since we have set it equal to audi everything will be fine so let's go to chrome and we're getting an error right now because we need to change something on our front end since we're retrieving only one single row so let's go to visual studio code the index let's actually delete the variables that we have right above our div and let's just print out variable cars save it refresh the browser and you can see that we have one single row where the name is equal to audi and we could also print out the name by saying pointer name which is equal to audi but what if we change audi insider where clause to something that we don't have so let's say tesla save it refresh the browser and we're getting a 404 because it does not exist there are a couple aggregates that we could use in eloquent as well so let's go in online below now let's say print underscore r a car where the name is equal to audi and return the count and cars with one r excuse me and let's comment out this one refresh the browser and you can see that the return value is one because we have one match instead of counting the match that we have we could get rid of the aware clause and say all so we want the count of all the cards that we have refresh the browser and the output is three we can print out the sum so let's get rid of everything after the double column let's say sum and well let's say they've found it which is an integer and we need to add an extra set of parentheses save it chrome refresh it and the sum of three integers is 5751 and we can print out the average so let's say avg instead of sum refresh it and the sum is 1917. now that we're done with retrieving data i want to show you how we could insert data and this is where you will see a difference from normal query builders and eloquent so let's hop to the code editor let's get rid of the average let's undo our comment let's just return all cars and now we're done with the index so we can proceed with the create method so inside the create method let's return a view to cars.create we basically need a form which will allow us to insert data into input fields click on the submit button and do something with it inside the store method so what's next well let's actually create our view so insert our cars let's create a new file create dot blade dot php now let's write down cars let's actually drag our index inside cars as well let's move it let's change our index view to cars.index and let's change the endpoint as well so inside the web.php let's change this to forward slash cars all right save it that's how to google chrome i'll change the endpoint to forward slash cars all cars are printed out let's add a forward slash create after it and we have cars printed out on the screen so what's next what we want to do is to add a create button somewhere here let's say right here first we need to hop to our index page again let's scroll up and let's go right below our class margin auto with eighty percent and padding on the y axis let's create a new div give it a class of padding top 10. let's create an answer in here with the text add a new car and i want a symbol of at r let's change the href to cars forward slash create and let's give our button a class so let's say class is equal to border b2 padding bottom is 2 border dash dotted italic text as gray dash save it refresh the browser and we have a button right here to add a new car if we click on it you can see that our endpoints change to cars for slash create so what do we need to do in our create well we could basically copy everything inside of our index and paste it right inside of the create we don't need the car information so let's get rid of that and we don't need the button but we just need the content all right so inside the content let's create a new diff which has a class of margin dash auto the width is again 80 and the padding on the y-axis is 24. this is the diff around everything so let's create another one with a class text center and in there i want an h1 create car the class is text five excel uppercase bold save it refresh the page now let's create our form by saying diff the class is flex justify items in the center justify in the center and the paddock top is 20. now we need a form because we need to submit it and i'll come back later on the action and the method all right we have a div in here which has a class of block type is equal to text let me actually analyze this on the line below the type it has a class of block shadow dash 5 excel margin bottom is 10 p width is 80 italic and i want to add a placeholder but i want to style the placeholder so placeholder that's great that's 400. let's give it a name of name and let's give it a placeholder so let's say brand name let's save it and test the output all right this looks fine let's copy paste our input actually so the second one has a type of text the class is alright the name is founded and the placeholder is founded we need one more because we have our description the name is description as well we need to submit it so we need a button so let's say button with a class of bg green dash 500 block shadow dash 5 excel margin bottoms 10 padding overall is 2 and the width 80 upper case and let's change the font to both and let's add a text of submit we need to submit our button so let's set the type equal to submit as well save it let's test the output and this looks all right we don't need anything fancy what's left here well let's scroll up because we need to change some things inside of our form whenever your user feels in the form the action is forward slash cars and we know that we're performing a post request so let's say the method equal to post if we fill in the form right now so let's say tesla 2000 test submit it's not working and this is related to cross site request forgery by default all laravel routes accept read-only routes so the get head or options request now if we go right below our form write down at csrf save it let's go back to chrome refresh the page and let's inspect the page for a second if we open our form you can see that we have an input type hidden the name is underscore token and the value is a weird id this token is generated at the start of every session and every non-read only route compares the submitted token against the session token so why do we do this it's mostly used when one website presents to be another the goal is for someone to hijack your users access to your website by submitting forms from their website to your site let's go back to visual studio code let's go to our cards controller because we're ready to do something so we're creating a form but we want to store data so we need to work in our store right now and usually when i program i always perform a dd to see if my form will be submitted or not so let's say okay save it go back to chrome refresh the page click on submit and let me close off my inspect element and you can see that it actually works because ok has been printed out there are two ways how we could insert a record let me remove my dd the first one is by creating a new instance so let's say variable car is equal to new car what i just showed you then we could say that we have our car but we want to specifically say that our name is equal to the request method right here and from the request we could wrap http information from input and then the name of our input field so in our case it's name let's do the same thing for the column founded and description so car founded is equal to request look for input fields with a name of founded let's do the same thing for the description let's set that equal to request find an input field with the name of description let's save it let's go back to google chrome well let's change the end point to cars let's add a new car let's say it's tesla founded in 2000 and the description is test let's submit the form it actually works because we're not getting an error so let's hop to iter let's say select all from cars and well something went wrong right here because tesla has not been added inside our table and the reason why this did not work out is because we need to save it and that's not what we're actually doing let's say that we have our car instance and we want to perform the save method on it so it will save the name founded and the description until you use this save method the instance of car represents the car fully but the data hasn't been saved before we test it out let's actually return a redirect to forward slash cars let's test it out let's go to google chrome refresh the page and you can see that tesla has been printed out and inserted inside our database let's test out an item let's select everything from cars again and you can see that tesla has been added all right the second way is the way that i actually prefer to use and that's by passing in an array to a model so let's actually comment out all everything that we just created and let's create a new variable card and i set it equal to car and that's called the create method so what we need to do is to pass it in as an array so brackets and inside the brackets we need to pass an associative array so we basically need to do the same thing as we did before let's define our column name so name and let's set it equal to request grab everything from input with the name of name comma found it grab everything from the http that's inside an input field with a name i found it now let's do the same thing for the description so request its input description this won't work right now because every time you want to pass an array to a new model every property that we have right here needs to be approved for mass assignment in our model file so what we need to do is to open our car model and right below our primary key we need to create a new property so protected fillable and let's set it equal to an array where we need to say which items can be mass assigned or columns excuse me so let's say the column name found it description let's save it let's hop back to google chrome let's create a new car let's say volkswagen i don't know when in this fount and the description is volkswagen as well let's submit it scroll down and volkswagen has been added inside their database i need to mention one more thing in our cars controller we could use make instead of create as well but i prefer to use create because it will save the instance to the database as soon as it's called so we don't need to add the variable car save method and whenever you use make you actually do need to use it so that's why i prefer to use create this was easy wasn't it well up well updating records are pretty similar you can get a specific instance change its properties and then save it or you could pass in an array of updated properties so right here we're going to work with the edit and the update page we're going to work with the edit and update method so same thing as before let's return a view in the edit form let's return our view for the edit form so return a view called cars dot create that's created so in our cars folder create a new file edit.php and we could basically copy paste our entire create file and the only difference is that we just want to add values in our input so let's copy it let's go to the edit page paste it right there but before we start editing something in our edit page let's quickly add an enter on every item that comes from the database in order to show the page so let's go to our index right insider for each loop and below our margin audio div let's create a new div let's give it a class of float to the right and let's create an entry in here and point to cars forward slash and we want to edit a specific post so we need to grab the id that we have from our car that we're printing out so let's say car id forward slash edit and just to make sure that our url is good let's go to item let me open a new tab now let me say php artisan route list now let's search for the edit action which is right here and you can see that it has cars forward slash car so the id forward slash edit all right let's add text first of edit and let's say and rar again let's give it a class of well we could actually copy the class that we have right here let's paste it in but let's change the text gray to text green save it let's go to google chrome refresh it and you can see that every item has a edit button let's click on it and you can see that the endpoint is good so we need to work on our form so let's do that let's go to visual studio code edit page let's scroll up let's say update the car and since we want to edit a specific row in our database we need to pass in that row to our views so let's hop to the controller again what we could do is to say dd variable id refresh it and the id is one which is right so based on that id we could basically say we have variable car let's set it equal to car and find me a car with that specific id let's do the car again save it refresh the browser now let's open attributes and we have a car with id number one the problem that we face right now is that the find method will return a collection so we need to loop over it and if we add the first method to it at the end so first we will return an object so we don't need to loop over it in our ui let me get rid of the dump and let's return it to the view and we don't want to pass it in as an array like i just said so let's say width cars s car well let's say car s car let's actually replace our view with edit save it let's go to chrome let's go back to the beginning let's click on edit and we're on our update and you can see that audi has been printed out let's do the same thing for the founded let's say value is car founded and for the description as well so let's say value variable car found it all right let's have a look at our end point because it's cars forward slash update right now let's hop to i term and as you can see it should be cars forward slash the id so let's change that let's set it equal to cars let's remove update forward slash car id let's save it let's test it out in the output all right let's refresh it you can see audi the value over found it and the description so let's up to vs code because we're ready to perform our update method right now and we're basically going to follow the same approach so what we did in our edit was finding our car and we sent that back to the view so what we need to do right now is to basically go up copy the create method that we have scroll down paste it right inside of our update and we need to change a couple things right here we don't want to create but we want to update a car but in order to update a car we need to do something with this specific id that we have so the current post let's go right after our model let's say where pointer let's go on the line below and in our where clause let's see where the id is equal to the current id that we have right there all right are we done well no let's return the view as well so the redirect right let's save it we're trying to update something right and that isn't possible with a post request as you could see right here our method is equal to post and in order to fix this in laravel we need to perform a put request which our csrf token will understand so let's save everything let's go to chrome let's change the name to aud2 and let's submit it something is going wrong right now with our token right or with our method and if you're familiar with html and php you know that it's only possible to submit a get or a post request so how do we fix this we could keep our method equal to post and we could go right below our token and say add method and we basically want to transform our post request to a put request and that's how you use eloquence update model save it let's go back to google chrome well let's go back to our cars let's click on edit change audi to rd2 submit it and you can see that our name has been changed to rd2 now there's one more request left and that's the delete method that we have right below our let's see update method and once again the leading is very similar to updating with eloquent so first we need to create a form in our index.blade.php let's say right below our edit and it can't be an answer because we need the request and that's not possible when you create an answer so inside our form let's create our csrf token again our action is to forward slash cars forward slash the specific car id let's save it and let's go to iturn as you could see right here destroying has a uri of cars forward slash the id the method is equal to post let me close it off actually all right and right below our csrf token we need to create a method where we want to delete so inside our form i want a button where the type is equal to submit and let's actually copy the entire class that we have over and over above let's paste it right inside of our button and instead of saying green let's say red and let the text be delete delete with the same icon that we have save it let's hop to google chrome refresh the page and we have a delete button right here but let's add a little bit padding in our form so let's say class is equal to pt3 save it refresh it and this looks alright let's go to visual studio code we already have our id passed in right here so let's test it out that's a dd that specific id save it google chrome refresh the page let's click on delete and the id is one which is equal to the uri let's go back since we already have the id inside our method we could go up to our edit copy the find method place it right inside of our destroy so we want to find where that specific id exists and we don't need to do the same thing as we did before with our creator update we only need to say car delete that specific row let's return a redirect to forward slash cars save it go to chrome refresh the page just click on delete and you can see that it has been deleted let's delete them all and you can see that this works fine but to be honest this is not the way that i prefer what i usually like to do is to pass my model right inside of my method so let's get rid of id let's say our model car variable car and if we do this we don't need to pass in the id since it's already passed in inside our action so let's get rid of this line we do need our car delete because we can do it instantly and this is how you save yourself an extra line of code there's no reason to look up an instance just to delete it but this is a thing that i will show you later on as we proceed with the course i didn't change it at first because i thought it was just as easy to keep the resource list and we can do the same thing for other methods as well like i said that's not for now later on when we're going to work with apis you often need to convert aries to json and before we continue on guys i want to start using my instagram more so if you haven't followed me on it and you're interested in more personal stuff about me i've added a link in the description so go check it out and give me a follow alright so what we've done so far was doing a request to the database in order to find one or multiple rows now whenever you try to return multiple rows you're returning a collection which you can see as an array on steroids but what if you want to convert it into a string that's when you could use serialization if you have a complex array or an object you want to convert that into a string and that specific string is usually adjacent in web-based context serialization can be very complex for database records and in my opinion this is where orm falls short but laravel comes with two very powerful methods that we could use and that's the two array and two json methods so before we test it out let's hop to our index.blade.php file let's open it let's get rid of every piece of laravel because we're not going to use an area anymore so every ids as well but we're going to work with a string which isn't json and don't close off the file because we will return it in a second when we're done let's open our cars controller let's go right inside of our index right now we're returning a collection so let me show that to you let's say far underscore dump variable cars save it let's hop to the browser refresh it and you can see that we have an eloquent collection right here we don't have a car in our database so let's add one audi rd1908 this is my audi description submitted and once again we have an object which is a collection with this specific data inside of it now we can convert this to a stream so let's hop to visual studio code and right after our all method let's add another access operator called to array save it let's go to google chrome refresh it and don't look at this right now but look at what is far dumped on the screen we have an array with everything that we need inside of it all right let's go back to visual studio code let's get rid of the fire dump let's go back to our index plate and let's go right above our section and let's just print something out so let's say s for each our collection is called cars or array excuse me add cars s one single car printing out an array is a little bit different than a collection what we used to do is say car access operator name what we need to do right now is to add brackets single quotes name save it go to chrome refresh it and audi has been printed out as you can see now you could also convert it to a json and to do that we need to go back to our controller and only replace two array to while we probably guessed it to json let's fire dump it once again so far underscore dump cars save it google chrome refresh it and you can see that the output or the far dump is a string with multiple values in here but our loop isn't working anymore so what's going on right here well you usually don't convert a collection into a json object but you do convert it to an array json is mostly used for api stuff which we will cover later on well as you can see right here we have an opening double quote right here and a closing one right here so jason will convert it to a long string sentence so we can't loop over it so what we need to do is to decode it so let's go to visual studio code right below our cars let's say cards again let's set it equal to json underscore decode variable cars save it refresh the browser and you can see that our json have been converted to an object which is a generic php empty class now to fix our ui let's go to visual studio code let's get rid of name let's say name again with the access operator go to chrome refresh the page and while you can see that we have one row right here but we've deleted all of our ui stuff so let me get rid of the fire dom to show it to you save it google chrome refresh it and audi is printed out right here as you just saw well let me undo it you can see that jason will print out everything we have our updated ad our created ad the description founded name and id sometimes you just don't want that you don't want to send back every single database information that you'll have in your apis what eloquent allows us to do is to hide certain attributes if we cast it to json so in order to do that we need to hop to our model and right below our fillable we can create a new property protect it called hidden and let's set it equal to an array and inside our array we need to pass in attributes that we would like to hide now right now we don't really have data that can't be shared with others right we have a name founded description but what if you have a password or let's say a remember token now that's when this is very very handy to use and in our case let's just say that we want to hide our id just to show you what it is save it chrome refresh it and as you can see our id has been deleted or removed right inside of our object so let's add our name found it and the description just to test it out save it go back to chrome and we have only our created ad and updated that but let's undo it so let's just get rid of id as well and let's just say updated underscore ad i think this looks better because we don't actually use it there's also an option to show attributes so instead of blacklisting them we could whitelist them so let's create a new property let's say protect it property name is visible let's set it equal to brackets and inside the brackets we need to add the names that we would like to show so let's say that we want to show the name founded and the description save it go back to chrome and you can see that one thing has been changed right here because our created ad has been removed so it only shows the columns or the values that we have added inside our visible so if we add updated or created underscore add save it refresh it our created ad has been added what we're doing right now is testing it on a json object but we could also test it on array so let's change to json to to array we need a fire dump but we don't need to decode it anymore save it go back to chrome and the output is exactly the same and if we let's say remove created at save it refresh the browser and our created ad has been deleted in a relational database model which eloquent is it's expected that you will have tables that are related to each other so let's say that we have a table cars with our id name founded and the description now every car obviously has its own car model or multiple car models would you adjust your current car table by adding a new column model or would you create a new table well if you would add a new column it would look like this right we have our id name founded description and our model like i said the issue is that one car has many models and not only one so we would have multiple ids with the same name same founded same description but different models and that's not beneficial in a relational database model it's expected that you will have tables that are related to each other and eloquent provides simple and powerful tools to make the process of relating your database tables easier than ever now in eloquent we call this an one-to-many relationship we can have one car and that one specific car has many models so our database would look like this we would keep our cars model just as it is so the id name found it in description but we create a new table called cars underscore model which has its own unique id but the car id which refers to the id in the table cars it has a model name and it has the timestamps there are two ways how we could handle our migration we could create it right inside of our current migration of cars so right below of it since it's related or we could create a complete new migration and from my demonstration today i will add it right below the current migration of cars that we have so it makes it a little bit easier but i recommend you to use a new migration instead of adding it right here let's create the schema so let's say schema create now inside the create method we need to define the table name so let's say car underscore models then we want to pass in the function just like what we're doing up here let's pass in blueprint variable table right outside of our parentheses let's add curly braces our car model has its own unique id so let's create a table increments now let's pass in the id then we have an integer so let's say table an unsigned integer with a name car underscore id which will refer to the id that we have right there we'll do that in a second we have another table which is a string of model underscore name let's add a table for timestamps the next one that we have is new so let's say that we have our table which has a foreign key of car id so let's say foreign and inside our method let's pass in car id so we're basically saying that the unsigned integer that we have is our foreign key we need to link it somehow with our cars table to the id so let's do that what we could do is to say access operator references to the column that we have inside our cars called id on so on the table cars so let's do that let's say cars and this can be it so we're basically saying our foreign key is car id which references to the id on cars what i want to do is to add one more method so let's say on delete and what we want to do on the lead is cascade and let me align it actually so it looks a little bit better all right why do we add the on delete that we have right here well if you delete the car you don't want your car models to be in your database since it's not being used because the id does not exist anymore and that's what the on the lead does if you delete a specific car it will automatically delete the car models related we can add one more value inside the parenthesis inside of cascade but for example right now let's just keep it as it is but just in case you don't want to delete it you could also set it equal to set no so whenever you delete a specific car it will set the car model's car id to no we will keep it as cascade what we need to do right now is to migrate it so let's hop to item let's say php artisan migrate all right it's saying that nothing is there to migrate so what we need to do is to say php artisan migrate rollback which will roll back our migration and then we can migrate it once again so it migrates to create cars table so let's hop to my sql let's say show tables and you can see that we have car models and our cars table when i created a video about introduction to eloquent we didn't do anything with our show methods right here the reason why is because i want to use it right now and before we continue on guys i want to start using my instagram more so if you haven't followed me on it and you are interested in more personal stuff about me i have added the link in the description so go check it out and give me a follow i want to show you how we could set our models manually first so let's hop to the command line interface and right here let's say desk car underscore models to see what we have as you can see our id is pre which stands for primary but our car id is mo mo basically tells your database that there can be multiple ids in this column that have the same value and that's correct right because a car can have multiple models there's also another one which you don't see right here and that's uni so let me write it down uni which stands for unique we need to do something inside our model that will do something with the data that we have since we already have our car model we need to create a new model so let's say php artisan make me a model called car model let's hit enter and our model has been created successfully if that went a little bit too fast for you let me rewind how this works is that the car model belongs to a specific car and the car has many models and we need to fix this right inside of the models that we have so let's go back to visual studio code and let's start off with our car.php so our car model what we need to do right here is to create methods that will return the right data based on our tables let's create a method let's say public function and the name is pretty tricky because it needs to be related to the associate table that we want to use this could be underscores camel cases or whatever but in my opinion try to keep it the same as the model name for the one-to-many relationship we have the opportunity to get multiple models so let's say that we want to call it car models instead of a car model so what do we need to do right here we need to return something which is this a method called has many parentheses semicolon inside the parentheses we need to pass in the model class of the associated table so in our case we want to associate car model common class with the car model that we have and we don't need to add the full pad right here because requiring eloquence complete model what we have done right here was defining the relationship method for the car model we need to create another method for the car model that's related to the specific car so let's open our carmodel.php but we could actually copy our let's say protected table and primary key let's go back paste it right here table is car underscore models and the primary key is id as well so what's next we need to create a new method so let's say public function and once again the name needs to be related to the model that we want to be associated with so car so let me add it as a comment a car model belongs to a car so what we need to do inside our method is to return this and i'm basically repeating myself again one model or multiple belongs to and inside the parentheses we need to pass in the model that we want to associate it with let's say car column column class save it and let me actually save my model as well all right we need to add data inside our database and there are multiple ways how we could do this we could do it directly inside of our controller but what i just want to do is to go to item and add it manually so let's say that we want to insert into cars because we need a specific car the title founded description comma values audi comma 1908 and this is my audi as the description oh it's not title excuse me it's name my bad all right let's select all from cars this point well let's desk car underscore models all right so we need to insert into car underscore models just the values car id and model name so let's say car underscore id model underscore name values and as you could see our id of the audi is one so one comma inside single quotes a1 which is the model of the audi if you're not familiar with cars hit enter one row has been affected let's hit the arrow up and let's change the second one to a3 and a5 three is enough so let's select all from car underscore models and this is fine you can see that our car id has multiple values which are the same and right now we're ready to output the data on our page so let's go back to visual studio code let's go to our cars controller and the show method since we've used the resource flag you can see that the id has been passed in automatically well actually let's go to our ui and let's change that first so inside our index let's go to the car name let's copy it let's create an answer and let's place it right inside of the enter let's give it the href of forward slash cars forward slash that specific car with the id save it let's go to chrome refresh it and you can see that our audio is clickable but let's add a other as well so in our h2 let's say hover text that's great that's 500 save it let's go to visual studio code and since we've used the resource flag inside our cars controller our show method has the id passed in so what we could do is dd id just to double check that everything works fine refresh the page click on audi and in the uri you can see that the endpoint is one and one has been printed out so this works fine this is always a double check that i do when i work with laravel let's get rid of our dd because we could create a new variable car since we're getting one single car back set it equal to the car and find everything of that specific id now let's return a view that we don't have yet to cars.show and we want to pass in an object and not an array so width car has that specific car before we continue on let's do the car again to see if everything works fine save it refresh the browser and i'm not opening my attributes but you can see that one car has been returned now we're ready to create our view so let's go back to our cars create a new file show.blade.php let's say extends layouts.app let's create a new section called content section let me close off my migration and my controller because we could copy most of the stuff in our index so let's scroll up we could use our header but instead of saying cars we could print out the car name variable car name let's go to chrome well let's remove our dd first in our controller excuse me save it chrome refresh it audi has been printed out let's go to our index and we don't need to add a new car button but let's copy this entire class all right let's place it right below our div let's scroll up we don't need the loop we don't need the edit button and the form and the closing div of course we do need to found it we don't need a name since the name is printed out at the top and we do need the description save it and let's go to chrome we need to remove our end for each refresh it this is all right let's style it a little bit remove the width because we don't need it let's say that we want to text center everything and this looks pretty good right now but what we could do is to add some lorem ipsum excuse me looks a little bit better than one piece of text all right let's scroll down let's copy it you don't need to do this it's just for myself let's edit it let's change the text all right let's click on the audio again all right and right now we're ready to print out our models what i want to do is to go right above our hr let's create an unordered list with a paragraph of models let's set the class equal to text large text gray 700 and padding y-axis is 3. right below our paragraph we want list items but before we do that i want to loop through my models first and i don't want to create a for each but i want to add a for else what that went wrong and the reason why i want to do that is because there might be a chance that a car does not have models so we want to print out something if there are no models right so first if there are models create a list item with a class of inline italic text as grade as 600 padding x is one and the padding y is six so right here we want to print out our model name but before we do that let's fix our loop we want to loop through our car but we want to access another property called car models so we're accessing the method that we have in our car model as one single model in our list item we could basically say model gave me the model underscore name if it's empty print out a paragraph of no models found save it go back to chrome and you can see that the models a1 a3 and a5 have all been printed out you might wonder why we didn't use the access operator to access our model name and that's because we're returning an object so we can't use the access operator and this was it now the has many true relationship is pretty convenient for pulling in relationships of a relationship does that sound weird let me show you an example if we take a look at what we've done earlier so we created a table cars that has many car models but every car model has its own r engine whenever you try to print out audi you could also print out all the models and the specific r engines based on the models so not the engine spare car but the engine spare model and that's basically what the has many true relationship does with this structure it will assume that your engine stable has a model id to relate the models to the engine so what we're going to do is to create our migration first then the model and then we will retrieve records from the database so let's hop to our terminal and in here well let me go to this screen all right and here let's create a new model and migration so let's say php artisan make me a model called engine dash m let's hit enter all right our model and migration have been created successfully so let's define our migration now inside our visual studio code or code editor let's scroll down let's open it and right here we have a table called engines with a unique id let's call it id we have a table which is an unsigned integer that will be linked to the models so let's call it model underscore id now every engine has its own name so let's say table string engine underscore name let's keep the timestamps and let's link our model id to our models table let's say table foreign let's say model underscore id references to id on the table car underscore models and on delete just cascade everything let's save it and we're ready to migrate so insider cli let's say php artisan migrate and apparently i've made a typo because ah i need to close it off with a semicolon all right let's run php artisan migrate one more time and our migration has been created so now we need to add some data inside our engines table so first let's go to my sql let's show all tables you can see that we have our table engine right now so that's desk engines all right this looks pretty cool our id is our primary key and the model id is the foreign key so what i want to do is to insert into engines we only need to add the model id and the engine underscore name the values are let's say 1 which is the rda bond comma 1.2 tfsi let's hit enter let's set the arrow up let's change the second one to 1.2 tfsi 122 arrow up let's change it to 140. let's actually select everything from our underscore models we just entered data for the model name a1 and let's add some data for the id number two so the a3 as well hit the arrow up let's change the model id to 2 and let's change the value to 1.2 tfsi and 185 and let's do the same thing for the third one which is let's say 2.0 tdi enter let's select everything from engines to see our data and this is all right i can work with this for now we need to do is to hop to our model but we don't need to do anything inside the engines model that we have what we do need to is to go to our car.php so our car model right below our headquarter method that we have because we're going to define a has many relationship let's create a public function and the name is engines since we're trying to access the engines and here we need to return this has many true this method accepts two arguments the first argument is the name of the model we wish to access and since we're trying to access the engines let's say engine column colon class comma the second argument is the name of the intermediate model so the model that we need in order to access the engines and since we're retrieving the engines based on the car model it will be car model column column class let me align it actually so it looks a little bit better all right this can be it but i need to show you one more thing before we continue on in eloquent the foreign key conventions will be used when performing relationships queries there might be a chance where you would like to change it in that case we could add a third and fourth argument to the has many true relationship so let's say comma inside swinging quotes let's say that the foreign key on our car model class is called car underscore id let me add a comma and add a comment so foreign key on car model table now for our engines our foreign key is model underscore id so let's say foreign key on engine table let's save it and we're ready to test it out in our controller so let's go to our cars controller right inside the show method let's dd let's say variable car and we want to access the engines save it go to the browser refresh it let's click on audi we have a collection right here let's open it and from the table engines let's open the attributes and it's 1.2 tfsi this is not actually what i want to show you what i want to do is to get rid of my dd and print it out in the ui so let's open our show that blades.php and let me actually zoom out a little bit for this section i want to get rid of the loop and the unordered list what i want to do is to create a table for this so let's say table let's give it a class of table dash auto and here we want a table row with a class of bg blue 100 that's a typo inside my table row i want to have table headers so th with a class of width dash one forward slash two border dash four border dash gray dash 500 and in here we need to write down model let's copy our table header now let's paste it one more time because the second one is the engines we have our headers now now we need the data right for our model and engines so right below our table row let's create there for each loop because we want to loop over our models and engines so let's create it for each loop because we want to loop over our models and engines so first let's say car car models as one single model if it does exist give me a new table row and there i want a field so a td with a class of border dash four border dash gray dash 500. in here we want to print out the model model name and before we continue on let's actually save it and test it out in the browser i remove my headquarters so let me actually get rid of it right here all right let me save it let's go to chrome refresh it and this works fine so now we want all of our engines right inside of these cells so so right below our table cell we need to create a new one with the same class so border dash 4 border dash ray dash 500 we want to create a for each loop because we basically need to loop over all of our engines so let's say car engines as one single engine but the problem that we have is that if we print out the engines like this right now all engines will be printed out for every r model so what we do need to do is to create a if statement where we check if our model id is equal to the engine model id if it is true print out engine engine underscore name save it refresh the browser and all of the engines have been printed out for the right model now let's add one more thing because our empty statement is empty well that sounds weird so let's say no car models found save it refresh the page and it still works pretty fine next to the has many relationship there's also another relationship which is pretty similar and that's the has one relationship what we've done with the has many crew was accessing many related items through intermediate items and as the name suggests with the has one true you're basically accessing a single related item through a single intermediate item so what if each car has its own model and every model has its own production date so we have audi with the id number one which has a model name of a1 model name has been linked to a specific date so once again let's create a new migration let's go to iturn let's go to our folder tab and right here let's say php artisan make me a model called car production date space dash m now let's hop to our migration because we need to set up our table let's keep the id but let's pass in the name id let's say table unsigned integer which is the model underscore id now we have our date as well so table date and let's call it created underscore ad we don't need the timestamp but we do need to link our model id of our foreign key to the table models or car models so let's say table foreign let's get rid of it actually and let's just copy the previous one of the engines now model id refers to id car models undelete cascaded let's save it let's go to iturn because we need to migrate her stuff again php artist in migrate migration has been created now the next step is to add dummy data inside our production dates table so let's desk tables again well let's show tables excuse me let's then say desk car underscore production underscore dates all right this went well what we need to do is to insert into car underscore production underscore dates model underscore id and the created underscore add now the values are id number one and for the creator that let's set it at 2007 because the a1 has been created in 2007 0 6 27 so 27th of june and 2007. let's hit enter set the arrow up now for the second one which is the a3 it has been created in 1996 and i don't mind doing the months and the days arrow up one more time for the a5 which has been created in 2007 as well let's select everything from car underscore production underscore dates oh excuse me select all from my bad all right so we have our ids our model ids and our created that now the next step once again is to define our relationship let's go to our car.php right below our engines let's say that we want to define a as one true relationship oh another typo let me zoom in because in here we want to create a new method so public function called production date and what we want to do is to return this has one true and just like the has many we need to pass in at least two arguments the first one is the name of the model we wish to access so car production date column column class let me actually analyze it again comma the second one is the intermediate model and that's car model column calling class comma let's actually define the foreign keys as well so let's say that the foreign key of our car model is car underscore id and for our car production date it's model underscore id save it let's go to our controller so our cars controller and right inside of the show method that's far underscore dump car production date save it let's go to chrome refresh the page we got our object returned with a laravel troopy with the right data that we need our year is 2007. like i said it has a model id and its own id now let's print this out in the ui let's remove our fire dump save it and in our show.played since we already have our loop and we only want to print out one specific cell from the database we don't need to create another loop what we could do is to go right below our td create a new td i'll give it a class again of border dash 4 border dash gray dash 500 and inside of our td we could basically say give me the php date function which accepts two arguments the first one is how you want to print out your date so let's say the day dash month dash year comma and we want to string to time our car production date and we want to print out the created underscore ad save it go to chrome refresh the page now let me zoom out actually and while our dates have been printed out but we need to add a table header so let's duplicate it let's change it to date as you can see the width of our cells are a bit too big so let's set the width to 1 4 save it go back to chrome refresh it and this is exactly what we need now compared to the other relationships i'd say that a many-to-many relationship is probably a bit more complex so let me show you a little example right there we have our table cars and a new table called products a carburetor can have multiple products think about auto mobiles electric vehicles automotive parts luxury vehicles pickup trucks each product type can be related to many cars audio mercedes can both be your auto mobile or an electric car in fact they are both in auto mobile so what would you do would you keep our current table cars and add a product id and for a products table at a car id since a single product can't have a column car's id and the single car can't have a product type id because remember we need to create a many to menu relationship what we have on the screen does not work so let me show you a better solution what we need to create is something which is called a pivot table and right now you might be wondering what a pivot table is it's a table that connects the two tables so we still keep our table cars and we also add our table products and we don't link them but in between we place a pivot table now for the naming of our pivot table i prefer to place the two singular table names together ordered alphabetically and separated them by an underscore so since we're linking our cars and products together our pivot table should be car underscore product you don't need to pass in any strings as the data type you only want to pass in the id of the car and the id of the product now that i've showed you a demo let's hop to the cli and let's create a new migration and model first we need to create one for our product so let's say php artisan make me a model called product dash m let's go to our migration let's actually open it up all right we want to keep our table name products we don't want to change our table name products is fine we want to keep our id well let's actually change it to increments let's give it a name of id and in here i want another column which is a string called name and i also want to keep my timestamps and that's it we don't need more so let's save it let's go to item and that's right now php artisan migrate our migration has been created now for the pivot table we do need a model and migration as well the model is there to interact and the migration is there to create our table so right here we could say php artisan make me a model called car product dash m alright let's open the migration as well let me scroll down and in here we need to create two columns for the ids that we need so let's get rid of the first the first column is an integer called car underscore id and it's unsigned let's actually duplicate it because the second one is our product underscore id which is unsigned as well now here comes a pretty weird part both our car id and project id are foreign keys so what we could do is to say table foreign let's say car underscore id references to id on the table cars and on delete cascade it let's end it with a semicolon let's copy it and let's paste it right here let's change the second one to product underscore id references id on products and on delete cascade everything now before we migrate it laravel will automatically define your table name sometimes you just want to define your own table name so let's change our table name to car underscore product save it let's run php artisan migrate our table has been migrated now let's go to mysql let's say show tables we have our car underscore product and our products let's desk products to add it so what we want to do is to insert into only the column name the values let's say electric vehicles insert into my bed products name and the values is electric vehicle all right let's hit the arrow up let's add another row called auto mobiles and let's add the arrow up one more time to add luxury cars now if we select everything from our table products we have three new values in here which is alright and let's also select all from cars so our car has an id of one and our products as pre new ids so what we could do is to insert into car underscore product let's say car underscore id product underscore id the values are 1 comma 2. so audi is an auto mobile let's hit enter let's say that audi is also a luxury car all right let's also add a new car in our database so insert into cars let's say the name founded description values are mercedes 1926 and let's say this is my mercedes for my description alright select all from cars again what we could do is to basically hit up again because we want to insert into car product the car id is apparently seven as you could see let's say that mercedes is a automobile and a luxury car so let's say two arrow up and three alright we're ready to define our relationships right now so let's go to visual studio code let's go to our car.php let's create a new function public function products in here we want to return variable this all the products that belong to a specific car so belongs to many which takes one param and that's the product model so let's say product common column class let's save it let's close all of our migrations because we don't need it let's go to our cars controller and let me show you how we could print stuff out now right below our variable car let's say fire underscore dump let's say variable car products save it let's go to google chrome refresh the page and you can see an entire list of data inside our collection what we could do is to say command f or ctrl f and let's write down pivot you can see that we indeed return data from a pivot table and like i said just like the has many relationship we get access to a collection right here of the related items but this time it's from both sides so from the cars and from the products so what i want to do right now is to print it right in the ui nothing fancy but just to show you how we could print it out so let's go to our show.blade.php again now let's go right at the bottom above our hr let's say paragraph let's give it a class of text left let's say product types let's create a for else loop we want to loop over car with all products as one single product if there is a product let's print it out so product name otherwise create a paragraph and let's say no car product description or something save it let's go to our cars controller let's get rid of our fire dump save it google chrome refresh the page and the products types are auto mobiles and luxury cars next to the many-to-many relationship there's also an inverse many-to-many relationship we have only printed data from one site so from the cars since i haven't got a ui where to print out the product types i want to show it to you right in the controller so before we do that we need to create our relationship inside our product.php so right here let's say public function cars we want to relate our cars to the product so we want to return this belongs to many car it takes one param which is the car calling colon class save it let's hop to our cars controller right inside of the show method we could create a new instant called products and let's set it equal to new product and what i want to do is to find data from this specific car so we want to pass in the id oh excuse me we don't need new my bad let's print underscore products save it go to chrome refresh the page and we have a object of product with every piece of data that we need that is related from our product to our cars like i said before the many-to-many relationship is unique because it's the first relationship that has a pivot table what we have done so far was accepting requests from the forms and sending it immediately back to the database without doing any checks now to give you an example let's go to google chrome let's create a new car now let's add the input fields so the brand name is mercedes the founded is well it should be an integer because we're trying to add the year where a car brand has been found so what we could do is to add a string right here so let's say dory the description is mercedes as well let's submit our form and right here we're getting an error so let's see what it is we're getting a general error incorrect integer value our database is accepting an integer for the input field founded as you can see right here and what we have done was sending back on string to the database so let's fix stuff like this i want to divide the video in two chapters first we're going to cover the controller validation and then we're going to cover the form requests larval has quite a few ways how you could validate incoming data and i want to split that section in two since we have two primary options so let's go back to our code right into the store method that we have and let me actually remove the comments because we don't need it the request object that we have right inside of our controller right here has a validate method that we could use and it will provide a convenient shortcut for the most common validation workflow what we need to do is to add it right above where we create a car so let's say request validate just like creating a new car we need to pass in an array right here so let's add brackets and let's hit enter inside the array we need to pass in the values that we have inside our input field so let's say we have our name pointer and a value now the value right here needs to be required so we're basically tearing our application every single user needs to fill in a name now let's do the same thing for founded which is required as well and for the description we want to set the value to required as well right here you can see five lines of code and if you have worked with php before you might think that this is definitely not enough to do validation well the lines that you see on our screen right now are doing way more validation that you actually think based on what we have entered right here it will determine what kind of error message needs to be printed out so let's rewind let me explain to you step by step what this method does first the validate method will check the incoming data from the request and check whether it is true or not if it is valid so if it's true we will move outside of the request and we will create a new car so let me add it as a comment if it's valid it will proceed so whenever our validation is valid it will create a car but what will it do if it's not vetted well it will throw a validation exception and then it will redirect you to the previous page with all the necessary validation errors next to setting our input fields equal to required we could use a couple other rules now to add them we need to separate them with a pipe so let's go right after required for the name let's add a pipe and what we want to do is to say that our name is unique and it needs to be on the table cars so we will look in the table cars to see if let's say a name already exists and based on that it will send back an error message or it will let you continue to create a car now for the founded let's add a new rule let's say that it has to be an integer another pipe the minimum is pipe the maximum is 20 21 so the year that the car has been found has a minimum year of 0 and a maximum of 2021. now right here we're passing it in as an array but you could also pass it in as a two-dimensional array so what we could do is to add brackets right around required just like this comma and let's do the same thing for unique just like this but this is not something i recommend i prefer to use the pipes so let me undo everything all right now there's a bunch of rules that you could use right here and it will take ages to add them all in one single video i'll recommend you to look at the laravel documentation to see which ones there are i will only show you the necessary ones that i always use and during the next few videos you'll be seeing some more rules that you could add now if we hop to our browser and refresh it our error message is still appearing on the screen that's because we somehow need to print it out in the ui so let's go to our create.php right here and let's go to the bottom of our page and let's say right below our form create an if statement and what we want to do is to use the global errors variable and we want to see if there are any errors so if there are errors we want to print out some stuff so first a div with a class of width dash four four slash eight margin is auto and the text excuse me and the text is center so then we need to create it for each loop because we're getting back an airway so there could be more than one error message we're going to loop over errors all so all the errors that we have as one single error and what we want to do then is to print them out as a list item let's give our list item a class of text as read as 500 and list none and inside our list item let's print out variable error save it let's go to google chrome refresh the page let's actually undo it add a new card let's say give it a name of mercedes found it in let's say a string description is mercedes as well let's submit it and they found it must be an integer have been printed out but something happened with our ui so let's go to our create blade again well let's copy your entire if statement and let's paste it right below our div save it google chrome refresh the page and let's actually do it one more time mercedes mercedes mercedes submit and the founded must be an integer have been printed out let's add one more card without a name so founded is 1918 description is mercedes submit and the name field is required has been printed out and this is how it works we haven't set the messages right here that will be printed out whenever the request goes wrong now a cool thing laravel provides for us is to create our own custom rules and in order to do that we need to run an artisan command so that's up to the command line in here let's say php artisan make me a new rule called uppercase let's hit enter and our rule has been created successfully so let's go back to visual studio code now to customize our rule we need to go right inside the app folder that we have and then you can see a rules directory right here that has been created so let's open it and you can see the uppercase.php file if we open it you see two methods that are created for us obviously we have the construct but we have the passes and the message now let's start off with the passes now this accepts an attribute name as the first param and the user provided value as the second param what it will do is return a boolean whether or not the input passes the validation rule then it will go to the message method that we have right here which will return the validation error message so let's create it in the passes let's say return string to upper we want to pass in the value and we want to equal equal equal see if it's equal to value now for the message that we have right there let's get rid of everything in here we could say that the colon attribute which is the placeholder in your message for the attribute name so let's say the attribute must be uppercase let's save it and this won't immediately work because we need to call it somehow inside our controller so let's go to our cars controller at the top of our page let's require the file so use app forward slash rules forward slash uppercase let's go back to our store method so let's say that we want to tell our application that the name needs to be in uppercase so let's get rid of the rules that we have and let's say that we want to create a new instance of our uppercase that we created so new uppercase without a typo new uppercase alright if we save it let's go to the browser refresh it write down a brand name without a capital founded in 1918 and the description is mercedes submit it the name must be uppercase have been printed out and this is the exact message that we created in our uppercase.php so the attribute must be uppercase now obviously i'm not really sure if this is the right example but it's just to show you how these little things that make laravel so incredible work what we have done so far was accepting requests from the forms and sending it immediately back to the database without doing any checks now to give you an example let's go to google chrome let's create a new car now let's add the input fields so the brand name is mercedes the founded is well it should be an integer because we're trying to add a year where a car brand has been found so what we could do is to add a string right here so let's say dari the description is mercedes as well let's submit our form and right here we're getting an error so let's see what it is we're getting a general error incorrect integer value our database is accepting an integer for the input field found it as you can see right here and what we have done was sending backhand string to the database so let's fix stuff like this i want to divide the video in two chapters first we're going to cover the controller validation and then we're going to cover the form requests larval has quite a few ways how you could validate incoming data and i want to split that section in two since we have two primary options so let's go back to our code right into the store method that we have and let me actually remove the comments because we don't need it the request object that we have right inside of our controller right here has a validate method that we could use and it will provide a convenient shortcut for the most common validation workflow what we need to do is to add it right above where we create a car so let's say request validate just like creating a new car we need to pass in an array right here so let's add brackets and let's hit enter inside the array we need to pass in the values that we have inside our input field so let's say we have our name pointer and the value now the value right here needs to be required so we're basically tearing our application every single user needs to fill in a name now let's do the same thing for founded which is required as well and for the description we want to set the value to required as well right here you can see five lines of code and if you have worked with php before you might think that this is definitely not enough to do validation well the lines that you see on our screen right now are doing way more validation that you actually think based on what we have entered right here it will determine what kind of error message needs to be printed out so let's rewind let me explain to you step by step what this method does first the validate method will check the incoming data from the request and check whether it is true or not if it is valid so if it's true we will move outside of the request and we will create a new car so let me add it as a comment if it's valid it will proceed so whenever our validation is valid it will create a car but what will it do if it's not vetted well it will throw a validation exception and then it will redirect you to the previous page with all the necessary validation errors next to setting our input fields equal to required we could use a couple other rules now to add them we need to separate them with a pipe so let's go right after required for the name let's add a pipe and what we want to do is to say that our name is unique and it needs to be on the table cars so we will look in the table cars to see if let's say a name already exists and based on that it will send back an error message or it will let you continue to create a car now for the founded let's add a new rule let's say that it has to be an integer another pipe the minimum is 0 pipe the maximum is 20 21 so the year that the car has been found has a minimum year of 0 and a maximum of 2021. now right here we're passing it in as an array but you could also pass it in as a two dimensional array so what we could do is to add brackets right around required just like this comma and let's do the same thing for unique just like this but this is not something i recommend i prefer to use the pipes so let me undo everything all right now there's a bunch of rules that you could use right here and it will take ages to add them all in one single video i'll recommend you to look at the laravel documentation to see which ones there are i will only show you the necessary ones that i always use and during the next few videos you'll be seeing some more rules that you could add now if we hop to our browser and refresh it our error message is still appearing on the screen that's because we somehow need to print it out in the ui so let's go to our create.play.php right here and let's go to the bottom of our page and let's say right below our form create an if statement and what we want to do is to use the global errors variable and we want to see if there are any errors so if there are errors we want to print out some stuff so first a div with a class of width dash four four slash eight margin is auto and the text excuse me and the text is center so then we need to create it for each loop because we're getting back an array so there could be more than one error message we want to loop over errors all so all the errors that we have as one single error and what we want to do then is to print them out as a list item let's give our list item a class of text that's read this 500 and list none and inside our list item let's print out variable error save it let's go to google chrome refresh the page let's actually undo it add a new card let's say give it a name of mercedes found it in let's say a string description is mercedes as well let's submit it and they found it must be an integer have been printed out but something happened with our ui so let's go to our create blade again well let's copy your entire if statement and let's paste it right below our div save it google chrome refresh the page and let's actually do it one more time mercedes mercedes mercedes submit and the founded must be an integer have been printed out let's add one more car without the name so founded is 1918 description is mercedes submit and the name field is required has been printed out and this is how it works we haven't set the messages right here that will be printed out whenever the request goes wrong now a cool thing laravel provides for us is to create our own custom rules and in order to do that we need to run an artisan command so that's up to the command line in here let's say php artisan make me a new rule called uppercase let's hit enter and our rule has been created successfully so let's go back to visual studio code now to customize our rule we need to go right inside the app folder that we have and then you can see a rules directory right here that has been created so let's open it and you can see the uppercase.php file if we open it you see two methods that are created for us obviously we have the construct but we have the passes and the message now let's start off with the passes now this accepts an attribute name as the first param and the user provided value as the second param what it will do is return a boolean whether or not the input passes the validation rule then it will go to the message method that we have right here which will return the validation error message so let's create it nd passes let's say return string to upper we want to pass in the value and we want to equal equal equal see if it's equal to value now for the message that we have right here let's get rid of everything in here we could say that the colon attribute which is the placeholder in your message for the attribute name so let's say the attribute must be uppercase let's save it and this won't immediately work because we need to call it somehow inside our controller so let's go to our cars controller at the top of our page let's require the file so use app forward slash rules forward slash uppercase let's go back to our store method so let's say that we want to tell our application that the name needs to be in uppercase so let's get rid of the rules that we have and let's say that we want to create a new instance of our uppercase that we created so new uppercase without a typo new uppercase alright if we save it let's go to the browser refresh it write down a brand name without a capital founded in 1918 and the description is mercedes submitted the name must be uppercase have been printed out and this is the exact message that we created in our uppercase.php so the ash reboot must be uppercase now obviously i'm not really sure if this is the right example but it's just to show you how these little things that make larvae so incredible work but as you start to build out your own application you probably start noticing some repeated patterns in your controller the validation that we're doing in our store method so validating the name founded in description could be copied we could scroll down to the update and place it right above where we update our car and as you might see we're repeating the same five lines of code twice in our application and it could be way more and later on you even need authentication for some methods as well so you will repeat yourself there as well if your goal is to structurize your code better and extract these common behaviors that we have inside the controller you may be interested in laravel's form requests and to do this we need to create a form request from the command line so let's hop to the cli right there let's say php artisan make me a request followed by the name of the request so let's say create validation request let's hit enter and our request has been created successfully so a form will be created for us and to find it we need to go back to our code inside our app folder you can see our http and a new folder requests if we open it you can see the create validation request form that we just created now in here you can see two methods authorize and rules there are both public which means that they are available from anywhere inside your code so let's focus on the authorized first because it's an optional method if this method returns true so let's replace false with true this action is authorized to perform this request if it's false it will be rejected and in our case we need to set it equal to true the second method that we have is the rules and this is actually pretty self-explanatory it will check some rules before it will perform so what we need to do is to return well we could go to our cars controller copy our name founded and description inside our validate paste it right here let's remove new uppercase to requires save it you might be wondering how we could use our form request and this is where laravel's magic comes into play any routes that type hints of form request as one of its params will benefit from the definition of that form request so what we need to do in our controller is to go to the top right below our use create a new use app forward slash http forward slash requests forward slash create validation request save it let's go down to our store method instead of creating an instant of the request object right here we can change it to create validation request as one request now what laravel will do is validating the input and authorize the request since we've set it to true insider create validation request right here so we will authorize it if the input is invalid it will act just like the request object that we had before so inside our store method we could basically say request validated you could actually set this equal to a variable so let's say validated equal to request validated if you want to see the output so you could dd it but in our case let's just write it down like this let's remove our entire method that we have because it's not necessary anymore because we're already validating it save it let's go to the browser refresh it first create a new car mercedes 1918 merch cities well let's actually change the founded to a string so we know if we're getting an error message or not submit it and we have been redirected to the previous page with the appropriate error message passed along right here later on if the user is not authorized laravel will return a four or three forbidden error message so what we could do right now is to go to vs code copy our request validated let's go to the update remove the five lines of code paste it right here change the request object to create validation request and this works as well what we've done so far was adding text numbers dates and some other data types to our database now there's also a matter of file uploads to consider so what our google eventually is is well let me go to the ui add a new car is to basically add a new button right here where users could upload their image let's rewind for a second what are the steps that we need to perform in order for this to work step number one is to add a button like i said right here or we could search for an image right inside a computer or laptop step number two is to use the request object that we have right inside of our controller right here to upload the image and step number three is to validate the upload now for the last step we need to save the image somewhere in our application and then we need to send back the image name to the database not the image itself but just the name so first things first let's go to our resources views cars create thatblade.php and right above our first input field that we have where we're asking for the brand name let's copy it let's paste it right above now let me actually zoom in one more time all right now for the input the type needs to be equal to a file because we're going to upload something now let's change the name to image and we don't need the placeholder so let's get rid of that as well the class could be the same so let's save it go to chrome refresh the page and this is all right for now i don't need anything fancy because this video isn't dedicated to perfect styling that will be done whenever i create a complete project but before we continue on to our controller there's one more thing that we need to add right here let's go to our code again what we need to do is to tell our form that we're posting an image so the data needs to be encoded and to do that we need to add a new attribute to our form so right inside our form we have our action method so right after our method let's hit the space let's write down enc type is equal to while double quotes and you can see the options appearing on my screen and what we need is multipart forward slash form dash data all right let's save it because step number one is done now for step number two we need to use the request object and do something with it inside the controller but before we do that we need to make some adjustments to our database like i said our goal is not to save the image in the database but instead we want to save the path of our image so if we go to our cli and to our mysql you can see that we have our id name founded description created at updated at but we need a new column that will print out a string with our imagepad since we already have data inside our table we need to create a new migration so let's go to the other tab let's write down php artisan make me a migration called add underscore image underscore 2 underscore cars underscore table alright it has been created let's open it migrations and it's always the last one so for our up method we only need to add one column so let's remove the comment let's say table like i said it's a string and the name is image underscore path now let's also add our down method so let's say table and what we want to do for down is to drop column and what we want to drop is the image underscore path so what this allows us to do is to add a new column without rolling it back so let me show that to you let's go to itune let's say php artisan migrated our migration has been added now if we go to my sequel hit the arrow up and select everything from cars you can see that we added a new column right here called image path now we're ready to go to our controller and work on our image upload let's close off the migration and create that blade let's open our car's controller and let's go to our store method before we do anything let's get rid of our request validated now let's dd the request all just to see what will happen if we submit an image and before we continue on in the last video we talked about form request and we changed the request to our create validation request let's change it back to request for this video now let's go to our ui refresh it well we actually need to add an image so let's open a new tab and go to pixabay let's write down mercedes and you can choose whatever image you want it really doesn't matter i'll open this one all right free download download it i'm definitely not a robot and if i sound like one just let me know download it all right mine has been placed on my desktop so let's close off this tab because we don't need it let's choose a file so it's on my desktop let's give it a brand name of mercedes found it in 1918 description is this is my mercedes and be aware we're dding so we're not adding a new card submit it and right here you can see that there is a token so the underscore token that has been sent to the request object we have the name founded description and the image now if we open our image you can see a lot of information regarding the image that we just uploaded and i'm not going to cover every line of code that you see right here because most of the things that you're seeing on the screen speak for itself all right before we process our image save it and show it you always need to validate it so let's go back to our controller let's get rid of our dd let's say that we have our request and we want to validate so what we need to validate is an array so brackets single quotes we want the name from our ui which is the image and let's add some rules to it first thing that we want to say is that it needs to be required because we always want to have an image so pipe then we want mimes or memes i actually don't know how you pronounce it the right way in english so correct me if i'm wrong but this basically means the image extensions that we're going to allow so let's say jpeg comma png comma jp eg and we could add one more rule and that's the max you need to be aware that the max needs to be in kilobytes so let's say 50 48. i know that in the last video i showed you how you could remove pieces of validation that you do on multiple places but for the sake of this video i want to keep my validation right here before we continue on let's actually add our name which is a required field as well we have our founded which is a required field pipe integer pipe the minimum value is 0 and the max is 2021 and we're basically repeating ourselves right here but don't worry we have our description which is a required field as well so what we're eventually going to do right here is returning an instance of symphony's upload file and that might actually be something that really doesn't make sense if you're a beginner of laravel or symphony's framework but laravel's framework is based on symfony's framework right now we're using symfony's upload file class which will allow you to easily inspect a manipulated file now there's an entire list of methods that we could use and i want to show them to you so let's go right above our validate let's say methods we can use on the request and to show it to you let's create a new variable called test and let's set it equal to the request file we're getting the file from input name and right behind our file we're going to add our methods and i will add them as a comment as well the first one is guess extension so right after our file let's say guess extension so right below our variable or while the line of code that we have let's dd variable test let's save it let's go to chrome refresh the page and this will show us the extension of our image and in our case it's jpeg we could also get the mime type or the meme type again so let's say get mime type let me copy it replace it save it refresh the browser and as you can see this will tell you what type it is so whenever it's a document it will say it's a document image well what we have right now an image or whatever so we have a couple store methods so let's say the normal store method the s store method and the store publicly publicly and we will be using the store method in a moment so i won't cover it right now but these are basically different type of store methods that we could perform now there's another method which is called the move method and we will be using this one in a second as well so the next method is the get client original name and what we eventually want to do is to allow an image to be uploaded with whatever name and then we need to change that name to something unique and that can be done with a get client original name so let's replace it save it and refresh the browser and you can see the current image name that we have we could get the entire meme type so get client mime type replace it refresh the browser and this is pretty much the same as the guest extension it will say that it's an image jpeg to only get the extension without the file name and without the dot we could say guess client extension copy it replace it refresh it and the output is jpeg we could get the size of our image with the get size method typo get size replace it refresh the browser and this is the current size in kilobytes and there are a couple methods to validate so we could basically see if there is an arrow with the get error method let's replace it refresh it and the output is zero so whenever you upload a file that does not have the right extension it will return true or false and in our case we're uploading an image so the output needs to be zero because it's correct and the last method that we have is the is valid method replace it refresh the browser and this will also return true or false if it's the right extension or not all right now that i've showed you all the methods that we could use we're ready to create our application so let's get rid of our dd and our variable and right after all of our checks that we're done we could create a new variable so let's say new image name we need to create a new name if you're going to work on large applications you don't want people to add an image with the same exact name now to fix this we could do a lot of different things and i'm not saying that i'm showing you the best way but i think this is the most favorite way we could set our variable equal to the time method concatenate a dash so in single quotes a dash concatenate the current name that has been entered inside our input field concatenate it with a dot because we're going to add the extension after so let's say dot request image extension so what we're doing is adding the time dash the current name that has been added with the image followed by the extension now once again before we continue on let's dd it once so new image name save it chrome refresh the page and the current name is a timestamp dash mercedes.jpg and to be honest this looks kind of decent so what is the next step well now that we have a new name we need to store the image and this can be done in various ways you can store it inside the storage what i want to do is to keep it pretty simple and store it in the public folder so right after our variable new name new image name excuse me let's say request image and right now we need to move it like i just said the move method so this takes two params the first one is the location so let's say public underscore path which is a method which goes right inside our public directory and it will search for inside single quotes a folder called images which we don't have yet but we will create it in a second now the second param is the image name and like we have right here it's called new image name so let's paste it right here let's save it and let's create our folder first so let's scroll down to our public directory let's create a new folder called images and once again this may sound silly be aware that the name that we're passing inside our public underscore path method needs to be equal to the folder name inside our public directory so before we test it out let's debug it one more time to see what's inside this line of code let's set it equal to test now let's change our dd to test save it go to the browser refresh it and you can see a lot of information right here and it's just basic stuff it's the current path of our images directory the file name the extension the real path so the path from our users directory and some other non-informational stuff so this is it let's remove our dd let's get rid of our variable save it refresh the browser and whoops we're getting an error message right there so let's see what's going on whoa i made a typo right here refresh it so how do we fix this well that can be done inside our model so let's go up let's open our models and car.php and as you can see we have a protected fillable so we're saying what needs to be filled but our image underscore path is not in here so let's add it let's say image underscore path all right save it go to our controller because the last thing that we need to do is to add it in our create method as you can see right here we have our name founded but we haven't added our image let's say image underscore path equal to well we're not going to grab it from the request but we just want to pass in the new image name variable save it go to the browser refresh it and our mercedes car have been added now what about the image if we go to our database select everything from cars again you can see that it has been indeed added to our database so where is it stored in the ui well we created the images folder so let's go to our public directory right here excuse me images now let's open the image and it has been added so let's close it off now the last thing that we need to do is to print it in our ui so you know you could either print it out right here but i think it looks better if you print it out on the mercedes page so what we could do is to go to our ui so the show.blade.php right above our name let's create an image tag let me align it on the line below give it a source of larval let's open the asset which will look inside the public directory now let's add the path of images forward slash concatenate our car access operator image underscore path save it and let's test it out in the output and our image has been added so let's give it a class to style it a little bit so let's set the class equal to w dash 40 so width 40 margin bottom is 8 shadow dash excel save it refresh the page and this does not look good well let's change it to 10 forward slash 12. save it refresh it and it could be a little bit smaller so let's say 8 12. refresh it the ui really doesn't matter so i'll keep it like this this was it for this video where i showed you how you could create a simple image upload in laravel in my opinion there's way more validation that you could do but the basic validation of laravel is awesome to start with if you do like my videos and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button i know that during this course we talked a lot about artisan before i show you a couple other important commands that we could use i want to look in the root folder of our application because you might have noticed that there's a file in here called artisan so let's open it and this is the file that you're calling with the php artisan command what you're basically doing is passing the file into php so it could be parsed everything after the php artisan command is just passed into artisan as an argument now for the people that have worked with php symphony framework artisan is a layer on top of the symphony console component if you are familiar with writing symphony console commands you should feel comfortable with php artisan there's also a possibility to get a list of all available artisan commands to do that let's hop to our cli and let me zoom in alright and here we can run php artisan list from the project root and as you can see right here we're getting an entire list of artisan commands that we could use honestly there's not enough space and time here to cover all of the commands as you can see there are so many but we'll cover some of them what i have done is separating some basic commands that might be useful with some grouped commands i want to start off with the basic commands so in our cli we could write down php artisan clear dash compiled now what this command will do is removing laravel's compiled class file let's run it and if you do not know what that is well it's like an internal larval cache run now the second one is the down and up method so php artisan down and this is used to put your application in maintenance mode in order for you to fix an error run migrations or whatever else and if you want to restore an application from maintenance mode use the php artisan up method as you all might know we have a emv file where we store the environment variables that we have in our application with the php artisan emv command we can see the current application environment and right now it's local because we're running it locally now the command that i use a lot and will help you in the future as well is php artisan help as the name suggests it will provide a global help and a help for a command and right now you can see a couple options so let's say php artisan dash dash version and we get a version number php artisan dash dash emv and we're getting everything that we could use from the emv now the next command is the php artisan optimize command and this will clear and refresh the configuration and route files these are the basic artisan commands that you need to know next to the controller migration and model commands because you do need these when you work on a real-life project beside the basic commands there's also a list of group commands and we won't cover them all but i'll cover the most important ones if you have worked with php before you must have run into clearing your cash once in your life in laravel you could do that using the php artisan cache colon clear command and right now you can see that our application cache has been cleared so whenever your css isn't loading you know what to do right now in the next video we're going to create a complete login system but when we do that there will be a column in our database with a password reset token let me show that to you let's go to mysql select everything from users let's say desk users and right here you can see our remember token but you don't always want your remember token to be saved in the database what we could do is to clear these with an artisan command instead of using the delete from method in mysql so what you need to do is to run php artisan art colon clear dash resets and this will remove all expired reset tokens that you have in your database as you can see it runs right now because laravel will automatically create a users table in our database even if you don't did it yourself but we have no data so nothing has been deleted we can generate a random application encryption code which will be stored in our emv file with the php artisan key colon generate command and right now you can see that the application key has been set successfully so let's go to the code let's open our emv file and you can see our app underscore key which is an encrypted token whenever you have an application that needs to use sessions you can create it manually in the code but you could also do that with artisan so in the cli let's write down php artisan session colon table and as you can see this will create a migration for us so let's go to our database directory migrations and it's the last one obviously and right here you can see a new schema with the name sessions that will store our users information regarding the session laravel's view rendering engine automatically caches your view usually it does a pretty good job of handling its own cache validation but if you ever notice that it's not working you need to run a new command called php artisan view colon clear to cache it and as you can see the compiled views have been cleared in many programming languages setting up a basic user authentication system with registration login sessions password resets and access permissions consumes a lot of time laravel found a way to make authentication very easy to use and to understand whether you have a freshly installed larval project or the project that we're running right now laravel projects always have a create user table migration and a user model there are a lot of different ways how you could pull in a login and register system ui this can be done through bootstrap tailwind and way more and since we use tailwind in this course let's go to the cli and write down php artisan ui so create me a ui of tailwind css space double dash art of authentication let's hit enter and this will create the ui for us and as you can see please run npm to compile your fresh scaffolding so let's copy it paste it right here and this might take a second so we'll edit it out all right as you can see npm is running so let's open a new tab let me zoom in let's go to the desktop workspace cars all right and we will get to the ui in a second but first i want to start off with the user model and user migration now for the migration let's open the database folder migrations and let's see which one it is it is the create users table so the first one now in here you can see that we're creating our table users with an id name a unique email the timestamp where the email has been verified a password the remember token and timestamps this covers everything you need to handle basic user authentication in most applications right now like i said the first migration that larva automatically created for us is the users table it also has a model called user so let's open our models let's open user.php now in here you can see a bit more code than we had previously or what we have created before and honestly it's a little bit more complex since we've talked about the protected fillable we're saying that the name email and password needs to be fillable and the password and remember token needs to be hidden before we continue on i want to make sure that you guys know what the difference is between authentication and authorization since it's something that could confuse you now whenever you want to authenticate you're basically verifying what someone is and then you will allow them to act as a person inside your application think about the login and log out process it allows the user to identify themselves during the time they use your application now authorization means that you're going to determine whether the authenticated user is allowed so authorized to perform specific behavior in your application a good example might be adding an access type to your application which makes a user enter specific pages on your application if we go back to our user model we can obviously conclude that this data will be picked up from the users table the name email and password needs to be filled in and the password and remember token properties are excluded when you output the data as json which is good right because you don't want to show your password and you remember token so let's scroll up what is this authenticable that's extended by the user class so let's command click on it and if you're new to coding you might think that this isn't very important but it actually is this file implements three different files the first one is the authentic cable contract requires important methods that allows the framework to authenticate instances of this model to the authentication system now if we scroll back up open the user php now if we click on authorizable contract you can see a method right here named can which allows the framework to authorize instances of this model for their access permission in different contexts and finally we have the can research password contract and if we open that one as well you can see two methods the get email for password reset and the send password reset notification this allows the framework to reset the password of an entity that satisfies this contract now since we have required the authentication ui let's open it right now and see what just happened first let's close off these pages and let me scroll up i don't need a vendor anymore and we don't need the database but let's open routes and the web.php file if we go to the cli first and write down php artisan route list to see all the routes i made a typo route collin list excuse me now let me zoom out you still can't see the login register and all the other routes this is happening because we need to clear our routes and optimize it once so let's write down php artisan route colon clear to clear the route and then let's perform php artisan optimize and if we perform php artisan route colon list one more time you can see all the routes that has been created for us including the login register password reset and way more let me zoom out alright this looks better i won't go over all these routes but most of them make sense if you followed my php course up until this point so if we go back to our web.php file you can see that we use odd column column routes and this is basically the same as route control and resource this bundles up all predefined routes so you don't need to define them manually again and a new get route has been created for us right here which will require the home controller class but we will cover these in a bit if we go to the browser and remove the endpoint you can see that we've been redirected to a new login page and that has been done with a total of three lines of code in our ui and we didn't even created this login page this is pretty cool isn't it you might wonder how this process work it all happens in the authentication namespace controllers so let's go to our controller let's close off the model http controllers and you can see a new folder called art let's open it and let me make it a little bit bigger all right right here you can see a confirmed password forgot password login register reset password and a verification controller i want to go over these files to show you what actually happens behind the scenes and first off i want to start with the register controller in here you can see some basic uses so the controller register uses the hash and the validator and the class register controller extends the default controller it has a property or better called a trait called register user and then it has a redirect so a protected redirect to the route service provider colin collin home and this will define where users will be redirected to after the registration if you want to change it press command and click on home and it will be redirected to a constant where you could change the values but we won't do it right now let's open the register controller again now if we scroll down a little bit more you can see your constructor which i won't cover right now because it's the middleware and we haven't talked about that yet but we have two methods a validator and a create method the validator method accepts a data array as you can see with the data from the input fields then it will validate it as we did a couple videos back right here and then it will make a new user now there's also a create method which defines how to create a new user based on the incoming registration so right here you will hash the current password right here now for the login controller let's open it you obviously know that it will allow users to log in and i think it pretty much speaks for itself right it will bring in the authentication users so a trade same as in the register controller and it has a redirect to home again and it has its own middleware we also have the reset which will simply pull in the reset password straight right here that provides validation and access to basic password reset views a password broker class of larval will be pulled in to handle sending password reset emails we have a forgot password controller that simply pulls in the send password reset email freight and it will show the password email form we have a verification controller that will pull in the verified email trade right here it will verify the emails and it will sign up users so up until this point we talked about migrations models controllers and routes and most of these were also created within the cli so what about the actual ui since laravel 5.2 laravel provides an authentication scaffold for us that gives you a skeleton code and well i actually showed it to you when we entered our ui because this is completely new to us and the cool thing about it is the fact that it takes care of the authentication in the web.php file or the art excuse me right here it will create the routes for us so we don't need to do anything so if we go to our app that blades the php inside our resources views layouts you can see a lot of new code that we didn't wrote ourselves but the most important one is right inside of the nav right here this part right here we have an if statement well first we have a guest so whenever a user is not logged in or not registered show a login button and a register button else print out the name and create a logout button now i don't want this video to be me talking all the time so i want to code a little bit what my goal eventually is is well let's go to forward slash cars as an endpoint is to remove the add new car whenever a user is not logged in and if a user logs in it can edit and delete its own post so not someone else's this can be done with the user interface so let's go to our contours index.blade.php and before we write any code let's write down or dd our odd column colon user save it go to google chrome refresh it and right now it's no so there is no data of a user so what we could do is to go to forward slash register create a new user so let's say code with dory info at nice and let me add my password let's register a user and as you can see we're logged in right now because the endpoint is forward slash home so this is the redirect to in every controller that we have if we go to forward slash cars we still have our dd and we have a lot of information regarding the user so now we're able to tell the application if it should allow users what type of button needs to be available for them so let's create that so let's rewind for one second in order to show the edit and delete button of a specific post we need to make sure that we add a user id inside the cars table because if we go to our mysql right now let's write down select all from cars i mean desk cars you can see that we have nothing which links the users table to our cars table so let's go to the other tab because we need to create a new migration called php artisan and let me zoom in a little bit all right make me a migration called add underscore user underscore id underscore 2 underscore cars underscore table enter our migration has been created for us so let's go and open it migrations it's the last one and in here we only need to add a new table which is an integer and the name is user underscore id let's also add the down method so table drop column with the name user underscore id right now we are ready to migrate it so save it i turn php artisan migrate all right now we need to somehow add our user id inside the create a car method so let's do that let's open the controller so the cars controller let's go to the store method and right below image pad let's say that we want to have the user underscore id and then we want to pull in the art user and a specific id save it but before we test it out we need to add a user underscore id insider fillable in their model let's open the car.php and in the fillable right here let's add user underscore id save it let's go to google chrome well let's remove our dd first save it refresh the page we're logged in and the add a new car button is still available but if we log out and go to forward slash cars add a new car is still available so let's go to our ui and let's say right above our div create an if statement because we're ready to show the add a new car button for users that are logged in so what we want to check is if aud user has been set if it is copy the entire div paste it right inside of the if statement now let me align it save it google chrome refresh it and the button is gone so let's login all right login go to forward slash cars and add a new car button has been added now we could add an else statement right here but it's optional so let's say add else on the line below paragraph please login to add a new car let's give our paragraphic class of py-12 and it's italic save it go to google chrome well please log in to add a new car will not be visible because we're logged in right now and i don't want to log out because if we add a new car so choose the file i've got an image right there the brand name is audi or well this is mercedes 1918 description this is my mercedes submit it and you can see that mercedes has been added but if we log out right now go to forward slash cars you can see the edit and delete button still available so to change this we need to somehow check for the user id of the aud and the user id inside the cars table that we just added so let's go to our code scroll down right inside of our loop below the div class margin auto create a new if statement where we want to check if it is set so our odd column column user id has been set and we want to see if the odd column colon user id is equal to car user underscore id so the user that has been set needs to be equal to this specific car of the user id so let's copy the entire div paste it inside the if statement let's align it again save it go to chrome refresh it and the buttons are gone let's log in one more time let's change the endpoint to cars and the edit and delete button are still available now the last thing that i want to show you is how we could redirect a user after accessing some page that they are not allowed to from the url well let's log out for one more time let's go to forward slash cars but if we add forward slash create to it we're still able to create a car so how are we going to block this there are many different ways how you could do that we could set it inside the web.php we could do it inside controllers but what i like to do is to use middleware and i know that i really haven't talked about the middleware but in this case it's so important and it's so much better than all the different methods that i want to show it to you so let's go to our cars controller right here right above our public index let's create a constructor so public function double underscore construct and what we want to say is that this middleware in here we need to pass in two params the first one is called odd in single quotes comma array in here we want to say accept so the methods that we do want to show people or users is another array and in here we need to pass in the methods so we want to show a user the index page without being logged in and the show page so we don't want to show them the store page or the create page save it google chrome refresh the page and we have been redirected to the login page so let's try to go to cars again we can access the show method so number 23 and if we log in for the last time you can see that we are allowed to enter the cars forward slash create all right this was it for this video we covered most of the authorization and authentication in laravel and if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button
Info
Channel: Code With Dary
Views: 558,822
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel 8 tutorial for beginners, learn laravel for beginners, laravel full course, laravel tutorial youtube, how to learn laravel, laravel tutorials from scratch to advanced, complete laravel 8 course, learn laravel 8, complete laravel, laravel from scratch, laravel code, learn laravel code, advance laravel course, Complete Laravel Tutorial, laravel scratch to advance, laravel tutorial, laravel complete developer course, laravel 8 for beginners, laravel8
Id: 376vZ1wNYPA
Channel Id: undefined
Length: 386min 25sec (23185 seconds)
Published: Fri Feb 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.