Lessons: 21Length: 2.4 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.6 Showing and Linking Data

In this lesson, we are going to implement the functionality for showing an individual resource. This means that we want to write the functionality for the show method on our controller. And we, of course, want to create the show view to well show that data. Now, when this class was generated, it used this ID as the parameter for this method. And the idea behind that is that this is the ID of the record in the database. It's a unique identifier so that you can find the unique resource that you want to display. And in most cases, that is an integer value. However, it doesn't have to be an integer, it can be any type of value.. As long as it is unique for an individual resource. So if that's a string, then you string, it just really depends upon the type of data that you're working with. In our case, it is an integer value because each one of our records, if you will, has an ID value. So the first thing that we want to do is take this ID and search our data for that record that has the ID. So let's start by getting our data. We'll just store this in a variable called guitars. We want to call, that getData method. And there are several different ways that we can do this. I'm going to use the easy way and just use array_search. Well, this isn't enough, basically, what we want to do is say that we want to find the ID in the provided array. But our array is a multi dimensional array. So what we want to do is search within the individual arrays for the ID value of whatever was supplied here. So what we can do is go ahead and get the values of all of the columns. So we can call array_column, we will pass in the guitars and then we specify the column that we want to retrieve which is going to be the ID. So this is going to give us an array that contains just the ID values. So that we can search that with the given ID, which is going to give us the index. Now, there are two possible values that we can get by calling array_search. It can be false if it doesn't find what we're looking for, in which case, we need to check to see if it's false. So we need to use three equal signs here. Because the alternative is an integer value that represents the index in our data array and if it's zero, well, that could be considered false. So we want to check to make sure that the index that we receive is exactly false. And if it is, well, then that resource doesn't exist. And how we handle that is simply by returning a 404. Otherwise, we are going to return the data based upon the provided index. So we are going to call the view function. We'll call this guitars show. And then we want to pass in the data for this individual guitar. So we're just giving it a key of guitar and then we would provide the data there. So that is the guitar at the giving index. So we can go ahead and implement that view, we're going to put this inside the guitars folder. So let's create a new file there, we'll call it show.blade.php. Let's copy what we have for the index view and we will use that as a basis because that already has a lot of the stuff that we want, such as using the layout, defining the content section and at least it has some of the markup. So in this particular case, we don't really need to check to see if we have a guitar because if we don't, the controller is already going to return a 404. So, all we really need to do is display the guitar information, and we already have markup for that. So let's get rid of the loop, we'll also get rid of the if statement and we don't have any user input here. So we are essentially going to show what we did on the index. But in this case, it's going to be just an individual guitar and wow, did I mess up that markup so, there we go. The unordered list should not be inside of the h3. So now, all we need to do is link to this view. And we can do that, of course inside of the index view, we do need to fix this markup now, so let's move that unordered list outside of the h3 element, because we want a link instead of the h3. So this is where we will have our href, and we will use the route function in order to generate the URL for our resource. So let's call route. And in this case, the name of the route was automatically given to us because, whenever we set up the route, we called the resource method. So by calling the resource method that automatically created named routes for all of our actions on the guitars controller and in this case it is guitars.show. So that is the route name that we need to use. But this isn't enough because we also need to tell it what guitar that we want to show. So let's go ahead and first of all go to the browser. Let's refresh the page and we are going to see an error that we are missing the required parameter for guitars.show, the URL is guitars and then the route parameter is called guitar. Now this is interesting because if we look at our controller, it created a parameter called ID. And remember that there is a direct relationship between the route parameter which is called guitar. I mean, we see that, all right, there, with the parameter name, in our controller. So this means, that we need to go to our show method, we need to change this ID, to guitar and we only used that value in one spot so we can go ahead, and make that change there. But then we need to go to the index view and where we are calling the route function, we need to supply that guitar value. So we're going to pass in an array where the key is the route parameter and then we want to supply the value which is the ID and then that is going to give us our URL. So we can refresh the page now, we can see that everything is okay. And if we click on any one of these links, it is going to take us to the individual page for that guitar. So there we have the American Standard strat. If we click on the Talman. We will see the Talman guitar there. And if we wanted to test the functionality for showing a 404, well, we definitely don't have a guitar with an ID of 10. So let's try that. And we see a 404 as a result. So there we go. We have implemented showing an individual resource. So in the next lesson, we can actually get started working with data, we'll get our database up and running and we will start creating our model.

Back to the top