[0:00] what is going on fellow developers my [0:02] name is telepot and in today's [0:03] video we're going to be creating a [0:05] laravel api [0:07] now you're going to need some [0:08] pre-requisites for this before getting [0:10] started [0:11] and the pre-requisites you [0:14] need is mysql and postman [0:18] now as you can see here this is postman [0:21] it allows you to make api requests or [0:23] restful a [0:24] restful request to an api [0:28] like this as you can see here we can do [0:29] a get animate one and as you can see [0:31] there [0:32] we get the i d the title and [0:35] if it's been watched or not um again you [0:38] can create a new anime we can put one up [0:40] and we can delete one from this api [0:42] so this is what postman could do but [0:44] you're also going to need some sort of [0:46] mysql database you can connect to [0:50] i'm actually cheating the system and [0:52] using mamp so mamp allows you to run [0:54] php applications using apache it also [0:57] installs mysql and when you start it [0:59] will start in mysql [1:00] server where you can access at localhost [1:04] um if you go into your settings here and [1:06] you go ports you can see your mysql port [1:08] here you can change it or set to [1:10] whatever you [1:11] think you need but as you can see that's [1:13] how you um [1:14] can set up using mamp um i'm not going [1:16] to show you how to download them simply [1:18] just google them and [1:20] find it there or check out one of my [1:21] other videos either getting started with [1:23] laravel or [1:25] my learn how to use php so without [1:28] further ado [1:29] let's get on with the video [1:32] today's video is brought to you by [1:34] cloudways a managed cloud hosting [1:36] platform for your php based applications [1:40] cloudways offers peace of mind and [1:42] flexibility to let you focus on growing [1:45] your business and reach new milestones [1:47] now with cloud wage [1:48] you get an optimized stack manage surf [1:51] and application [1:52] backups staging websites integrated [1:55] getting composer [1:56] and the choice of five cloud providers [1:58] like aws digitalocean [2:00] lynode and more so if you're an agency [2:03] or an e-commerce business and looking [2:05] for a managed solution [2:06] then go for cloudways without wasting a [2:09] moment [2:10] you can use the promo code tylerp [2:13] when signing up via the link in the [2:15] description to get 15 [2:17] free hosting credit and by the way i [2:20] really like their slogan [2:22] moving dreams forward [2:25] so the first thing we're going to need [2:27] to do is create a new [2:28] rfl project um so to do that we're going [2:32] to use composer [2:33] we're going to create a project and [2:36] we're going to call it well we're going [2:37] to [2:38] we need to use larafell and then we need [2:41] to give it a name so this one is going [2:42] to be [2:43] larafell hyphen api hyphen youtube i'm [2:46] going to hit [2:46] enter and that's going to create our [2:48] project so our project's now [2:50] completed already so to get started what [2:53] we're going to do [2:54] is we're going to run php [2:57] artisan make model [3:01] and then we're going to give the model a [3:02] name now we're gonna be creating an [3:04] anime [3:04] uh list like what my anime list what i [3:07] wanna watch sort of thing [3:09] api so we're gonna call this make model [3:11] anime um [3:12] and then we're gonna also do a hyphen [3:15] and i'm gonna say [3:16] uh we're to make a migrations and we're [3:19] going to make [3:19] the well it's just in migration so what [3:22] we're going to do is phps and make model [3:24] anime and the migrations which go with [3:26] this [3:27] we can actually do migrations i believe [3:30] um so let's hit enter could not find [3:32] file oh [3:33] sorry we need to actually cd into the [3:35] directory so let's cd [3:37] into the laravel api youtube [3:40] and then let's run that command again uh [3:43] just to let you know i made a mistake it [3:45] wasn't migrations it's migration [3:47] um and now that has done it's also [3:48] created our migration table [3:50] there so let's open this up with vs code [3:53] or whichever [3:54] text editor you prefer to use i'm going [3:55] to open this up [3:57] and let's close that uh [4:00] let's zoom out one just for now um we'll [4:03] zoom back in when we're done [4:04] and the first thing we want to do is go [4:05] into dot emp and we need to update our [4:08] database so we actually need to go and [4:10] create a database [4:11] so what we need to do is open up our [4:14] browser [4:16] if i can open it there we go and then we [4:19] want to go to [4:20] localhost forward slash php [4:23] my admin and that's if you're using mamp [4:25] if you're using saying else obviously go [4:27] to where for you stored your database [4:29] we want to create a new item here and [4:31] we're going to call this larafell [4:33] underscore api underscore youtube hit [4:36] create [4:37] and it will create us a new database so [4:40] now we need to take this name [4:42] go back into our dot emv file [4:45] and go larafell underscore api [4:48] underscore youtube and also the password [4:51] is root for map [4:52] um and it's root for map but also use [4:55] whatever database password and username [4:57] your [4:58] my sql setup uses so let's close the dot [5:01] emp file and actually head [5:03] into the app and models now actually if [5:07] we open this up [5:08] we have class anime extends model here [5:11] now we're going to have to make a couple [5:12] of changes to this i'm not going to do [5:14] them now because i want to show you the [5:15] errors that happen if you don't make the [5:16] changes here [5:17] um so let's actually go into our routes [5:22] uh our api.php and we're now going to [5:25] set up our first [5:26] route so what we need to do is go [5:29] route route get because we're going to [5:33] be making a get request and we're going [5:34] to get [5:34] from forward slash anime we're then [5:37] going to run a function [5:40] and inside this function we're just [5:41] going to return anime [5:44] all and what this is going to do it's [5:46] going to go to our anime model it's [5:48] going to know what [5:49] how to connect to it to the mysql it's [5:51] going to run a query [5:52] to get all of our anime items [5:56] inside of that array and it's going to [5:57] return it so what we need to do [5:59] is actually use um [6:03] app forward slash models [6:06] backlash anime also a lot of people [6:09] mentioned that that's just the incorrect [6:11] path because it's [6:12] it's a capital a where it's in the [6:14] perfect lower case when you [6:16] use something you need to capitalize the [6:18] first layer because if you don't it will [6:20] not find that model so let's save that [6:24] and now if we go and run this inside of [6:27] postman so if we bring postman [6:30] up here let's create a new [6:33] collection we're going to call this [6:35] anime list [6:37] youtube let's create let's click in here [6:40] and click add a new request we're going [6:42] to call this get [6:43] anime save and also [6:46] let's actually close all of these [6:50] there we go so let's open this one now [6:52] we need to actually surf this file here [6:54] so we need to press control option t [6:56] to open up our terminal and i'm going to [6:57] run php artisan [6:59] surf to run our um [7:02] our app says run there you go so now we [7:05] can see the link here [7:06] so let's copy that go back to postman [7:09] paste it in here [7:10] but now every so we did four slash anime [7:13] but [7:13] every single route will be prefixed with [7:17] forward slash api forward slash route [7:20] so we always need the api when we're [7:23] using this [7:24] and now if we hit send we can see [7:27] we're getting an error base table or few [7:30] not found [7:31] laravel underscore api underscore [7:34] youtube dot [7:35] animes and you're thinking to us but we [7:37] created anime [7:39] well there's actually two things we need [7:41] to do before this actually gonna work [7:43] so we need to go back to our app here [7:46] and we need to run [7:47] let's close this we need to run php [7:50] artisan migrate which will create our [7:54] anime table [7:55] so if we go to the [7:59] database migrations and click on the [8:02] anime table you can see what does [8:06] it creates a new animaze table [8:11] so what we need to do is go in here and [8:13] do [8:14] table we need to create a string [8:17] and in here we need to call this [8:20] uh title we don't need another table [8:23] which will have a boolean sorry not [8:27] create a new table use table and we'll [8:28] create a new boolean called watched [8:32] and hit save and now that way this is [8:36] going to be our structure for our [8:39] anime database so we actually need to [8:42] um create a so what we need to do is php [8:46] artisan migrate [8:49] rollback now what this will do is roll [8:52] back all the changes we made [8:54] and now because we've updated our [8:57] um our migration we can now run php [9:01] artisan migrate again [9:05] and this time it's going to create our [9:06] animes table but with the correct [9:08] structure let's close this [9:12] and let's go into our anime here and we [9:14] want to create a protected [9:16] sorry a protected table [9:20] and set this equal to the table name [9:22] which is going to be animes [9:24] let's open up our timeline again and [9:26] clear that and now run [9:28] php artisan surf [9:32] let that surf it let's go back [9:35] to our api and or to our postman and [9:38] let's send that request again [9:40] and there you go we get an empty array [9:42] so that's cool obviously notes come back [9:44] so we can't really tell if that's worked [9:46] or not [9:46] but we we aren't getting any errors so [9:48] the next step is actually to create a [9:51] new anime [9:52] so to do that let's create a new request [9:55] let's [9:55] copy this link here put this in here [9:58] anime and let's change this to a post [10:01] now what the post request will do will [10:04] allow us to [10:05] actually create a new anime so let's set [10:08] that route inside of our [10:10] um laravel application so i do say root [10:14] post to forward slash anime [10:18] i'm then going to run the function [10:22] and inside of this function we're going [10:24] to basically [10:25] check the request and what we're going [10:29] to do is we're going to say [10:30] just to record can we validate the [10:31] request [10:33] and the items we want to validate is the [10:35] title we want to make sure [10:36] we definitely have a title when we [10:40] create [10:40] an anime post we're then going to return [10:44] anime create [10:47] and then we're going to pass through a [10:49] title which is going to be our request [10:53] title and then we're going to pass [10:55] through watched [10:56] which will be our request oh [11:00] our request watched so what this means [11:05] is we're going to check if we're making [11:07] a request [11:08] so when we check the request we're going [11:10] to validate it to make sure [11:11] we definitely have a title if we don't [11:14] have a title [11:15] that means it's going to throw back an [11:17] error saying hey [11:18] you need a title then what we're going [11:20] to do is we're going to return [11:22] we're going to create a new anime we're [11:25] going to call title [11:26] request title watched request [11:30] watched so basically we're just getting [11:32] the title from our body [11:33] our json and the request from our json [11:36] so let's go back to postman quickly [11:39] and let's actually try and do this let's [11:41] go to our body [11:42] let's click raw and let's put in some [11:44] jason [11:45] now let's just pull us through a title [11:48] and call it [11:49] uh tokyo ghoul [11:53] and have we watched it let's say no so [11:56] we'll go pass through false [11:58] so let's try and send this request i'm [12:00] going to warn you this will throw an [12:02] error as you can see we are getting an [12:04] error now [12:05] it basically is not allowing us [12:08] to it's basically a security layer from [12:11] um [12:11] laravel to fix this we need to go into [12:14] our model [12:14] and now we need to create another [12:16] protected variable called [12:18] fillable and we've got to set this to an [12:20] array and [12:21] inside this array we need to give what [12:23] we're allowed to fill mass [12:25] fill which will be title and watched [12:28] there you go so now if we go back to [12:30] postman and try to run this again click [12:33] send [12:33] you can see it's now created so if we go [12:35] back to our get anime request [12:37] and send that request you can see we get [12:40] returned [12:41] a object with our tokyo ghoul anime [12:45] added [12:46] so let's now save this one and call this [12:48] one [12:50] create anime save to the list and there [12:53] you go we've got them saved in our [12:55] little list over here which is useful [12:57] so the next request we need to make is [12:59] actually updated let's say we've [13:01] actually watched tokyo ghoul [13:02] so let's now go back let's go into our [13:06] api [13:07] and let's create a new route call it [13:10] uh put for this route because we're [13:12] gonna be updating so we're gonna say [13:14] anime forward slash and now this one is [13:16] gonna look a bit different we're gonna [13:17] use brackets [13:18] and pass through the anime and this is [13:22] actually just going to be the id [13:24] but we need to give it the name of anime [13:26] or whatever matches [13:27] our um model here [13:31] so in here let's now type in function [13:36] anime dollar anime [13:41] and down here what we want to do is say [13:45] return dollar anime [13:49] update with the array [13:52] of title so we're gonna pass by qatar [13:55] which is request [13:57] title and our watched [14:01] which will be request [14:05] watched so there you go [14:08] now we actually only want to update our [14:10] watched element [14:12] but we can also update our title too but [14:14] let's remove this and just update our [14:16] watch element for now [14:19] so just to explain this so what we're [14:20] doing is we're making a put request [14:22] to anime forward slash anime which is [14:24] actually going to be the id so this [14:25] would be such as one [14:27] two three so let's just pipe in anime [14:30] here [14:30] and then we're going to take this name [14:31] and we're going to create a variable [14:33] outfit over on the right side which is [14:34] going to be [14:35] a model so what this is actually doing [14:38] to create this laravel is running [14:42] anime or sorry anime [14:47] find by id [14:50] and it's going to take an id and it's [14:52] basically returning it that way [14:54] so what's it basically it's just trying [14:56] to find an id [14:58] find an anime with the anime model with [15:01] the id [15:02] of whatever we pass through here and [15:04] it's now returning it into [15:05] a actual object we can use to update [15:09] so let's now go and test this inside of [15:12] postman so let's create a new request [15:14] let's just copy the route here let's [15:17] put it into a put and now let's pass [15:19] through an id so if we go back to get [15:21] anime you can see the id of tokyo goal [15:23] is one [15:24] so let's go in here and just paste in [15:27] the one [15:28] let's go and create a body of raw [15:31] type jason and let's just say watched [15:35] which is going to be set to troops we've [15:37] now watched it let's say it's sent [15:38] and you can see we get a one now one [15:40] means it has worked if we go back to get [15:42] anime and we resend this request [15:44] you can see watch is now returning with [15:46] one as well so one just means true [15:48] and that's what they're saying it's [15:50] basically saying success is equal to [15:52] true [15:53] and that is now the um update [15:56] anime root done now if we want to we can [16:00] go back here and we can also pass [16:02] through [16:03] a title and we could say request [16:06] title save that and go back [16:10] now if we make this request again you [16:13] can see we're going to get an error [16:14] because [16:14] title cannot be null so what we need to [16:17] do is in here [16:18] say title and pass through the title of [16:21] the anime [16:25] hit save click send and that comes back [16:28] with one again so if we go to get anime [16:30] refresh you can see it's just there so [16:31] let's just actually make a change let's [16:33] say tokyo ghoul [16:35] updated or we could say re send [16:40] re is one of the update or one of the [16:42] anime seasons [16:43] and there you go tokyo ghoul re we've [16:45] added it to a watch list [16:47] let's save that and there you go that is [16:50] now done now there's one last route we [16:51] have not covered and that is deleting an [16:53] anime so let's create a new one quickly [16:55] let's create one let's say [16:58] naruto watched true already so let's [17:01] create that [17:02] as you can see it's come through as true [17:04] so if we go to our get animate and we [17:06] sent [17:06] you see we get two items now we've got [17:08] one which toggle and naruto [17:11] let's say we we actually didn't want [17:13] tokyo ghoul in there at all [17:15] um we could now delete it so let's go [17:17] back here [17:18] let's go to our route let's create a [17:21] delete [17:22] and we're gonna say forward slash anime [17:25] forward slash [17:26] the same route as before so we're gonna [17:28] do the anime [17:30] we're gonna do function again [17:33] anime dollar anime [17:36] and now to delete this we're gonna [17:37] return i'm gonna say [17:39] anime delete [17:43] and it's as simple as that to set up a [17:46] delete request you just get the object [17:50] by using the id pass through the url [17:53] and then we delete it using this [17:55] function here so [17:56] save this let's go back let's create a [17:59] new [18:00] let's copy the route again [18:04] let's not let's do a delete [18:07] body we don't actually need a body for [18:10] this this time so we can actually just [18:11] run this [18:12] click send and as you can see it's [18:14] successful so it's come back as one [18:16] so if we save this as uh delete [18:20] anime save let's go to get anime and see [18:23] if tokyo ghoul is still there so let's [18:25] hit sent [18:25] and as you can see it's been taken away [18:27] it's been deleted now we just have [18:28] naruto and that is as simple as it is to [18:32] make an [18:32] apa and a an api [18:35] in laravel it's super basic super simple [18:39] and it works really well now in the next [18:43] video we're going to look [18:44] at actually creating some sort of [18:46] website or some sort of laravel a bigger [18:48] laravel project [18:49] um but for now i hope you've enjoyed [18:51] this video if you have don't forget to [18:52] leave a thumbs up [18:54] smash that subscribe button share it [18:56] with all your friends leave any comments [18:58] or feedback if you get stuck at any [19:00] point or you have any questions don't [19:01] forget to just drop it in the comments [19:02] below [19:03] and just as a quick shout out don't [19:04] forget to check out my twitter it's [19:06] in the description below and same with [19:08] my new discord surfer where [19:10] you can join us get involved with the [19:12] community and [19:13] if you need any help or anything like [19:14] that i will be there to try and help you [19:17] but anyway guys thank you for watching [19:19] this video i will see you in the next [19:21] one [19:21] peace out [19:24] [Music] [19:42] you