Creating a Laravel API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody welcome to uh digilation's webinar today in this webinar we're going to be talking about building a larevel api and if you've been following along with any of my personal previous webinars and tech talks here i've been doing a lot of api building headless cms type stuff i'm kind of on the search for finding the best back end that can like let us build apps as quickly as possible and i'm uh traditionally from the php realm so here we are building a laravel api so to get some things out of the way my name is chris on code i am a senior developer advocate at digitalocean i started a website called scotch.io for web development tutorials this talk is being recorded and it will be emailed to you after the fact you can also find it on the digitalocean youtube channel as soon as this is done uh let's see what else uh welcome again thank you so much for joining us we're here to have some fun today and if you have any questions i'll do my best to answer them as we go along there's a little questions box somewhere in here uh in the go to webinar panel that you can put your questions in and we'll do our best to answer them live or if not we'll answer them at the end of this webinar all right so what our goal is today is to build a laravel api from scratch and the cool thing about laravel is it's gonna help us make this really easy so we're going to do that over the next 40 minutes and then we'll have q a at the end so to get us started we're here at digitalocean if you want to know a little bit more about my background which i don't know if you do or not i built a website called scotch and this is built on laravel has a laravel api serves views with laravel and it was posted on digitalocean for the entire life of the project and uh now we are part of digitalocean the digitalocean family so if you're wondering if laravel is a good idea for your project i would personally recommend laravel it's taken us a long way and been very easy and fun to work with so here we're going to go to laravel and if you haven't used laravel this is um a php framework that comes with a lot of really good stuff out of the box so to get us started php framework level forge what is this all the ecosystem so level comes with a lot of fun stuff we're going to be doing dealing with the code itself and building the api so i don't want to take up too much time just like looking through the level site let's go into our terminal and here i have a batcave folder i'm going to open up my laravel sites folder and we'll start a new project we'll say laravel new and i'm gonna call this my super duper new api so that'll go ahead and install using composer which is php's package manager all right so this is installed for us oh um so what i'm doing is i'm using a tool called valet it comes with laravel you can install it and then what it does is it looks at your folder structure and pretty much every single folder in the level sites folder is going to spin up into this right here which is going to say uh my super duper uh see that's the problem with my goofy naming is i already forgot so let's go here sorry about this ls my super duper dash new dash api okay dash new dash api api.test so laravel here is um by doing laravel new we have a brand new level install and then by putting it in that folder we are able to visit it with the dot test testing domain all right so remember that you can add any questions in there in the questions box if you have any and we'll answer them so the next step here is i'm going to open up laravel my into vs code and let me hide the activity bar let me hide the status bar and then let me um maximize this so we all get some room i hope that's large enough for you all to read maybe i'll um yeah there we go so if you're not familiar with laravel let us know we can definitely do a getting started with laravel webinar and uh what we're gonna do here is if you're working with laravel the cool thing about it is that it gives you a lot of conventions to follow to build out your applications so a few of the things that we need to focus on are routing for just getting http requests to the right spot we have uh controllers which is kind of the middleman that maneuvers people around our application and then we also have models and our database so this is very similar to the mvc format models views controllers but we'll kind of be skipping the view part because we're building an api and we want this to just be accessed via an api question from the chat will we be securing this api we will not be in this in this one but uh we will schedule another one for securing and authenticating and authorizing laravel api since that's such a big topic so to get us started if we open up this routes folder we have this api folder and then we have this web folder these are the two sorry file and these are the two that we're going to focus on so if we open up the web file let me zoom out for you all um so we have a brand new laravel application and all we have here is a route for the forward slash and returns the view welcome and if you look here that's this view right here but we're not really focused on the front end right we're focused on the api more so if you open up this api folder right here and we scroll down this is middleware off api get user so what i want to do is just say route get and then let's name this like test testing the api i named these way too long but function and basically we're going to return hello um that's not an array message and that's our return so the cool thing about laravel is that it has these route files for us and as soon as we put a route in here route get and you can do a route post or a route delete or a route put or patch if you're familiar with the http methods but here we'll just do a get and now we'll go to our api and let me push this to the left and we can do slash api slash gosh i gotta stop naming these so weird so we have slash testing the api i'll just copy that right here uh right there testing the api it returns json as messages hello so the cool thing about this is laravel already has a file for us for building out our api and the convention is if it's in this api.php file it's going to be forward slash if you look right here forward slash api and then whatever the route name is so when we're building out our apis if you're doing a crud type project let's say we have a something like um users actually let's do something else let's do posts so let's say we are building a blog right and we have posts you would do stuff like route get for all the posts um and then i'm just gonna do some pseudo code some fake coding right here so we would need to get all the posts if we're talking about crud we're right we would do crud is basically um step one is get all step two is get a single step step three is uh let's see update a single and step four is delete right so when you're building out apis this is the crud format uh that we know and basically the http request is going to be how we differentiate between what we want our user to do so this is going to be an http get this is going to be an http get as well this is going to be an uh put or a patch and this is going to be a delete uh and then we missed one which we'll call this two yeah thank you so in the chat uh they're letting me know i missed create so that's create a post and that should be a post request so with these http requests that's how we differentiate between the api right get is on get all so this is going to be forward slash posts and if we want the full url it'll be api posts this is going to be at slash api posts this is going to be slash api posts and then the id so we're going to say id right here and then we're going to say slash api slash posts slash id but it'll be a put or a patch and then this is going to be api slash post slash id and that's the rundown for how you build a crud api right is you're gonna have these five uh requests so we're gonna go ahead and build all of those we're gonna build out our database and then we're gonna build out our migrations and make sure that we can actually use all of these in a laravel api for a post and then as soon as you want to build on this application you just you just add more resources so instead of posts you'll have something else i don't know computers or whatever you're building right so um we can definitely talk about authenticating in a future webinar if you are interested in that but right now let's make sure that we our database actually has content for us to crud so to get us started we're going to do posts right to do a post and this is my notes so to create a resource and that's like posts in this in laravel so step one thanks for hanging out with my um my note-taking skills step one we need to create the database step two we need to create a model to connect to the database so this should actually be create the database and migration so that we can uh fill out our tables in the database step three where we at um create a controller to go get info from the database uh and then step four is return that info now you might be wondering shouldn't there be like a 2.5 in here for um create a service the cool thing about this is laravel provides a tool called eloquent orm which is going to help us connect to the database um it'll map our model to the database so we can avoid writing out so many sql statements which is really nice so let's start with creating the database and the migrations the cool thing about laravel is that it comes with a cli tool called artisan we can do php artisan and we can do controller sorry create cali i already lost my um so we're gonna do make model post so we're gonna make the model and then we are gonna say hey i want you to also create a migration and actually before i do this let me do a git init cool so now i did a get knit git commit first cool now i need to get ad sorry about this everyone git commits m first okay so let's clear that out and now i'll do a php artisan make model post and then i want the migration also so let me show you what that created it created two files and if i open up my git panel right here we'll see that there's two files a post model and then a post migration so if i open up the migration file we can see that it's going to create a table called posts and then it's also going to let us define the properties on this table so we want table definitely title or sorry string and this is going to be a title we definitely want something like content so we'll do a string for content and we're not going to do that too much of this in this webinar because this is more like a getting started with creating an api but we could definitely do table integer and then we'll put the user id in there and then this will start to be our relationships between tables but right now we just want to build a base api real quick so let's see titling content what else do we need in here um title and content i guess that's okay for now let's do table string slug right because we need that and then we want to make slug actually slug is required let's make this nullable so that we can create posts with just a title okay so the next thing that we need to do is make sure that we actually have a database to connect to before we run this migration and create this posts table so i'm going to close everything out the way that we can do this is that we can say hey laravel i want you to use either mysql or you can use postgres or in this case since i'm working locally i'll want to use sqlite since it's probably the easiest to set up locally we've got a request for likes in the database so let's go ahead and add likes let's do table integer and we'll do likes and then let's do a default of zero okay so next up we need to create the database that this is going to work in so if you're working laravel all of our config files are in this handy dandy config folder so we can open up config we see there's a database file right here and then what i can do is scroll down and we can declare a database connection now the cool thing about laravel is all of these config files are really fun to walk through and see how our application is configured down here we have database connection is mysql by default but we can update this db connection in our env file and this is how laravel pulls from our env our environment variables so i'm going to go into my environment variables down here.env and then instead of all this stuff which is how we configure mysql i'm just going to delete all that and do sqlite yes so this is saying hey laravel i want you to look for an sqlite database now when you deploy this to production like when we deployed um scotch to digitalocean we use the mysql database here we're working locally it doesn't matter too much so we'll do sqlite so if you go back to this database.php down here under database connections there's a section for sqlite and this is where laravel will look for a database so by default it'll use the database folder database.sqlite so i'm going to go ahead and create that you could actually create a db.database environment variable name and then move wherever you want to keep your database file for us i'm going to just go into this database folder new file and say database database.sqlite cool so let's take a step back and i want to use a tool excuse me real quick i want to use a tool to be able to look into this database so i'm going to do sql pro for sqlite this is the app i'm going to be using so here we have um a couple databases i'm going to go create new database or sorry open existing database because we already have the file i'm going to look into the batcave folder which is where i keep all my projects laravel sites my super duper new api database folder gotta leave this so far uh and database.sqlite okay so once we're here is this is our table and we have nothing in our database right so we can go over here we can go into our peach our post migration and the migrations are how we declare what's going to be in our tables we have a title slug likes content so the next step right here is to run this migration php artisan migrate and this is going to say hey laravel go ahead and run this migration and add things to our database we have some default migrations that laravel comes with users table password resets fail jobs and here's the one we just created the post table so we go back into sq sql pro we have now our posts table and we can see the fields that we have so if you're trying to do laravel if you're trying to do migrations larryville makes it easy if you're trying to build out your database layerville makes that easy as well okay so we have that next step is to make sure that our model is correct so if we close all these out and that's kind of a fun file to look at right is we look at our database.sqlite do you want to open it um maybe a bad idea let's try it cool so this is the database that we just created um all kinds of stuff that vs code can't really read super cool so let's go into our app folder and this is where the post model was created and notice how there's nothing in the post model yet we need to tell laravel okay so this is how we tell laravel what is in our um in our database we say hey there's a title hey there's a slug there's likes in there and then also do we have something else content that's right okay so we have content as well cool so now we have the um migration we have the model next step is now we can start using uh what's called eloquent which is laravel's orm to start creating data so now i have the post model i have the post migration let's go back into our routes and i can show you some test stuff right here so i'll go back into api.php and let's do a route get posts and then inside of here we're just going to say hey laravel i want you to let's go grab it first use so let's say use app if i could spell app post and we're gonna say hey laravel i want you to create a post um create where let's say the title is my first post and the slug since it was required is my first post okay so as soon as we hit this api route it should go ahead and create this but actually let's save that in a variable post is equal to that let's um spread this out so it's easier to read right there and then let's return the post so we can see what was created cool so now i can go into this right here and lea i shouldn't have named it so long the api slash posts is where we were at for this route on the left if i do that now we have my first post my first post is here sql pro goes ahead and updates itself and if we go into the post folder right here sorry post table and we go into data we can see my first post was created right there so we're at about 22 minutes and what we've done is been able to create a resource type which is posts we're able to create a model which right here lets us go ahead and create um you can do create you can do update you can do delete so level eloquence uh sorry laravels eloquent orm comes with a lot of stuff to let us do um database content changes okay so if you have a larger application it doesn't make sense to have route get post right here route post for the creation for posts so this would be the create route then we would need the route put route where this is post slash id um and then i'm just writing some pseudo code so this would be update this would be delete where we would do route delete posts id right so it wouldn't make sense to put all of these things in this file because now this file starts to get large once we have posts once we have computers um any other resource type would go in here so that's why laravel comes with what are called controllers where we can move all of this logic into a controller and we can go ahead and create that controller let's see what the command for that is so the command for that if i am remembering everything correctly mars php artisan make controller and we're going to call it post controller and we're going to say this is an api controller so if i press enter here oh so all it's laravell's complaining laravel's pretty good at finding errors it's complaining that all of these aren't even real so let's go ahead and delete all these and now we have no routes in our api folder let's go back into our terminal let's try that again php rsn make controller post controller dash dash api cool so now it should have created our controller let's go over here app http controllers and this is just from using laravel so much that i know where everything kind of is and post controller is what just got created so we have a index to get all posts so this will get all posts and let me zoom in for you all real quick okay so that's get all post store is for create a post show is for show a post update is for update a post and you kind of get this destroy is for delete a post right so these are this is the controller now we need to route to the controller where are we so we would do route get posts and then we would do post controller at index and then we would do this multiple times for post and this would be store i believe it was this would be update this would be delete or it was destroy so this is kind of if you want to write it out by hand and this is the four routes that you have but laravel comes with a handy dandy helper for that um let me pull it up for you all laravel right here the docks are really good we're going to do resource controller so if we go to resource controller this is a handy way to to shorthand all of those commands that we just wrote let me zoom in so this is wrap resource photos photo controller in our case posts post controller so another cool thing that we can do is we can actually bind a model to it and i'll talk about that in a second there's a lot of fun nuances to laravel so yes there are some things that you have to remember yes it's very convention based and configuration based but we can do a lot of fun stuff with laravel and have to write way less code so i showed you kind of the long way to do it now we're going to talk about the shorter way to do it route resource and we're going to say posts and that's going to go to the post controller so to test that this actually works we can go into our terminal right here i'm going to clear that out let me zoom out a little bit right there zoom there we go so we can say um let's go into cd my super duper new api clear that out you can say php arson route list and laravel will list out all of the i gotta zoom out on this there so i hope that's large enough let me try that so laravel will actually create all the routes for you and show you the routes that you have api slash posts for the get is going to show all and it's going to go to the post controller and go to the index method on that controller so there's a lot of cool stuff that level does for us and here it created all of these resources for us so let's go and test this out because i want to make sure that everything is working out correctly we're going to use a tool called insomnia if you're familiar with postman which is an api exploration tool insomnia is very similar so here's insomnia and the reason i like insomnia is it's just a little bit faster uh in my experience so we can do i have all of these when i was testing out this whole thing on a twitch stream oh no i don't want that so let me delete all these and then let's delete delete delete okay so let's create a new request and let's say get all posts and we have this as the get is the http call here we're going to say http oh now i really regret what i named this thing let's go grab it my super duper new api dot test api slash posts so this is how we get all posts so now we're going to run through the all of the different requests that we have so here's post so we'll send that nobody returned um of course chris come on we didn't even fill out the controller okay so let's go to our controller right here post controller we'll scroll up to the index right here and we need to get all posts so first we need to say um use app i always do that app post okay so let's fill these out get all posts we're going to say return post all okay so that's get all posts uh store a post we're going to say post return post create and then we're going to say anything in the request and there's validations we can do which we'll talk about in this webinar if we have time but that's how you create show is going to be return post find id down here we're going to update so we're going to say post find by id update with request all right there and then we're going to so let's say post is equal to that um post update and then we're going to return the post that should work uh and then let's go down here to destroy we're going to return the post delete no and the id is that right um so it's not delete kali i'm forgetting mine post destroy that's right and then it'll be the id so notice how easily that laravel lets us do crud on a resource we were able to write out our entire controller we were able to do creation update delete um get all and destroy in whatever that was like a minute two minutes right so let's go back to insomnia let's see if this all worked out just fine there's insomnia send that we got all the posts so we get one post right here because we only have one in our database let's go ahead and create a new request for create a post and this will be a post request here so let's do glee i hate what i named this thing this whole thing right here let this be a lesson to not name things obscure things which i should have already known and this will go to the post api but it's using a post request the other things that we need to remember for laravel's api is we always need to set content type is application slash json and this kind of goes for every api you want to deal with is you say accept application json so that is an indication to the api hey i want json data back and then we can go into the body right here we can say title is my second post and slug is equal to my second post or sorry that should be dashes and there's ways to enforce a slug in laravel um if you're interested in that definitely look up eloquent um mutators and accessors alright so what else did we have likes is that what we call them let's go here it was likes yep and that should actually be an integer which is interesting so likes and then we can say uh 10 and then the last part of this was content is my content so let's send this and hope it works cool so i was able to save to the database title slug likes content and id uh get out of here insomnia and then from there where are we we can go into our database and actually see the second post right there as my content it has likes right there um so i noticed that insomnia is returning this as a string a cool thing about laravel is it has laravel let's see models you can type hint in here i wasn't planning on doing this webinar but now i'm just searching the docs which is a good place to be for laravel since their docs are so good um type eloquent orm it's not mutators sorry everyone now you're just watching me move around docs but i guess this is like real development work right um i want models defying models okay so we have defining models uh there's a way to bind here that i don't remember now ali okay um i think this is it no that's default attribute oh cass that's right thank you in the chat no not lara cass so we'll call this cast attribute casting thank you okay so we can go down here into this right here and we can call it protected casts and then we can say hey likes is going to be a boolean so let's go back into here we'll open up our post model and we have our fillable right here we'll do the same for cass we want to say likes is going to be an integer all right so if that does its job we should be able to go get all posts hit send and now likes comes back as an integer so laravel lets us do stuff like this pretty easily um thank you everyone in the chat for letting me know that was cass uh there's a lot of things that are cool in laravel that are good to remember cass being one of them i should definitely remember that but here we are um so let's do an update we want this number two so let's do a new request actually let's copy this one duplicate update a post let's do create right there and then let's move this up here update we want this to be a put request post slash 2 so we need to go to a specific post actually let's do show a single post so let's do um we'll duplicate this one right here show a single post uh this will be a get request slash post and then we just need to pass it a number for the id and then we don't need any of these anymore and we can hit send and now we get one object for the specific uh post that we wanted and if you look at get all posts it returns an array of objects right here show a single post will return a single object uh there's a recommendation in the chat for doing a getting started with laravel we can definitely do that one uh up next so show a single post we have update a post right here let's see here's number two so we're saying hey we want to update number two uh and let's just update the title right here i don't need to update anything my updated post right there we'll hit send and remember we can definitely authenticate all this stuff so you can go into auth and pass in a bearer token and authenticate this way which we can definitely handle in a future webinar but right now we're just trying to get all of the crud functionality done so this sent let's see it's not updating for some reason did it update in the database my second post it didn't update there so let's go back to our controller it seems like something's wrong around here update so we have post find the id okay post update request all and then return the post so something happened here uh and usually when i'm doing stuff like this where i see a problem i'll just go and write it out in code so i'll go ahead and do this there's definitely better ways to debug but this is like a really simple way to debug get posts and then i'm going to say function oops so we're going to say post is equal to post find to and then i want to do post update title is going to be my newest title and then we're going to return the post um let's just name this something different so that i don't like confuse it with this one right here so let's go forward slash blah in our browser so we'll be over here over here posts slash blah okay so my newest title is here my second post is here it looks like something happened in our request then because it did update in the database right here my newest title so what i'm seeing is that let's see we go to our post fillable title is infillable so we're allowed to do that i think post controller request all um [Music] i think this is fine i think this works but the problem is in insomnia where we sent it instead of doing a multi-part i'm going to send it in as json data and we're going to say title my new new new title sorry everyone i know you probably don't like my naming so we'll send that in it did update right there we'll go down here into our database i gotta figure out how to there we go reload happens automatically cool so that is going to work out just fine um there's a question in the chat is it a bad idea to show post ids on the url uh i personally don't think it is it depends on your situation right like if you're trying to hide how many posts or specific resource in a database there are you could create something besides the id like a uuid or um any other field and then use that as the primary and then use that to identify these resources but right now i think it's fine scotch when i built it has ids as the api call so it's fine there all right so where are we let's see we are at wow that's 40 minutes already okay but um the other cool thing that we can do is that we can go in here and laravel lets us do some validation so we can say well now i'm forgetting my code for validation but let's go over to the laravel docs and this is something you'll find yourself doing a lot is just searching the docs validation so the cool thing about laravel's validation is that it's very easy to do writing validation logic yeah so we can do request validate right here right so i'll take this request validate i'll go back into my controller and i'm gonna say not the show one i want the creation there we go so the store we're going to say validate the data first and we're going to say request validate and we're going to say hey title is required and you can do other validations like minimum length you could do max length you could do like a regex if you want cool so let's see if this works let's go back into insomnia let's create a post um title is right there let's not pass in title send it over the given data was invalid title field is required so that's how easy it is for laravel to authentic not authenticate validate api data um let's see questions in the chat is it better for seo purposes to have the name of the post so the slug in the url so yes i um since we're just building an api right now having this be the number the id for or whatever it is um let's do two um that should be slash posts slash two actually so having the id for the api is fine if you're talking about building out your blog and showing this to users you definitely want this to be the slug and the way you can do that is uh and i'll do this real quickly if you go back to this goes beyond building out just the api side of things this goes to the front end side of things we can go to routes we can go to the web.php file and we can say route get and then this would be slug right and it would say function slug and then you could say post is equal to app post where slug is equal to slug um first and then you would like return a view which we don't have time right now but um you could do post and then you would pass in the post uh that way if my syntax is right so that would be the way to um use the slug as the url part but that would be the web.php file so that's the front-end side of things since we're on the api side of things here i think it's fine to have the id in the url yes you can prevent duplicate slugs if you are interested in that definitely look at eloquent mutators accessors and then there's also eloquent eloquent eloquent eloquent there's eloquent um what are they called um also there's another way you can do this in your migrations post migration where are you posts table yep you can say slug is unique so it has to be unique if it's not unique then the um then the database will just fail on the save another way you can do that is in your post you can create what's called an observer and you can say onsave check if this slug is unique if not you can append like random numbers to the end of it oh yes so thanks in the chat uh everybody's so helpful today so there is a validation so if we go back to the layer of all docs uh let's go for validation so here's the validation docs right here i'm gonna go to the top so it's always weird to find if you scroll down there is an available validation rules and this is fun to see so this is the ways you can validate incoming data from laravel um here let's see you can say required which is the one we used unique should be somewhere in here unique for the database you can say hey this has to be unique for your user model or your post model and by the email address or in our case it would be unique app slash post command slug cool so yeah we're here at the 45 minutes mark we were able to create a what's the best view for this i think this is actually a really good view we were able to do crud in a laravel api and we're just scratching the surface on creating a apis there's so much more to do that we can talk about in uh next ones if you're interested there are uh authentication there's validation there's um deploying laravel apis um and i recommend digitalocean because i'm biased because it digitaloce has hosted scotch for seven years now um so big fan and then let's see what else is on the list deploying and scaling with laravel we could do um you could do rate limiting and api a lot of stuff let me know in the chat if what you're interested in seeing so oh okay so question here what is the difference between store and create i actually should not even have create in here so notice how get oh what's happening i pasted um notice how the get the create method actually uses the get request that's to show a form to like say hey fill out this form to create something and then it will use the store method to actually make the database call to put it into the database but since we are using just an api side of things we can go back here into our api.php routes um i think it's api resource yeah so it's api resource we can get rid of this blah route and now if we go back here let's clear this out so now that create method is gone so now it's just index store show update and destroy uh we are getting requests for laravel passport we can definitely do that deploying a scalable api with digital ocean using laravel we can definitely do that um off the top of my head it would be if i were to do it i would say um manage database as a service which is one of digitalocean's products which is what was used for scotch.io to scale to four million page views per month and then i would say uh a couple a couple servers and a load balancer in there but we can definitely talk about more stuff like caching and laravel caching views and all that good stuff uh database calls all that good stuff let's see more requests for deploying how do you do you generate route list that is php artisan route colon list uh other question what is the convention for versioning i know that some people have they'll say my dash let's go here actually so there's api slash post slash two uh i know that there are conventions to do like v1 and then as soon as you update your apis if you don't want to break the old api you can do v2 v3 before so that is one way to do it you could just go into your routes file right here and you could wrap this all in a route group right here it would be v1 so if you wrapped all that in the v1 and then put all your routes in there it should prefix all of that with v1 so let's go double check that um oh i already had it typed oh it broke i think must be of type array i'll leave my syntax is rusty okay so i think it's this right here if it's not somebody yell at me okay let's try that out let's go over here uh okay sorry everyone let's go up here let's do a route group and let's check my syntax because it's wrong oh the nuisance effects okay so that's middleware to a route group i want route group route prefixes so this is it i think is the one that we want yeah everyone's everyone's yelling at me hey it's prefix chris thank you everyone prefix um and then it's sorry it's prefix v1 didn't i paste it yeah prefix okay so this is what we want right here prefix right there group okay moment of truth chris does no syntax anymore let's go back phpr is in route list okay so now it's api v1 slash posts so that's one way to do it um another way you can do it is storing it actually in the database you can do it by like user you can say user has version one so then um you can look at who was authenticated you can look at what version they're using and then you can route them to the correct controller so that's uh you take a little bit more of the work into your code base rather than the user having to specifically pick out v1 or v2 so those are two different ways to do it it depends on your user base um a couple other questions let's see how what is sorry let me look through these but while i'm looking i hope everyone found that helpful laravel has done a lot of work over the past few years and honestly level's always been great and i've always been a big fan so thank you everyone for joining let's see um how do you delete multiple resources in one api call laravel has something in the docs where you can say if one is deleted then it chains to something else can be deleted also at the same time instead of you explicitly deleting two different objects you can say one chains to the other so that's one way to do it it seems like we're getting a lot of questions for like nested data and complex data we can definitely push a little bit further into the uh how laravel handles data and relationships and some more about eloquent like many-to-many relationships polymorphic relationships if you're interested in that let us know we can definitely do more of that conventions for multiple query string so i'm a personally a big fan of like overly annoyingly talking about the routes so basically what i'm getting at is if you have a post that has comments on it i'm a fan of doing like posts uh this would be like post id comments uh and then if you wanted a single comment then you would do comment id right so some people think that and both camps can be right it depends on your application i gotta stop saying it depends but you can do post slash post id or you could just like skip all that and do comments comment id i guess that would just turn into comment um it depends on how like fine detailed you want your api to be i kind of like doing it here because it it uh i would say it minimizes the requirement for documenting your api because this is more self-explanatory than doing that and then saying in your docs this will also uh return a post object or something like that right and again it depends gotta stop saying that but uh another thing that we can talk about in a future webinar is graphql which can help kind of alleviate all of these creating your api routes because in graphql right it's just one giant route route get graphql and then basically the query that you give to this one route will determine the data you get back so we can definitely talk about some graphql stuff cool yeah so it seems like there's some interest in graphql uh it definitely helps solve a lot of the problems that rest apis have or it makes them a little bit easier to maneuver around i don't want to say like outright it's better or anything because i still build out rest apis because i think for as far as just building out apis this is pretty easy to do with this right here and let me show you something really cool about laravel real quick uh here we are 10 54. um i'm in pacific time so let's see we can also like let's say we had a post right we created a post migration table uh we migrated it we created a post model we created a post controller and then we had to create the poster route so let's say we had um let me zoom in a little bit where did my terminal go sorry about that okay so let's say we had um a brand new table called dog right you could really cool you could say php arson make model dog and you could say hey i want a migration and i want a controller so i want the migration i want the controller and i want it to be an api and you press enter and check it out i have a dog's migration i have pretty much everything we just did in this webinar in one command so i have a dog model right here if you go to database migrations we have a dog's table migration which you would fill this out right here um like name uh playfulness whatever attributes you need on your dogs table and then also we go to http controllers dog controller and we have everything here we have the index we have store we have the show but it's also type hinted to that dog model which is really cool um let's see question about symphony developer versus going to laravel i am personally a big fan of laravel because for me and this is kind of one of the things when i built scotch.io um i did a lot of the coding on the backend stuff but ultimately i'm a big fan of focusing on what your users consider your competitive advantage and for scotch ours was content so any time that i spent not focusing on content for me um was time that i shouldn't have spent so um the question there i think laravel helps me code quickly and get up and running with a product as fast as possible um if you've looked at our previous webinars we've done strappy content which lets you build out an api even faster than this but laravel is a little bit more customizable you can definitely find those strappy videos on the digitalocean youtube let's see we have some questions about oauth we can definitely do that plus one for graphql laravel nova's in there um for level deployment i recommend forge that's how scotch has been deployed for all these years so it went from forge deploys scotch to digital ocean and that's how um scotch has been deployed golly look at that an hour's already gone by uh author authentication authorization is definitely on the list for the next one cool yeah api keys laravel does have auth and authorization and api keys and bearer tokens and all that stuff um and we can definitely talk about that so to wrap us up here we are at an hour thank you everyone for joining uh this webinar it will be recorded and it will be posted to our digital ocean youtube channel and you will get an email to that a link in your email to that any questions that we didn't answer we'll try to answer and get you a document for all of the answers to those um a lot of good ideas for upcoming webinars so we'll definitely be doing more of that let's see what else um if you have questions you can find us twitter.com digitalocean you can find me twitter.comcrissoncode uh my dms are open there what else we were about to close out this webinar and then uh a question just came in what does the budget for building something like scotch.io look like uh if you're building it as just a blog so i would say building a blog out that's a big question huh so building a blog out like this i would say takes a lot of time it i mean scotch has been slowly incremented on it's been on laravel for five years now and it's slowly been incremented on beyond that um i would say i don't know budgets honestly but a good way to kind of get a head start is laravel comes with a lot out of the box if you want to look at something like a headless cms that gets you a lot out of the box as well um but yeah maybe i'll try to answer that and like put it in the dock and email it out to everybody so yeah thank you everybody for joining uh i hope this was helpful i hope this like live coding format it was fun i know that some people i've doing a lot of twitch streams and we're starting up our twitch digitalocean channel so kind of less formal than normal webinars you may be used to uh maybe a little bit more fun i hope so yeah thanks everybody for joining and uh we'll see you in the next one you
Info
Channel: DigitalOcean
Views: 68,401
Rating: 4.8598833 out of 5
Keywords: DigitalOcean, Digital Ocean, Cloud, Iaas, Developers, laravel, API
Id: mgdMeXkviy8
Channel Id: undefined
Length: 59min 10sec (3550 seconds)
Published: Fri Jul 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.