LARAVEL essentials you need to know in 45 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey welcome back to the channel or welcome to the channel if you're new here this is a very different style of video I've never done this before so this video is about an hour long and it's going to show you pretty much everything you need to know to get started with laravel so if you've never heard of laravel or anything like that it's essentially a PHP framework which makes it so much easier to build web applications so I use this where I work I use it on all my projects it basically takes out all of the tedious stuff and makes building products actually quite fun because things like databases templating all of that is handled for you you just feed in some stuff and it feeds it out it's how I can describe it really it just kind of works it makes it so much quicker it gets rid of all the tedious stuff so if they interest you if you want to build products you want to build them quickly and you want them to scale and just work nicely laravel is the framework if you're using PHP so in this tutorial you'll learn how to build this and it's nothing fancy I will admit it's just like a Twitter style thing really bad twit so Stella thing shows a bunch of posts you click view you can view the post you can create new posts like this and then it adds it to the list I admit this is not this is this is not pretty this is not why I want to sell this tutorial one you can make so much better products or so much better applications after following this but this is a purely back-end tutorial I might I've always thought about this I might do a full thing where I teach you front-end and back-end in one so building a product from start to scratch from start to finish but this is purely laravel it's going to go through all the concepts of MVC being modal view controller it's going to show you some what sort of the best practices and things it's just going to show you how to get started with laravel and then from this tutorial you can go onto something more advanced you can build something yourself you can read the docs pretty much just like that but anyway I'm gonna stop talking and I'm gonna carry on talking at the next clip where we're actually going to start learning some stuff so before we can get started we need to actually grab a copy of laravel so think of this like the base files or the dependencies that you need to use laravel so we're going to use a package manager called composer and this is one of the more popular PHP package managers so effectively it just lets you download and keep up to date your PHP packages you might have used something like nodejs NPM or something like that but for PHP the common one is composer so to get started I'm going to go to laravel calm and just go to the installation page it's the first page in the documentation and for the latest version right now Lara laravel 5.6 the version of PHP that you need is seven point one point three so you need to get this installed on your computer if you don't already have it so that you can access it from the command line I'm not going to show that in this tutorial otherwise we're going off-topic but I'm sure you can just sort of google around on Mac this is quite simple on Windows it's a bit err but I'm sure you'll figure it out just do a bit of a google search for your platform so once you've got that out of the way we need to actually install composer now I've done a tutorial on this but it's Mac specific but it's very similar for Windows so I'll put a link to that in the description and it'll be on your screen right here now but otherwise you can just google it as well it's pretty simple to install if you can get PHP installed you can install composer as well so once your composer there is a little err I always use this command here so composer create projects and then laravel slash laravel and then the name of your project so that is sort of the string the command that you use to create a composer projects pretty simple stuff so I'm gonna open up the terminal so my text editor of choice is Visual Studio code I really recommend it I've come from phpstorm moved over to this because it's free and it's also quite good I might do a whole video and a kind of tools that I use some other time but I don't wanna go off-topic so that has a integrated terminal or you can just use the terminal like this but I just use the integrated one so I'm gonna paste that come on in there and I'm gonna call my project message board now I'm not really sure what this project is going to be just yet so the bit you watched at the beginning I sort of chorded afterwards but I'm kind of just making this up as I go along so what that's doing now is pulling all of the dependencies from packages you will see that laravel has tons of them most of them are from like Symphony Oh grab all the packages hook it all up and set up your project for you all right that didn't take too long so now in my message board folder I've got a ton of files these are all like the base files for laravel the important moment that we need first is the Dorian V file this is the environment file and effectively if you've never seen this before it's like a settings file except it's kind of special in the fact that this does not go into version controls so if you're using git which I recommend if you're using like github or bitbucket or something to collaborate on this you do not sync DMV it's in the git ignore so it doesn't go into the DMV and the reason for this is because it's very specific to this environment so this is my development environment so I've got apt debug it was true I'm gonna have my local database settings and then in my production environment I'm gonna have a separate door env file with my production database I'm gonna an app debug equals false I'm gonna have the app URL to something else so whatever comm etc etc so it's really important to keep these separate never ever ever sync these in your version control what you can do though if you want to sync in version control is have the dot env example edit this file because this does go into version control if you've got like some default settings maybe you got a bunch of API keys you want to share with your team put them in the example and then get your teammates to rename this file and put their own settings in I hope that makes sense but probably doesn't matter for most of you anyways don't you envy not example but unity we need to set our database settings so my one is called YouTube I believe and my username is root password root the default port for my sequel is 3 306 but my one in my case is 8 8 8 9 this depends on how you're hosting your database and what port you're on so I'm using MAMP which is gone on to my other screen I'm using mount for my mice server and my port is eight eight eight nine and then my apache port would be 8080 but i'm not going to use this for apache I'll explain in a second alright so now we've got the database hooked up we can actually start the server so if you head back into the console laravel has its own built-in server for testing only this is never for production so here you can use the command PHP space artisan which is pretty much just laravel as command line stuff so all of your commands that we like make controllers and all of your sort of scaffolding commands will be in artisan but there's also artisan serve not server serve and something I've just realised is that my laravel installation is inside of this folder so there's a folder inside a folder so we need to CD into message board message board yep like that and now we're in that folder so now we can go PHP artisan serve and Autzen serve artisan is actually this file right here which just loads up all of the PHP stuff cool so now we can go ahead in our browser and launch localhost 8080 website I'm probably not done yet unless that's your thing but that's what we're going to do now so the first file I want to introduce you to is a file called the routes file and nowadays this is actually called web dot PHP and this can be found inside of the routes folder and then web dot PHP in sort of version 5.2 and older it was in a file called routes dot PHP but now we've separate the mind to web console channels API blah blah blah now this is essentially where you put in a very very vague explanation a list of pages and I will get heat for that because that's not what it is at all but in a very basic sense I could put a list of pages here so I could put root care about and then inside of these inside of the function is what you do on those routes so slash about I'm going to go echo hello sure so now I want to go to slash about up here about we get so basically we define all of the URLs all of the routes as they're called and what to do with them so we can also have routes post not like that post on its own and this would handle a post request and I've also got get for a get request so a get request is just you sort of navigating to a page a post is usually when you submit a form or if you're using an API or something like that but we're really not getting into that that's way advanced anyway so I'm gonna get rid of this because we don't need it for now and this leads me perfectly into views now this is laravel sort of templating system so we have a view here so we're returning a view called welcome a view is literally just the HTML templates and these are used for displaying web pages for emails and probably some other stuff that I can't think of and these are stored in the folder called resources views and then you've got the name of the view so here is welcome blade PHP and that blade file is exactly what you see when you go to the home page so effectively here you go to the root directory and then we return a view or a HTML file which is welcomed up blade and here is all the HTML now your notice in here there's bits of PHP with some funny syntax so basically we can echo out any sort of function or variable or something like that using these curly braces so let's say on the page I want to echo out so here on laravel I'm gonna echo out the date so you don't need to put the word echo because the curly braces are saying this is already an echo so that makes sense so I'm gonna go out the date year-month-day H : I : s and that will give you the date and time so you'll see here I've got 20 1806 30 17 15 45 that is the current date so that's all done dynamically there's no sort of hard coding there if you want it sort of inject a bit of PHP use curly braces you can also do some PHP sort of structural stuff or conditions and loops and stuff using this @ symbol so I can have if as if let's go one equals one because I can't think of anything else R then at end if and here I'll go yes there we go and you can also stick and elsif or an else and stuff like that so else no and obviously it's never going to be no because as far as I'm concerned that's always true we can also do stuff like loops so we can go for each which is a common one and usually before reaching through maybe some database rows or something like that but I'm just going to put an inline array so I'm going to go 1 2 & 3 as dollar number so you see proper PHP syntax going on here the only difference is this @ symbol and I'm gonna go end for each and for each then inside here we can also use braces to sort of echo something so I'm going to go dollar number and here you'll see one two three done in a loop obviously this is not what you'd ever do ever but just an example so that's kind of a quick introduction to the routes file and blade templates which are two really quite major things in laravel the next thing is controllers now our controller is pretty much where all of your PHP logic happens this is where you do anything so your blade templates or for anything to do with echoing or displaying data and your controllers are for manipulating data or doing any kind of processes any action stuff like that so that's the next part of it so I'm going to go back into the terminal and for this you need to stop your server the command we want is PHP artisan make : controller and this does pretty much what you expected to PHP artisan just runs the command line stuff make controller generates a controller so it generates the classes for us so the name of our controller is going to be a home controller and this controller is going to handle everything to do with displaying the home page now the home page isn't going to be much but we're going to put in a controller anyway just how to do that for the future so run the server again and we can find this file homepage controller oh sorry home controller and that is in a folder called app HTTP controllers and then there's home controller you can also sort these into individual folders when your project gets big but there's no point in that for now so generated a very basic controller it extends controller because that is sort of the base of this controller which has a bunch of base code but in here we can create a bunch of methods so I want to create a public function of index which is going to handle loading the index page or loading the home page and we're simply going to do a return view of welcome so we're going to display out that welcome page and we're going to totally get rid of this code because now we're going to do it the sort of more elegant way using controllers so we're gonna go for another route so this is the route object get and the first option is still the actual route itself so we're going to go for slash and then the next one we're gonna put in quotes instead of an inline function and here we'll put the actual name of the controller so home controller it might seem a bit weird we're doing this in quotes but laravel interprets the string finds the correct controller and the correct method and then handles it that way you're not directly passing the objects in here if that didn't make sense to you don't worry about it just stick with it and then followed by an @ symbol and then we're going to use the name of the function so in this case index so false gone well now we should be able to refresh this page and there's our welcome page that's right here the one we edited a minute ago and you might be thinking well what was the point in that because we just had it in three lines in one page versus all of this it doesn't really make sense now because we're only doing one thing but think about if we were doing a bunch of maths or something algorithmic or we had to actually process data it would make a lot more sense to have it separated into separate controllers I've seen projects where even my own projects from back when I started laravel where I've got like three thousand lines in this web dot PHP because I didn't know any better but the proper way to do it would be to separate things out into controllers alright now that that's out of the way the next part is models so you can think of models as being like sort of tangible objects although that's a really bad sort of explanation because they're not always think of this like database tables because usually they are linked to a database table so you might have a table called message it so you create a model called message you might have a table called users so you create a model called user si Maya models for use our messages shop I don't know just stuff like that so we're going to create a model and you guessed it we're going back into the console and we're going to stop the server by the way I stopped the server with ctrl + C that's the same for Windows and Mac here appear to be artists and make : model and the model is going to be called message you'll notice with the controller I went for the naming scheme of home controller and for models I went for message and not message model it's just a laravel convention I guess they put controller after controllers you don't have to it's just a thing I then models you just call like message start the server again PHP artisan serves that we can test in a moment and then search for your file called message dot PHP and you'll see that's an app message dot PHP again you can sort is into folders I always do because it makes it much neater laravel ships with a user folder which is for if you want to do authentication stuff I'm not sure if we're going to get into that in this episode but that's also a thing there as well now before we can really get started with models the thing you need to understand is that this model is linked to well it's going to be linked to a database table now laravel has a very special way of creating database tables you might normally just go into your my sequel etre or whatever go in there create your table sort of the I don't want to say the old-fashioned way but mmm you might do it that way by the issue here is that if you work in a team or you want to put this on github or something as an open source project you had to like ship the database tables with it the best analogy I can give here is if you've ever downloaded WordPress one I feel sorry for you but two it ships with an installer which installs all of the tables you don't you have to create all the tables and all the table rows and all of that yourself it does it all for you so this is pretty much the same as that just slightly less elegant but more developer focused so we can create a migration back in the terminal as you expected with PHP artisan make migration and we give this a name and the naming scheme completely changes here so we're gonna have create underscore messages underscore table and this is going to be a script to build up the to build up the table so I want to run that and now we've created the migration and we can find that by just doing a search for create underscore messages table and you'll see it's got a time stamp at the beginning of it that's just how they get generated not sure why and there's two functions in here this up and this down basically up is create the table down is delete disabled because up is sort of running the migration and down is literally right here reversing the migration so let's say you go I don't want that you just reverse it you roll it back all sort it so function up this is the one we're really interested in but we'll do both of them anyway this is setting up the database table schema and if you head over to the laravel Docs you'll see a really good example of migration structure in fact the whole of the laravel Docs is really good it's all really well laid out the kind of putting me out of a job here but we'll give it a go anyway so I'm pretty much going to copy their example here so schema hopefully spelt right create and then that is a method so the first one here is the name of the table so in our cases messages and the name of the table is quite important I'll show you why in a moment don't get sidetracked here second one is a function which is where actual going to build up the table and the function you pass in an object blueprint and then dollar table so this is just passing in the table object and then in here we said on that table object we set up all of the different columns so we can go dollar table and the first one is called increments which is literally just an auto increment field so usually your ID and we're going to call this ID and there is some reasoning to this name one just general my sequel conventions and to laravel can recognize the table names and the table column names and then it does all sorts of cool stuff with the models I'll show you in a moment the next column we want is a string so just literally just string and this is going to be called title so the title of the message and the next one will be content and then followed by a function called timestamps laravel has these built-ins or recognized columns called created and updated up which are effectively a set of timestamps that will automatically handle for you without you needing to worry about I'm sure it's pretty obvious what they do just to date that the row was created at and the date it was last modified and for the down one usually this is always just schema schema colon colon drop and then the name of the table so in this case it is messages so if you roll it back you just run the migrate down and that will just delete the table so now we can go ahead and run in the integrator terminal we can go ahead and run PHP artisan migrate and what that's going to do is it's going to create all of the tables based on our migrations and you'll notice that there are three migrations here there's a create users table passwords reset table and messages table now these to ship with laravel they're parts of the laravel authentication system but they're not important to us because we're not going to be dealing with that today but essentially when I go back into navigate which is my my sequel program you can also use sequel pro or PHP myadmin whatever this is just the one I use you'll see a bunch of tables so messages password resets users and migrations migration stores a log of when the migrations are run if they're being run or not and then the others are the default tables and their messages is the one that we want so you'll notice that by including this migration file in version control other collaborators or open-source users or whatever can just run PHP artisan library and then they get set up with with all the tables alright awesome now we can finally use our models so we have a file now called message PHP and I don't think we're going to need to use this in our in this episode because laravel will automatically know which table name to use what the primary key is and what the timestamp fields are so what it does is it knows well this is called message with a capital M so it removes the capital M so now it's message adds the s on it and then it comes to the table name called messages it's quite smart it just sort of guesses almost where your table is and then it also knows ID is the primary key otherwise you'd have to set this up by going like protected dollar table I think is dollar table which sets the table name and you can not you can manually override it so for example I had a table called classes once which really clashes with everything in laravel so I'd to rename it and then you can manually go ahead and name your tables back to the actual controller so I'm going to create a view now which is going to be our home page and that is in I'm going to close out some tabs resources I'm going to get rid of the welcome one because we don't need it on a creative you called master blade PHP and this is as you can guess going to be the master template it's gonna have the header the footer and all the JavaScript stuff and it's all gonna be there so with the power of Emmet I can generate all of my HTML right there like that and right here is where our content is going to go so there's a bunch of blade sort of tags that you can use the one we're gonna use is yield and that lets us sort of get a section you'll see in a minute so yield content this is where we're gonna put the content and then we can also have in the title yield title and then we'll have another view another folder we'll have another file called home blade dot PHP and this is going to extend that template so out extends and then master double-a sorry.just master you don't need to have the blade PHP because it knows that it's always going to be blade PHP and then here you can define your section so section title and then the second parameter being the actual title so a home page a yes and then I'm going to have a section of content and this one I'm going to end out here so end section and if I put some content in here like a h1 content and we set up this controller to return the view of home so not the master but the home page which will extend the master and then I run over to the home page so I can't connect because I'm not running it in the terminal so no I'm not migrate PHP artisan where is it PHP artisan surf like you've always done cool now we get oh that's not good I extend looks like we've got a syntax error that needs to be extends as a plural go for again content good view page source you'll see that we've got the title is home page all of the HTML stuff is there and then we've filled in the content in the right place nice one so I'm gonna stick some head of stuff in here really basic stuff I'm not gonna bother bringing in bootstrap or anything like that because this is a purely back-end tutorial but you can if you want you just bring in your bootstrap here as you always would and then use your bootstrap tax so this is gonna be cool I'm just gonna call the site Neal Road because why not self brand it and the content is going to be the stuff all I needed home blade dot PHP so the home page is going to have a list of messages so we're going to go recent messages and we'll have a list in here an unordered list and this will be sort of all the messages so this is a this is a message for example it's going to display out just like this and we're going to echo out each message Donita but we're going to do this in a loop and before that because we're getting ahead of ourselves we need to actually deal with the database to grab all of those messages so in the home controller there's a nice little thing we can do so we can use message which grabs the message object and a vs code when I hit enter now it's going to add this line up here which pretty much just imports that object so we can use the method message all and that will grab every single row in that table so use this precaution and I'm going to set the variable message is equal to message or so that's going to grab a collection which is multiple rows it's going to grab a collection and store it in this variable as an object I could then as I normally would go for each dollar messages as message and then echo out the message and then arrow because this is an object title for example so you grab the column headings just like that by putting an arrow and then the column heading it's super nice and simple and I want to stick up a die on the end here just that we don't return the view and in my database we need to add a row here so title is super cool post I guess sure and lorem ipsum I'm not going to bother with the created and updated that for now but those are important refresh it and we get super cool post if we add another column sorry another row and not so cool post blah blah blah like so we get super cool posts and not so not so cool post all right so now you've done that directly in the PHP we're going to do that in the view now right now if I were to try and access that messages variable in here we wouldn't get anything we need to pass it into the view so it has a second attribute or parameter or whatever that it can take which is an array of values so when you set the array the keys are going to be the future variable name you'll see what I mean by that in a second so messages and then we pass in sorry messages and then we pass in the value that we want it to be so dollar messages which is here are there now in home dot blade PHP we can go at for each and this is pretty cool here dollar messages as dollar message so now we've actually passed in that variable and we can access it within the HTML so end for each then I'm going to stick some markup in here so Liu and then curly braces and then dollar message title and then I'm going just gonna put a dash there and then more braces dollar message what was the other column called I think is called content yep content and now watch this recent messages we're for reaching through it we're grabbing two different rows because those are all the rows and that's pretty much it so I guess this is starting to look like a really really rubbish Twitter if you sort of squint a little bit and lower your expectations so as part of Twitter you obviously get so the timestamps and and Twitter you see two hours ago or the actual day if it's long ago enough and while you're there you should you should follow me because even though I don't tweet a lot I really should and I need a bit more motivation to tweet so go ahead and follow him in there if you're still watching anyways going back to our original point I'm gonna put a timestamp in here and laravel makes this super simple so I'm gonna just sort of neaten this up a little bit in fact I'll remove the dash and put a break in instead so now it looks like that which still looks rubbish but sure I'll put strong in there or something that's kind of more readable still rubbish but sure we're gonna put the timestamp in there so I'm gonna stick another break in here break like that and more curly braces and we can use dollar message created out because that is the default layer of or timestamp dollar message created at and if I use that at the minute first Sony's actually put some timestamps in here because we don't have any so they and then sure good enough so if I do that right now we're going to get the sort of my sequel year-month-day H is which most people don't read so we can do it we can do this we can use formats now this is using the carbon package and is built into laravel so this is by default a carbon object so you can append format onto it to format it as a PHP date so we're going to go for D / M / y if you're American we do it the right way I'm sorry and there you go there's the date and you can also put the time on there as well so H : I there you go both seven o'clock exactly on different days you can also use one with a slightly funny name but I quite like it I think it's diff for humans and you might be thinking what who reads dates there's not human difference basically means that well it takes the difference between today and then and it puts it in a readable string so 58 minutes from now which is kind of odd / then the bust 1901 okay so the date is out on my PHP but that's fine 58 minutes from now because we are time travelers and the other one says one week ago so it sort of rounds it up after a point because I don't really care how many minutes ago it was I just care about roughly was it last week this week a month ago a year ago in the future whatever so that's kind of similar to what Twitter does when it puts date is close enough you could also just use the format as well to put the actual specific date Carbon is an entirely different subject that I want to get to sidetracked with it there's so much you can do with it it has its own website separate from laravel because it is its own project there's so much you can do with date manipulation and stuff like that it really just extends PHP is built in date object so do check that out when you get more into laravel anyways I think it's about time that we actually make this a bit more dynamic so we're going to create a form on here so a form and the action is going to be create so that is going to be where the form is going to submit to this is just normal HTML a method it's going to be post we're gonna have an input and this is going to be a type of text the name is going to be title and we'll give it a placeholder because I don't really feel like doing markup title and we can have another one which is going to be a textile and this one's going to be content placeholder content one last thing we need a button with a type of submit you can also use input type equals submit just depends what you do submit so now here is our form it's in the wrong place it should be above recent messages so I'm gonna have post a message and there's the form there so just a markup good alright and break this is really awful markup by the way this is like 1998 markup user framework or something but don't do this so obviously what you can expect to happen is you put your title in here you put your content in here you submit it and it shows up on the message board there's not really any kind of authentication or capture or spam prevention it's really that's not the point we're just gonna submit this field and that's going to show you how you can insert into a model so next thing we're gonna do is create a new route so I'm going to duplicate that one and this is going to be a post route because it's going to be when you submit the form and for this I'm going to create a new brand new controller called message yeah message controller PHP artisan make : controller this is giving message controller this is going to handle anything to do with messages but not the same thing as the model does this is going to handle creating messages deleting messages and maybe editing them something like that so now we've got a message controller which goes in the same place as home controller I'm going to create a function in here public function fun action create and as I say this is going to handle creating this message in the in the database so we can create a row based on that object so we're going to use the object and create a new instance of it so dollar message equals new message and again that's going to import it at the top just need to be aware of that if you see an error always check whether these are in there so if you see like undefined object message check that's in there it's usually that so we can set a few things here and message title is equal to you might be used to doing this in PHP so dollar underscore post title which is perfectly valid PHP but let's say that doesn't exist for some reason that's gonna throw an error because that that array key won't exist also if you for some reason turn this into a form that can be submitted by get and post that also won't work so we can use a little variable called request so in the actual when you're declaring the function the object is request again hit enter and that's going to load in that package or that object sorry and usually this is called request request and now we can grab anything related to the request so the user agent the post values to get values headers all that kind of stuff so dollar requests and you can go request title the longer way to do it is to go dollar request arrow inputs and then put the string there so title but this is the shorthand version which works with pretty much any name unless it's like a reserved name but usually you'll be fine with just this I just prefer it and the other one is content request content and then what we will do is throw a message safe and that's just going to save it to the database I've prepared the object and we're saving that object back to the database and after this we're going to do a return redirect which is like a global function that you can use and then you put the URL that you want to redirect to so in this case just slash and before I finish this I've just realized we already had this is the wrong request this gets kind of confusing you just got to get used to it I had those multiple things of the same name sometimes usually for laravel is usually the illuminate httprequest but it might not be so just keep an eye out for that but the laravel docks are really good at showing you which one you need and there's aa syntax error I've gone we can run the server again and now let's give this a go so title is going to be my new post test test test sure okay not found exception web dot PHP okay we didn't finish this off I think I've got sidetracked there the controller is message controller that's the one we just made and the function was creates you see that method here create so I want to refresh that submit it and still not found and again I'm being a bit stupid I forgot the root name here so what did we call that we called it slash create accessible so slash create try that one more time token ah this is the error that I was expecting token mismatch exception so laravel automatically has built in some CSRF protection this is a I believe it stands for cross-site request forgery I think effectively means let's say you're on some dodgy web site and it's got a form on there which you think is gonna post a tweet or a message or something like that but actually it's going to post to URL at your bank to transfer some money and that would work usually because the bank doesn't care who submits it it will just take that like gets a request you happen to be logged into your bank in another tab and it just goes off this is the user and then the submit sir and all your money's gone and you're a bit upset about that but what CSRF tokens do is it passes a set of characters in a separate form field and then the server at the other end verifies that it came from its own application and if it didn't it denies it it just blocks it and if it did it lets the sort of transaction happen so we can protect against CSRF protection built into laravel and that's what's causing this error we can protect against that using a CSRF CSRF token and because we don't have that token at the minute that's why we get in that arrow it's really nice and simple just put some braces in your form the function is CSRF field CSRF field which will echo out the hidden field so the user will never see it and I'll have a value with a token in it and the server remembers that token it knows to accept it so when you submit it with the token it will be happy I'll have that make sense so going back here now if I view the page source you will see that there is this input type with a name of underscore token with a I don't know what this is it's some kind of hashed value which the server will understand so now when I submit this my new post and whatever you see a posted and now it's here at the bottom of the list that's pretty neat okay one more thing I want to do is I want to have a view a view page where I can view an individual message I don't no way to have that but might as well add it in because it's going to show you some more fancy route stuff that we can do so I'm going to create a new route and again I'm duplicating it and this is going to be a forward slash message forward slash and it's going to be the message ID now this is quite cool you can go a forward slash message and then we're going to call it ID and here we're going to have the function being called view and what this is going to do this is a dynamic value so it knows that if it sees slash message the bit after it is the ID so in message control of view we can go public public function view and then the value we're going to pass in is dollar ID we don't need to deal with get variables or anything like that or the request request it's just going to pass in a dollar ID just here which is quite neat so to demonstrate this I'm just going to go echo dollar ID and now when I go to the forward slash view forward slash one doesn't exist whatever don't wrong sorry full of slash message I'm doing this video kind of raw which is why I'm leaving in the errors but these are all normal things so you see it's echoing out what's here so if I put that there you go so in the controller we're going to find this exact row based on the ID so we can do that using message so just finding one message equals message : : : find or fail right so this function there's a function would find built-in but find or fail will throw a 404 error if it doesn't find this row so if we put in something dodgy in the URL here it's going to throw a 404 because it doesn't exist which is exactly what we want to happen so it's going to go find or fail and in brackets we put the ID of the row we want so I'm going to go echo dollar message title just to demonstrate this for you so if I run forward slash one cannot use illuminate mail message as message because name is already in use again vs code is tries to import the wrong one if anyone knows a plug in for the S code that handles this nicely it's always important the wrong stuff we've already got message in there so that's flying super cool post and if I go number two not so called post all right again we can return a view now so a view and we're going to call this message I'm going to throw in the message variable I'm going to create a new view file I'm just going to duplicate the home fog to save ourselves a bit of time rename that to be message blade dot PHP and I support message wrong soon esas the title is going to be dollar message title and in the content I'm just going to go h3 not that h3 dollar message title and then these are going to be paragraph tags with the content Nero because that's in the master the title and then the the link sorry those content and then going back to the home page we could probably do some links so I want to go to the home blade PHP and we'll add a break tag and a link so a yeah let's do this way full slash message forward slash and curly braces dollar message ID and that's obviously gonna fill in the ID view click the links done pretty much it post in here done I know that was an hour long tutorial I think we just hit them our mark depends how is edited we've just hit the hour mark I know that's a lot of time for not a lot of content but you can expand this you can add some nice stars you can do whatever you want from here I think that pretty much covers everything you absolutely need to know to get into laravel there's so many more things laravel is such a massive project there's so many different things are so many different use cases follow follow the documentation if you want to see more like this whether it's different projects whether it's something else from laravel or whatever just let me know in the comments below tweet me whatever I'm really interested to find out if you didn't like this format let me know if you liked it let me know but other than that I'll see you next time I actually upload Cheers
Info
Channel: Neil Rowe
Views: 153,319
Rating: 4.9252186 out of 5
Keywords: coders, guide, tutorial, hd, 1080p, php, laravel, backend, development, developer, software engineer, neil, rowe, how to, beginner, essentials, getting started, models, controllers, blade template, web app
Id: ubfxi21M1vQ
Channel Id: undefined
Length: 45min 44sec (2744 seconds)
Published: Sun Jul 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.