[0:00] hey guys welcome back so guys in this [0:02] video we are going to learn the complete [0:05] rest API in larl 11 version so guys I [0:09] have already installed a fresh laral 11 [0:11] application here and and also served the [0:15] application at Local Host 8000 which is [0:18] running over here okay so now guys let's [0:21] get started so let's open our project in [0:23] our editor so I use vs code editor so [0:27] first step uh after installing your [0:29] larel project [0:30] let's install the API also so let's open [0:34] the terminal and click on new terminal [0:36] so before installing I just want to show [0:38] you few things like here routes so in [0:40] this routes you see you don't find [0:42] api.php file and inside your database [0:46] you don't find few migration about the [0:48] API personal access token and also guys [0:52] about the model so in the model user.php [0:55] you don't have a trade called has API [0:59] token okay so guys now let me [1:02] just get started with installation PHP [1:06] Artis install colon API hit enter so [1:10] your installation [1:18] starts so guys our installation is [1:21] completed and here it's asking a [1:23] question like new database migration has [1:25] been published would you like to run all [1:28] the pending migration files yes or no so [1:31] right now we'll give no guys because we [1:33] want to see our database connected or [1:35] not and here you see API scaffolding [1:39] installed please add the has API token [1:43] trade to your user model okay so make [1:45] sure you're copying this now let's [1:47] minimize the terminal and first let us [1:50] go to your app model user.php inside the [1:55] user model we have to add this has API [1:58] token [2:00] so let us give as use now you can copy [2:03] this at the last of your use inside the [2:06] class user model you have to add this [2:09] has API token perfect so now guys uh we [2:12] have added the has API token and now [2:15] let's move in the migration so database [2:18] migration you find the new table about [2:22] personal access token okay and also [2:24] you'll see a new file inside the routes [2:28] api.php [2:29] cool so now guys let's migrate that so [2:33] before migrating please check do you [2:35] have connected your database or not so [2:37] yes I have connected here using MySQL [2:40] and larel 11 database so now guys let's [2:44] hit the command PHP rtis migrate hit [2:47] enter and your table is [2:51] migrated now let us get back and see do [2:56] we have a personal access token table [2:58] here so let's refresh so guys here you [3:02] see we have a personal access token [3:04] table okay so now so first step guys we [3:06] are going to start with the API CED [3:09] operation with an example of products so [3:11] let's create a migration file named [3:14] products the HP Artis make colon [3:19] migration [3:20] create products table hit enter and here [3:25] migration is created now you can go to [3:28] database migration and the last and now [3:32] let's give the fields so we'll give a [3:36] string the product [3:38] name then the product price and one more [3:42] field let's add that as a medium [3:48] text text function which will be [3:52] description okay and now let's push this [3:55] table into our database so PHP Artisan [3:59] my [4:00] hit enter it's migrated now you can find [4:03] that products table Let me refresh the [4:06] table here okay products table cool so [4:09] we have migrated it now we can get back [4:13] and let's create the model so PHP [4:15] artison makeen model the model name will [4:18] be product hit enter so our model also [4:23] is created product.php let me close [4:27] this and go to app model your [4:31] product.php [4:33] perfect let me just zoom it so first [4:36] step guys we will declare the table like [4:38] product table equal to your table name [4:42] products is the table name and then [4:44] protected dollar [4:47] fillable will be all the fields so what [4:51] are the fields you can just copy from [4:53] your migration [4:55] file that is named description and price [4:59] let's close [5:02] name [5:04] description [5:06] price that's it and now let's create One [5:09] controller so PHP Artis make colon [5:13] controller and I want to create this [5:16] controller inside a API folder API SL [5:22] product controller hit enter and your [5:26] controller is created so you can find [5:28] that in app HTTP controller uh API [5:31] folder inside that API folder you find [5:34] your product controller cool so now guys [5:37] let's begin with creating the functions [5:39] like public function the function name [5:42] index function and here is a typo so in [5:46] this index function we will get all the [5:48] records of the product so before getting [5:51] the records we have to create the API so [5:54] let us go to our routes api.php and here [5:59] let's let begin so first step guys we [6:01] are going to create a route using API [6:04] resource route okay so now guys let us [6:08] give [6:09] the URL so creating a route products and [6:13] let's mention the [6:15] product controller colon colon class so [6:19] make sure you're importing the class [6:20] guys which is already imported [6:23] automatically and now guys we can uh [6:25] check the route list for this product so [6:28] let me open the terminal PHP Artisan [6:33] route colon list hit enter so let's move [6:38] out the top and here you see the API [6:41] part okay so this will be our complete [6:45] API route so now guys we can just follow [6:49] this and create the complete [6:52] functions so let's move inside the [6:54] product [6:55] controller copy [6:57] this create for the store [7:01] and for the [7:06] show and [7:09] update and then [7:14] destroy that's it so now guys let's [7:16] begin with the index function so first [7:19] let's create a variable called [7:21] products equal to your product model [7:24] colon colon get function okay so getting [7:28] all the product data [7:30] and import the product class so it's [7:33] imported here guys and now let us return [7:36] this in jsn format so first I will check [7:40] if there is [7:42] records like [7:44] yes [7:47] else so if a record is available then [7:50] show else you can [7:52] return response function in JSO n [7:58] format and reply 200 [8:01] here and give the message [8:05] as um no [8:09] record available okay so if the record [8:13] is found then we are going to return the [8:16] Complete product data so how are you [8:18] going to return guys we will be using [8:20] the resource part so let me just a new [8:24] terminal and create a new resource PHP [8:28] Artis [8:30] makeon resource [8:33] the product resource okay hit enter and [8:38] your resource will be created so you can [8:40] find that in the app HTTP directly [8:44] resources folder will be created and [8:46] here is your product resource so guys [8:49] this resources is used to respond in [8:51] Json format okay so here you see Json [8:55] resources it uses so guys now you can [8:58] use this [9:00] product resource so you are sending [9:04] multiple data so you have to use in [9:06] different way like just return product [9:09] resources colon colon [9:11] collection function and inside this you [9:13] paste this product variable directly [9:16] okay so whatever data you have in this [9:19] you can access each column so before [9:22] that let me import this class okay we [9:24] have imported and now guys all the [9:26] column Fields like name description and [9:29] price at this product you can access in [9:31] this product resources so right now you [9:34] see here that it's returned directly [9:37] parent to array so instead of this you [9:39] can return in this format [9:42] also like the [9:45] name of dollar this of name okay so same [9:51] way guys you have to change for the [9:53] description [9:56] description then price if if you want to [9:59] take the created at column so you can [10:02] take it same way created [10:06] at [10:08] Price description and if you want to [10:10] give name or else [10:13] product name so anyhow it depends on you [10:16] so this will be the response output okay [10:19] okay so let me just keep it name itself [10:22] and we can get the ID [10:25] also done so guys the difference is here [10:29] you can can manage only like what Fields [10:31] you want to show or else if you want to [10:33] show complete thing you can just keep [10:35] the same okay so guys I will prefer [10:38] using like this so now let's get back [10:41] and yep so guys let us test this API [10:44] like this index function part so let's [10:47] go to our api.php and to test this API [10:50] I'm using a postman software so let me [10:53] just open so guys this is a postman [10:56] software which I have installed so now [10:58] guys let's make make a new request here [11:00] just click on this plus icon and here [11:02] your request sends so now guys let us [11:04] copy the API get back and by default you [11:08] have to give API and then products so [11:11] what is the complete URL guys let us go [11:14] to our browser just copy this Local Host [11:17] 8,000 and paste [11:19] here let me remove this extra forward [11:22] slash and let's send the request so if [11:25] you send so let's see what happens now [11:28] so here we get at the error guys like [11:33] raw so internal server error which is [11:36] telling the collect function does not [11:40] exist so it's not collect it's [11:42] collection guys so let's go back to [11:46] our index function and tell [11:49] collection and now get back send the [11:52] request and here you see by default data [11:56] has come okay now you can click on this [11:58] pretty so here you see in the object [12:01] data you are getting this empty array so [12:04] guys here it's not able to check like it [12:07] has data or not so you can use just [12:09] count function if it is greater than [12:11] zero then you have to return this else [12:14] return this so now get back again send [12:17] the request so here you see the message [12:19] no record available and the status is [12:22] 200 so you can see at this okay so now [12:26] guys uh let us get back and and start [12:29] with inserting the record using the API [12:32] so it is very simple guys just use a [12:34] simple request here get the request [12:36] dollar request and do the validation [12:40] first request of [12:42] validate function and use the name field [12:50] okay this will be your input field value [12:52] and what will be the validation it is [12:56] required and it's going to be a string [12:59] value and then Max [13:02] 255 same way for [13:05] the price will be integer and you can [13:08] remove this [13:10] Max so this will be the price and for [13:13] the [13:15] description it is required string and [13:17] Max should not be there okay so now guys [13:21] you can get started with inserting the [13:23] record into your model I mean product [13:25] table so let's use product model colon [13:29] colon create function in Array format [13:33] and you get all this each Fields so let [13:37] me just copy and paste here and insert [13:40] this just get the [13:43] request [13:44] of name okay so this will be a name [13:48] description so you have to just paste [13:50] that let's paste here paste it so my [13:55] input request description and input [13:58] price so once product is created you [14:01] have to send let's response so return [14:04] response function in JS n [14:08] format in [14:10] Array 200 and here array will be like [14:15] message [14:17] of product [14:20] created successfully and also you can [14:24] respond the data guys which [14:27] is the [14:30] dollar product equal to Dollar product [14:33] okay and here you set the [14:37] data instead of sending the complete [14:39] model we want to send our format data so [14:42] which is the product resource so let's [14:45] get back on this let's copy the product [14:49] resource [14:51] and make as new product resource [14:55] function okay so now guys let's get back [14:58] and create a product record so let me [15:02] just copy this complete API and paste [15:05] here so guys to create or insert the [15:08] record you have to use the same route [15:10] which is shown here let me just show you [15:12] in [15:14] the route list so here you see guys that [15:17] is about the post request which is [15:19] product. store it goes to the store [15:21] function and this is the API so using [15:24] post method and the API stat so now guys [15:27] let's get back to our Postman [15:30] and yep so this will be the API and it [15:33] is a post request so let us select that [15:35] post method and now guys let's go on the [15:38] body Tab and here you have to select [15:40] with form data or raw data okay so now [15:44] guys let's add the fields so make sure [15:47] this raw data which you are entering it [15:50] should be in jsn format so select the [15:52] jsn format here first and then you can [15:54] work on it so now guys let's add the [15:57] fields like name colon and here I'll [16:00] give null and the field [16:05] description description colon null and [16:09] finally the field price colon [16:13] null okay so now guys let us uh just [16:16] click to send the request what happens [16:18] now if you click [16:20] Send uh here you get an error like let [16:23] me just click on preview so here you see [16:25] guys that is it is showing the main page [16:27] homepage so I think it's not able to [16:30] handle the validation part so let us get [16:33] back and move at the store function at [16:37] validate code so instead of this [16:40] validation we'll be using a validator [16:42] method so let's create a variable called [16:45] validator equal to your [16:49] validator class colon colon make [16:51] function and inside this function you [16:54] have to pass dollar request of all [16:57] function and then use array and give all [17:00] the input Fields so here you just copy [17:03] and paste the [17:05] fields and yep so now you can remove [17:09] this now let's copy this [17:14] validator [17:16] if [17:19] validator fails [17:22] function then just return response [17:26] function in JSO n [17:29] format and the error will be [17:32] 422 so in this uh you can send the [17:36] validator messages so let me copy this [17:39] variable and mention that error then [17:43] Arrow function dollar validator of me S [17:46] AES messages function okay and if you [17:50] want to mention like the [17:55] message [17:57] uh all [17:59] Fields fields are [18:02] mandatory okay and now guys uh import [18:05] this valid data class so class import so [18:09] here you select with eliminate support [18:11] fets valid data let's click on it and [18:14] yep so you can just [18:16] move and update that valid dator okay so [18:19] here our valid dator is imported perfect [18:23] so now guys let's get back to our [18:24] Postman and send the request again so [18:27] here all the fields are empty right now [18:30] let's click to send the request so here [18:32] you see all fields are mandatory the [18:34] message is and the error inside that it [18:37] is showing name field is required [18:39] description required price is required [18:41] so let us add the product like [18:45] apple and this is Apple [18:50] description and I will keep the price [18:52] empty let's send a request so you see [18:55] again that it is asking for the price [18:57] field and now if I give [18:59] like 250 and let's click to send the [19:02] request so here you see the product [19:05] created successfully and this is the [19:07] data guys okay which is just shown only [19:10] whatever Fields you want so now guys let [19:12] me just show you in the database whether [19:14] it is stored or not perfect so here you [19:16] see guys our record is stored and now [19:19] guys let's get back to the editor and so [19:23] we have done with the create product and [19:25] now let's move on to view the product I [19:27] mean fetch each product so for that [19:30] let's get back to the api.php and this [19:33] is the API resources guys and according [19:35] to this to get the data one data you [19:38] have to use this route so which is [19:41] coming via show method so here is the [19:45] show method with the product product ID [19:49] and using a get method okay so what you [19:51] have to do you have to use this product [19:54] product variable inside your controller [19:56] so let's copy this product and get back [19:58] inside your show function and here [20:02] provide the model name product and then [20:06] the product variable so make sure this [20:08] variable is same as provided in your [20:10] route list so this is the variable guys [20:13] product so I'm pasting that product so [20:15] now let's return this so return inside a [20:19] gson format you can return or else with [20:21] the help of your resource that is your [20:24] product resource you can just use this [20:26] and return it okay so it's the same [20:28] logic guys returning with the status 200 [20:30] with the your field data okay product [20:34] datas so it's the same so now guys let's [20:37] save and yep let's go to the postman [20:41] let's click on new tab for the new [20:42] request and copy the API so let me just [20:46] copy this get back and paste Here and [20:49] Now guys the route is like providing the [20:52] ID so ID number one here okay so now [20:55] guys let's click to send the request so [20:58] here here you see you have got the data [21:00] that is id1 and the details so let me [21:03] just add few more products okay so here [21:07] I will add like M mango this [21:12] is mango [21:14] description and the price is like 150 [21:18] and let's send the request to save it so [21:20] here product created successfully with [21:23] the mango details and let's with the [21:25] gpes grapes G PES grapes and this is [21:30] going to be like 100 let's send the [21:34] request so here we are so product [21:38] created and now let's get back to the [21:41] show detail okay so let's send the [21:43] request here once again and with the ID [21:46] one so let me get the Grim item so [21:50] product let's send and here you see for [21:54] the ID number three we have got the [21:56] grapes perfect so this is how we work to [21:59] get the data okay so now let's get back [22:02] and continue with the update part so how [22:04] do you update this details okay so you [22:07] want to update this graes record so for [22:09] that uh let us get back to the code and [22:13] write the code for update the product so [22:17] for that guys according to this API you [22:19] are using uput or patch method and here [22:22] is the variable that is product which [22:24] goes to the update function so let us [22:28] get the product variable I mean product [22:32] model and dollar product variable okay [22:35] so this is the same variable you are [22:36] getting it and also we need the request [22:39] like we are sending the input request [22:41] from this Postman right so to get that [22:45] please add the request also request of [22:49] dollar [22:50] request comma [22:53] done and then guys let us add the [22:55] validation and the update code and then [22:58] return [22:59] the data so let us move at the top at [23:02] the store function so here we have [23:04] already written the code for the [23:07] validation and then create and then the [23:10] response so let us copy this complete [23:12] code and paste in the update function so [23:16] now uh let me just arrange it here it's [23:19] gone [23:23] messy okay so guys First Step uh doing [23:26] the validation let me close this [23:28] terminal so we are doing the validation [23:31] and responding that all fields are [23:34] required if validation is done then you [23:37] will start with the update part so here [23:40] I'm just going to remove this complete [23:41] model and just use update function [23:45] that's it okay so this is the variable [23:47] guys from the update function you are [23:49] getting the product variable so using [23:51] that product variable you are updating [23:53] that particular record and this will be [23:55] the [23:56] fields cool so now guys once once [23:58] updated you will return that product [24:01] updated successfully and return the [24:05] updated record okay that's it so now [24:08] guys let us get back to the [24:13] postman and yep let's open a new [24:17] request and let me copy the [24:20] API so here we paste and what method you [24:24] have to use you have to use the put [24:26] method so let me just show you put or [24:28] patch any of this method you have to [24:31] use and let's use it put method itself [24:35] so you are using the put method and now [24:37] get to the body part and at this body [24:40] you have to select with the form data or [24:43] raw data anyhow so if you are selecting [24:45] a raw data please select this in Json [24:47] format and provide the fields so let me [24:50] just copy from the post request guys so [24:53] we have already typed it let me just [24:55] copy this and paste here okay so on this [24:59] ID number three I want to update this [25:01] grapes to g r a p e grape okay and here [25:07] also grape and this is grape only we'll [25:11] keep let's send the request so let's see [25:14] what happens send in the request and [25:16] here you see product updated [25:18] successfully and the grape spelling is [25:21] changed okay so let me just show you [25:24] again with the pricing like I will keep [25:26] 50 send the request and here you you see [25:29] the data is price 50 so guys we have [25:32] done with the updation part also cool so [25:35] now guys let's begin with the last [25:37] option that is destroy which means [25:39] deleting the record using the API so let [25:43] us get back to our code and this is our [25:46] public function destroy function and in [25:48] this destroy function how you can use [25:50] please check out your route so here you [25:52] see it is using a method delete and the [25:55] API is same like as we edit upd dat so [25:59] same way you can delete Also let's call [26:01] the product model P doct product model [26:05] dollar [26:07] product so from where are you copying [26:10] this variable product so here is the [26:13] product variable guys just copy this and [26:15] paste here and it going to the destroy [26:18] function so now let's use this [26:21] product and use delete [26:24] method that's it so this will delete the [26:27] record and then you have to return a [26:29] message so let me just copy from above [26:32] and paste here and here we don't need to [26:34] show the data because we have deleted it [26:37] so let's mention the message as deleted [26:41] successfully okay so now guys uh let us [26:45] get back to the postman and let's copy [26:48] this [26:49] API get to the new request paste it and [26:54] please select the delete method so using [26:57] this delete method and sending the [27:00] request with the ID3 I want to delete [27:03] the ID3 data and now let's click to send [27:05] so once you click to send it will delete [27:07] the record so let's click on send so [27:10] here you see product deleted [27:12] successfully and if you try to get the [27:14] record with the ID3 you'll not be able [27:17] to get because it's already deleted so [27:19] let us see that let me just move to the [27:21] third tab here which is using a get [27:24] request and this is the ID3 let's click [27:27] to send so here you see we have got the [27:30] response guys and let me just preview [27:32] here you see that 4 not4 perfect so here [27:35] you see that 4 not4 means it's not found [27:38] so ID3 is not found but guys this is not [27:41] the correct way to show the output [27:44] because it is a API request so for that [27:46] guys you have to just add a simple code [27:48] inside your uh go to bootstrap inside [27:51] this bootstrap app.php and inside this [27:54] app.php with exceptions so let's go to [27:58] the larel documentation on error [28:01] handling so here I found that how we can [28:04] skip this so just copy this code guys [28:07] and paste inside this exception part [28:10] okay and also import this not found HTTP [28:14] exception and also the request classes [28:16] so here it is let us just copy this [28:19] contrl C and import or paste at the top [28:23] that's it now done so guys let us close [28:26] this app.php and get back to your [28:28] Postman and send the request again let's [28:31] click to send so here you see message [28:34] record not font cool so guys let us just [28:37] get to the first uh request that is [28:39] fetching all the products so let's fetch [28:42] so here you see that on this request you [28:45] have got all the products so ID one and [28:47] id2 that is Apple and mango product [28:50] third product is deleted [28:53] okay so guys we have successfully [28:55] completed with the restful API crowd [28:58] operations using the API resource route [29:02] okay so guys in next video we'll be [29:03] seeing about how to create [29:06] authentication for this route and secure [29:08] this route so guys in this video that's [29:10] it thank you for watching this video [29:12] please subscribe like and share