Build a Laravel API in 45 seconds?
45sFast-paced demo of creating a full CRUD API hooks viewers who want quick results.
▶ Play ClipThis tutorial demonstrates how to build a RESTful API using Laravel, covering CRUD operations for an anime list. The video walks through setting up a Laravel project, creating a model with migration, defining API routes, and testing endpoints with Postman.
Requires MySQL and Postman. The presenter uses MAMP to run MySQL locally.
Use Composer to create a new Laravel project named 'laravel-api-youtube'.
Run 'php artisan make:model Anime -m' to create the Anime model and its migration file.
Create a database in phpMyAdmin named 'laravel_api_youtube' and update the .env file with database credentials.
In routes/api.php, define GET, POST, PUT, and DELETE routes for the Anime resource.
Run 'php artisan migrate' to create the animes table. Update migration to include 'title' (string) and 'watched' (boolean) columns.
Add protected $fillable = ['title', 'watched']; to the Anime model to allow mass assignment.
Define POST route to validate and create a new anime using Anime::create().
Define PUT route with route model binding to update an anime by ID.
Define DELETE route to delete an anime by ID using $anime->delete().
The video successfully demonstrates building a basic RESTful API with Laravel, covering all CRUD operations. It's a solid foundation for beginners to understand API development in Laravel.
"Title accurately describes the content; the video delivers a step-by-step guide to building a RESTful API with Laravel."
What command is used to create a new Laravel project?
composer create-project laravel/laravel project-name
02:25
What command generates a model with a migration file?
php artisan make:model ModelName -m
02:54
What property must be added to a model to allow mass assignment?
protected $fillable = ['field1', 'field2'];
09:15
What is the default prefix for API routes in Laravel?
/api
07:13
How do you define a route that accepts an ID and automatically injects the corresponding model?
Use route model binding: Route::get('/anime/{anime}', function (\App\Models\Anime $anime) { ... });
13:16
What does the 'watched' field represent in the anime table?
A boolean indicating whether the anime has been watched.
08:23
What is the purpose of the 'fillable' property in a Laravel model?
It defines which attributes can be mass-assigned, protecting against mass assignment vulnerabilities.
09:15
What HTTP method is used to update an existing resource?
PUT
13:12
How do you delete a model instance in a route?
Call the delete() method on the model instance: $anime->delete();
17:39
What does the 'php artisan migrate' command do?
It runs all pending migrations to create or update database tables.
07:55
Model and Migration Creation
Shows the essential command to generate both model and migration simultaneously.
02:54Mass Assignment Protection
Explains the fillable property, a key security feature in Laravel.
09:15Route Model Binding
Demonstrates Laravel's elegant way to automatically fetch model instances by ID.
13:16Delete Operation
Shows the simplicity of deleting a resource with a single method call.
16:50[00:00] what is going on fellow developers my
[00:02] name is telepot and in today's
[00:03] video we're going to be creating a
[00:05] laravel api
[00:07] now you're going to need some
[00:08] pre-requisites for this before getting
[00:10] started
[00:11] and the pre-requisites you
[00:14] need is mysql and postman
[00:18] now as you can see here this is postman
[00:21] it allows you to make api requests or
[00:23] restful a
[00:24] restful request to an api
[00:28] like this as you can see here we can do
[00:29] a get animate one and as you can see
[00:31] there
[00:32] we get the i d the title and
[00:35] if it's been watched or not um again you
[00:38] can create a new anime we can put one up
[00:40] and we can delete one from this api
[00:42] so this is what postman could do but
[00:44] you're also going to need some sort of
[00:46] mysql database you can connect to
[00:50] i'm actually cheating the system and
[00:52] using mamp so mamp allows you to run
[00:54] php applications using apache it also
[00:57] installs mysql and when you start it
[00:59] will start in mysql
[01:00] server where you can access at localhost
[01:04] um if you go into your settings here and
[01:06] you go ports you can see your mysql port
[01:08] here you can change it or set to
[01:10] whatever you
[01:11] think you need but as you can see that's
[01:13] how you um
[01:14] can set up using mamp um i'm not going
[01:16] to show you how to download them simply
[01:18] just google them and
[01:20] find it there or check out one of my
[01:21] other videos either getting started with
[01:23] laravel or
[01:25] my learn how to use php so without
[01:28] further ado
[01:29] let's get on with the video
[01:32] today's video is brought to you by
[01:34] cloudways a managed cloud hosting
[01:36] platform for your php based applications
[01:40] cloudways offers peace of mind and
[01:42] flexibility to let you focus on growing
[01:45] your business and reach new milestones
[01:47] now with cloud wage
[01:48] you get an optimized stack manage surf
[01:51] and application
[01:52] backups staging websites integrated
[01:55] getting composer
[01:56] and the choice of five cloud providers
[01:58] like aws digitalocean
[02:00] lynode and more so if you're an agency
[02:03] or an e-commerce business and looking
[02:05] for a managed solution
[02:06] then go for cloudways without wasting a
[02:09] moment
[02:10] you can use the promo code tylerp
[02:13] when signing up via the link in the
[02:15] description to get 15
[02:17] free hosting credit and by the way i
[02:20] really like their slogan
[02:22] moving dreams forward
[02:25] so the first thing we're going to need
[02:27] to do is create a new
[02:28] rfl project um so to do that we're going
[02:32] to use composer
[02:33] we're going to create a project and
[02:36] we're going to call it well we're going
[02:37] to
[02:38] we need to use larafell and then we need
[02:41] to give it a name so this one is going
[02:42] to be
[02:43] larafell hyphen api hyphen youtube i'm
[02:46] going to hit
[02:46] enter and that's going to create our
[02:48] project so our project's now
[02:50] completed already so to get started what
[02:53] we're going to do
[02:54] is we're going to run php
[02:57] artisan make model
[03:01] and then we're going to give the model a
[03:02] name now we're gonna be creating an
[03:04] anime
[03:04] uh list like what my anime list what i
[03:07] wanna watch sort of thing
[03:09] api so we're gonna call this make model
[03:11] anime um
[03:12] and then we're gonna also do a hyphen
[03:15] and i'm gonna say
[03:16] uh we're to make a migrations and we're
[03:19] going to make
[03:19] the well it's just in migration so what
[03:22] we're going to do is phps and make model
[03:24] anime and the migrations which go with
[03:26] this
[03:27] we can actually do migrations i believe
[03:30] um so let's hit enter could not find
[03:32] file oh
[03:33] sorry we need to actually cd into the
[03:35] directory so let's cd
[03:37] into the laravel api youtube
[03:40] and then let's run that command again uh
[03:43] just to let you know i made a mistake it
[03:45] wasn't migrations it's migration
[03:47] um and now that has done it's also
[03:48] created our migration table
[03:50] there so let's open this up with vs code
[03:53] or whichever
[03:54] text editor you prefer to use i'm going
[03:55] to open this up
[03:57] and let's close that uh
[04:00] let's zoom out one just for now um we'll
[04:03] zoom back in when we're done
[04:04] and the first thing we want to do is go
[04:05] into dot emp and we need to update our
[04:08] database so we actually need to go and
[04:10] create a database
[04:11] so what we need to do is open up our
[04:14] browser
[04:16] if i can open it there we go and then we
[04:19] want to go to
[04:20] localhost forward slash php
[04:23] my admin and that's if you're using mamp
[04:25] if you're using saying else obviously go
[04:27] to where for you stored your database
[04:29] we want to create a new item here and
[04:31] we're going to call this larafell
[04:33] underscore api underscore youtube hit
[04:36] create
[04:37] and it will create us a new database so
[04:40] now we need to take this name
[04:42] go back into our dot emv file
[04:45] and go larafell underscore api
[04:48] underscore youtube and also the password
[04:51] is root for map
[04:52] um and it's root for map but also use
[04:55] whatever database password and username
[04:57] your
[04:58] my sql setup uses so let's close the dot
[05:01] emp file and actually head
[05:03] into the app and models now actually if
[05:07] we open this up
[05:08] we have class anime extends model here
[05:11] now we're going to have to make a couple
[05:12] of changes to this i'm not going to do
[05:14] them now because i want to show you the
[05:15] errors that happen if you don't make the
[05:16] changes here
[05:17] um so let's actually go into our routes
[05:22] uh our api.php and we're now going to
[05:25] set up our first
[05:26] route so what we need to do is go
[05:29] route route get because we're going to
[05:33] be making a get request and we're going
[05:34] to get
[05:34] from forward slash anime we're then
[05:37] going to run a function
[05:40] and inside this function we're just
[05:41] going to return anime
[05:44] all and what this is going to do it's
[05:46] going to go to our anime model it's
[05:48] going to know what
[05:49] how to connect to it to the mysql it's
[05:51] going to run a query
[05:52] to get all of our anime items
[05:56] inside of that array and it's going to
[05:57] return it so what we need to do
[05:59] is actually use um
[06:03] app forward slash models
[06:06] backlash anime also a lot of people
[06:09] mentioned that that's just the incorrect
[06:11] path because it's
[06:12] it's a capital a where it's in the
[06:14] perfect lower case when you
[06:16] use something you need to capitalize the
[06:18] first layer because if you don't it will
[06:20] not find that model so let's save that
[06:24] and now if we go and run this inside of
[06:27] postman so if we bring postman
[06:30] up here let's create a new
[06:33] collection we're going to call this
[06:35] anime list
[06:37] youtube let's create let's click in here
[06:40] and click add a new request we're going
[06:42] to call this get
[06:43] anime save and also
[06:46] let's actually close all of these
[06:50] there we go so let's open this one now
[06:52] we need to actually surf this file here
[06:54] so we need to press control option t
[06:56] to open up our terminal and i'm going to
[06:57] run php artisan
[06:59] surf to run our um
[07:02] our app says run there you go so now we
[07:05] can see the link here
[07:06] so let's copy that go back to postman
[07:09] paste it in here
[07:10] but now every so we did four slash anime
[07:13] but
[07:13] every single route will be prefixed with
[07:17] forward slash api forward slash route
[07:20] so we always need the api when we're
[07:23] using this
[07:24] and now if we hit send we can see
[07:27] we're getting an error base table or few
[07:30] not found
[07:31] laravel underscore api underscore
[07:34] youtube dot
[07:35] animes and you're thinking to us but we
[07:37] created anime
[07:39] well there's actually two things we need
[07:41] to do before this actually gonna work
[07:43] so we need to go back to our app here
[07:46] and we need to run
[07:47] let's close this we need to run php
[07:50] artisan migrate which will create our
[07:54] anime table
[07:55] so if we go to the
[07:59] database migrations and click on the
[08:02] anime table you can see what does
[08:06] it creates a new animaze table
[08:11] so what we need to do is go in here and
[08:13] do
[08:14] table we need to create a string
[08:17] and in here we need to call this
[08:20] uh title we don't need another table
[08:23] which will have a boolean sorry not
[08:27] create a new table use table and we'll
[08:28] create a new boolean called watched
[08:32] and hit save and now that way this is
[08:36] going to be our structure for our
[08:39] anime database so we actually need to
[08:42] um create a so what we need to do is php
[08:46] artisan migrate
[08:49] rollback now what this will do is roll
[08:52] back all the changes we made
[08:54] and now because we've updated our
[08:57] um our migration we can now run php
[09:01] artisan migrate again
[09:05] and this time it's going to create our
[09:06] animes table but with the correct
[09:08] structure let's close this
[09:12] and let's go into our anime here and we
[09:14] want to create a protected
[09:16] sorry a protected table
[09:20] and set this equal to the table name
[09:22] which is going to be animes
[09:24] let's open up our timeline again and
[09:26] clear that and now run
[09:28] php artisan surf
[09:32] let that surf it let's go back
[09:35] to our api and or to our postman and
[09:38] let's send that request again
[09:40] and there you go we get an empty array
[09:42] so that's cool obviously notes come back
[09:44] so we can't really tell if that's worked
[09:46] or not
[09:46] but we we aren't getting any errors so
[09:48] the next step is actually to create a
[09:51] new anime
[09:52] so to do that let's create a new request
[09:55] let's
[09:55] copy this link here put this in here
[09: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
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.