API Key and Endpoints0:00
Okay, now we can start to make use of the game API to pull in information into our application. So head over to the IGDB API page, and you can grab a key here. I'm not going to bother blurring it out, I'm just going to delete it after the video. And it says you need to add a user_key param to authenticate. And if you go here, you'll see some examples of endpoints. So if you scroll down here, you'll actually notice that there is a wrapper for Laravel, which you can make use of if you want to. And it looks pretty good. But in this case, I'm just going to make use of the HTTP client. So ever since the client was released for Laravel 7, I'm more inclined to use that and
But in this case, I'm just going to make use of the HTTP client. So ever since the client was released for Laravel 7, I'm more inclined to use that and make use of the API directly, rather than using a wrapper, but it's totally up to you. So let's go ahead and take a look at an example request here. It's kind of small, but you can see an example or a screenshot of them using Postman. And the endpoint is /games. So we can just grab this. And they sort of have their own query language, which is based on a standard called API Calypse, like apocalypse. And from what I've seen, it's sort of like GraphQL or SQL for REST clients.
Testing API in Client1:10
like apocalypse. And from what I've seen, it's sort of like GraphQL or SQL for REST clients. So you'll see what I mean. So I'm going to go into my REST client here, I use Insomnia. And let's go ahead and make a new request here. So we can start from scratch. I'm going to say, test game API, it's a GET request, create that, and it's based into the endpoint here. And we have to put in our user-key here, like I said, let's grab that, grab this and set the user-key header.
So it's sort of like SQL. So all we have to do is grab this, go to our body and just say other here. And you can paste in the, I'm going to call it SQL just for my sake here. So grab the fields and then sort by popularity descending. So let's try this. Okay. And you can see the ID, name, and popularity because that's what we selected, name, popularity. And you can see that sorted by popularity descending. So you can spend some time to play with the API to get more familiar with it. But now we can try to get this information within our Laravel app.
Creating Games Controller2:51
So you can spend some time to play with the API to get more familiar with it. But now we can try to get this information within our Laravel app. So here I am in my routes file. Let's go ahead and set this up. So I'm going to make a new controller, make:controller, say GamesController, and let's make it resourceful. Okay. Let's go to GamesController and it's play around in here. So our app calls for popular games as well. So let's do that.
HTTP Client Request Setup3:21
So our app calls for popular games as well. So let's do that. Let's grab popular games and let's make use of the HTTP client in here. So if you look at the documentation for the Laravel HTTP client, and if you go to headers, this is how you pass in headers with the request. So let's grab this, paste this in here, let's re-indent this. And the header name is user-key, like you just saw. And we only need one. And I'm going to paste in my key here. Okay.
And I'm going to paste in my key here. Okay. And we'll move this to an environment variable in a second. Let's just see if we get the results in our Laravel app. So let's grab the endpoint here. And like you saw earlier, I'm passing in raw data into the body. So to do that within Laravel, I believe there is a withOptions method we can make use of. And you can see that here. And this is what I used to get it working within our Laravel app. So back to our code.
And this is what I used to get it working within our Laravel app. So back to our code. Before the get request, let's say with options, like this. And let me close this. And then we can call get. So the options are, we're going to pass in a body. And this right here is where we specify our SQL, like you saw. So let's just grab this, paste it in here, and see if we get anything back. So we don't need this. And let's also call JSON to get the JSON response back.
And of course, I didn't change the routes file. So we don't need these anymore. We can make use of the controller. So Route::get /, and it's GamesController, index and say games.index. Okay, so let's try this now. And I have to import that. Import this. Okay, try again. And there you go. We are getting the same results in here than we are in our HTTP client.
Moving Key to Config6:28
There you go. You get 20 results here. Cool. So let's see if that works in our app, say limit 20 should work. And there we go. We get 20 results. Okay, so let's clean this up a bit. I am going to move this into the config. So we can do config.services.services config method and services.IGDB. And we can move this on its own line.
So we can do config.services method and services.IGDB. And we can move this on its own line. Just indent all of this. And let's go to config/services. And let's make a new one called IGDB. And I'm just going to paste that in. Actually what do I name it? So I have to make a key called IGDB. So let's do that. So IGDB.
So let's do that. So IGDB. And this will be this array here. So I can just join this here. Okay. And that looks good. And now we just have to separate this into something we just copy this or cut it and say ENV IGDB_KEY. Okay, let's put this into our .env file and say IGDB_KEY and paste that in. And hopefully this still works.
Okay, let's put this into our .env file and say IGDB_KEY and paste that in. And hopefully this still works. Cool. And just to double check that it doesn't work if I change the key and there you see an error here. Cool. Okay, so let's go back to our controller. And instead of dumping it, I mean, instead of dying and dumping it, let's just dump it and it's returned. Let me just move this up a view index and it's passed in the popularGames.
based on our design. So first off, we only need 12 results for the popular games because that's what I have here. So we can do limit 12. Now if you want to see every single field available to us, we can just do fields * and then we can just base it off what we need there. So again, refresh this and now we can see every single field that we need. So for example, we need the cover. So there is a section here for cover, but it's only returning the ID, but we want the details of the cover, specifically the URL of the image.
So there is a section here for cover, but it's only returning the ID, but we want the details of the cover, specifically the URL of the image. So we kind of want to do a join here on a covers table and this makes it really easy or the API makes it really easy. All you have to do is say the name of the field and then we can do dot star to see everything. And then that will return the fields of the cover and then we can just select it based on that. So we cover and now you see it's an array instead of an ID and this is specifically what we need right here. So we can just do cover.url because that's what we want.
what we need right here. So we can just do cover.url because that's what we want. And I'm going to spare you all of that and just paste in the fields that I actually need. So name, cover, firstReleaseDate, popularity. So platforms is the systems it's on and I did the same trick here where I did platforms.abbreviation is what I need and we want the rating as well. So if I do this, let me show you what I get here. If I look at these games, you'll see that, well, this is a Unix timestamp, but this is an older game and I only want somewhat recent games. And if I look through here, you might actually get platforms or games that are on older platforms.
two months after this current date. So I'm going to do this. So I'm going to do an and clause. First releaseDate, again, that's the field that's returned. Where then or equal to two months prior to today's date in Unix timestamp. And first releaseDate is less than two months after today's date. So we can actually make use of Carbon here. So that's what I'm going to do. I'm going to put a variable in here. That's why I use double quotes.
I'm going to put a variable in here. That's why I use double quotes. So I can just inline variables here. So I can do before and after. And we'll use Carbon up here to grab those dates and make sure to put a semicolon here. And that looks good to me. I guess we can put this on its own line. OK, so let's go ahead and grab those dates. So like I said, I want before to be Carbon now. I'm going to subtract two months from today's date.
So like I said, I want before to be carbon now. I'm going to subtract two months from today's date. And I'm just going to grab the Unix timestamp. And we can do the same for after. But instead of subMonths, it's addMonths. And hopefully this works. If I did everything correctly, I have to import Carbon. OK, and let's try this out. So now I have a syntax error. OK, let's see, maybe this space here.
Rendering Games in Views14:14
Let's go into our index.blade.php. And where is the first game here? Popular games. This is the first game. So we no longer need these 11 hard coded ones. So let me just delete that. OK, so I erased the 11 below. There should only be one here now. OK. And now we can just foreach over these.
OK. And now we can just foreach over these. Let's do that foreach. I believe I passed it through. Popular games as game. And we can just move this below. And we can reinvent this. OK. And I believe the title is gameName. So we can do a gameName.
Let me just undo that. And it should be right here. Cool. There we go. Let's get the cover in here. So instead of this, we can do gameCoverUrl. Say URL. OK. See how that looks. And as you can see, we're not getting the correct size.
So we can just do a string replace in here. So let's just do this. str_replace first. We want to replace the thumb string with cover_big. And that should return the correct image to that. And let's close the bracket and see if this works. OK, looks good. And let's put the platform in here. And I'm going to do this within the view for this video. But later on, when we refactor our code, we'll move this somewhere else.
And I'm going to do this within the view for this video. But later on, when we refactor our code, we'll move this somewhere else. So let's just do a foreach here. On $platforms is the array of platforms this game is on say on $platform. And we just want to output the platform abbreviation. And let me just hard code a comma here. And like I said, in future videos, we'll refactor this. OK. So I was getting that error because if you look here for Doom Eternal in the $platforms, there exists a platform that doesn't have an abbreviation.
So I was getting that error because if you look here for Doom Eternal in the platforms, there exists a platform that doesn't have an abbreviation. So we have to check if that abbreviation exists. Again, we'll clean this up later on. But for now, I'm going to do it here so we can use the array_key_exists php method to check if the abbreviation key exists on the platform array. And we can do this and put a comma. And do nothing if it doesn't exist. See if this works now. OK, and it does.
See if this works now. OK, and it does. So, yeah, we have this trailing comma, but like I said, we'll fix that later on. So now we just have to do the same thing for these sections over here. So one for recently reviewed, we have to make a query for that. And same for most anticipated and same for coming soon. So let's do this quickly. So I'm going to grab this and do something similar. So let's just duplicate this. Actually, let's change this to recently reviewed.
So that is our algorithm for getting recently reviewed games or our query, and we only want three. And we need to get the ratingCount that I used here. So we can do that. OK, and let's see if this works. Stump this. And it's passed that in as well. Recently reviewed. OK. And see if this works.
OK. And see if this works. Current. Oh, yeah. So I just have to pass in or make another Carbon variable here called current, and that's just the current date. So Carbon::now(). timestamp. OK. OK, so I'm not sure why I'm getting ten. I thought I limited it to three.
And we can delete the other two. So let me do that. OK. And there should only be one here. Okay. And now we can just loop over that. So let's do foreach. And let me just put this underneath so I don't forget. And let me just indent this. Okay.
Cool. This is called the summary. And I'm not sure if I'm getting that in the fields here. So I have to add summary here. So let's do a summary. Okay. And we can put that in here. Oops. Let me just grab the name again. It should be summary. Okay.
Okay. So for the popular games, I'm just going to paste it in. It's gameRating and I'm rounding it and I'm adding a percent sign. And let's do it for this as well. This is the right one, right? Yeah, it is. Okay. And I'm getting this error because some games don't have ratings yet. So we have to check for that index. So say round up here. We just have to check if gameRating is set,
I just have everything that's coming after the current date with a popularity greater than five and sorting by the first release date. So the soonest should be first, and you can see it in here at the time of this recording. Cool. And the views are pretty much the same. The only difference is the cover is smaller, and we have this date here. So let's go into the view here so you can see that the cover is smaller here, and it's using the Carbon::parse method, giving it the Unix timestamp and formatting it to this format here. And now we have our games set up, and we can get rid of these dumps, too.
