در حال بارگذاری ...

Filtering Notes by User0:00

Alright, welcome back everybody, let's jump right in where we left off in the previous episode. Okay, so behind the scenes I've created two Users, as well as a handful of Nodes that belong to those Users. So notice this one has a user_id of 1, and that one also has an id of 1. So those correspond to John. Now if I write a SQL query for that, I might say SELECT * FROM nodes WHERE user_id = 1, or John. And sure enough, I will get only his Nodes. And if I change it to 2, then I will get Kate's Nodes.

And sure enough, I will get only his nodes. And if I change it to 2, then I will get Kate's nodes. Okay, let's try to get this on the screen. So if I open index.php, of course a number of episodes ago we required a router, and this router has some example endpoints that we respond to. So let's have a look in the browser. And yeah, this probably looks familiar. So let's do this. I'd like to add a new notes link to the navigation bar. Alright, we will go into Views/Partials/Nav, and yeah, if I hide this sidebar, here's

Adding Notes Navigation Link0:58

I'd like to add a new notes link to the navigation bar. Alright, we will go into Views, Partials, Nav, and yeah, if I hide this sidebar, here's our links. So why don't we put it after the About page. That will take us to /notes. We will activate it if the URL contains that. And then, yeah, this is a little messy, and by the way, we could clean this up quite a bit, but I just want to stay on track for now. And I will update this to notes. Okay, so if I switch back and give it a refresh, there we go.

Registering Notes Route1:25

And I will update this to notes. Okay, so if I switch back and give it a refresh, there we go. We have our notes link. But of course, if I click on it, we get a sorry, page not found. We get a 404 because we haven't registered a listener, so to speak, for that URI. So that's the next step. Let's go back to, yeah, here we are. Let's go back to our router, and we'll say, we'll duplicate this, and listen for, that's how I think of it, sort of like a listener. Listen for when the user requests this URI, and when they do, I'd like to load my controllers.

how I think of it, sort of like a listener. Listen for when the user requests this URI, and when they do, I'd like to load my NotesController. All right, that's the next step. Right here, why don't we duplicate About, so Command C, Command V, and I will call it notes. There we go. Our heading is notes, and this will load a view, notes.view.php. And just like before, I can duplicate this to save ourselves some time, notes.view.php. Okay, so if I come back and refresh it, there we go.

Explaining MVC Request Cycle2:20

And just like before, I can duplicate this to save ourselves some time, notes.view.php. Okay, so if I come back and refresh it, there we go. So now we have our Home page, our About page, and a new Notes page. Okay, let's take a quick moment to talk about this. This is a really important cycle that you're going to appreciate down the line when you start learning about frameworks. So keep in mind what we did. I wanted to create a new page for our notes. So I started by registering a route listener or an endpoint, however you want to phrase it.

So I started by registering a route listener or an endpoint, however you want to phrase it. Well, let's listen for this URI. Let's respond to this URI. And when the user visits that page, here's the controller that is responsible for handling that particular request. And then we created that. That controller can now do whatever it needs to. It can gather all of the data that's necessary, and then it passes that data off to a view. The view is then responsible for, at least in our interpretation, the view is responsible

It can gather all of the data that's necessary, and then it passes that data off to a view. The view is then responsible for, at least in our interpretation, the view is responsible for presenting that to the user. And here we go. So we're going to think of it sort of like the HTML layer. I bring this up because some people might squawk at the idea that the view is basically the HTML. It's sort of a new thing, but you know what? It's fine for now. You can dig into that later on your own.

It's fine for now. You can dig into that later on your own. Okay, so we have this idea of registering a route listener. We have controllers. We have views. I promise, once you start learning about frameworks and Laravel or Symfony, you're going to feel right at home. I promise. Okay, so let's come back, and this is looking good. Okay, so now the next step is to fetch all of the relevant notes from the database.

Okay, so let's come back, and this is looking good. Okay, so now the next step is to fetch all of the relevant notes from the database. And yeah, I'm just going to assume that this guy here, the Stock model, is our John Doe. So I want to grab all of John's notes. All right, let's go back. And it sounds like we should do that from our Controller. Because remember, the Controller accepts the request. It can delegate. It can prepare. And then it passes that data off to the view.

Fetching Notes from Database4:18

It can prepare. And then it passes that data off to the view. Okay, so first up, we'd like to access our array of notes from the database. But yeah, notice that, well, I want to interact with the database, but if I go back to index.php, we don't create our database instance until after we require the router, and that router loads the controller. So for example, if I were to, within our NotesController, simply die and dump that $db variable, we're not going to see anything. Let's go back and give it a refresh. Yeah, and notice we get undefined variable $db, which makes sense, because again, we aren't

already have the database available to us. That way, when we require the router, and the router requires the controller, then we will have a database class that we can instantiate. Okay, so if we come back to Firefox and give it a refresh, there we go. We're up and running. Okay, so let's write our query now. I can say $notes equals DB::query(), and yeah, we're just going to write our query that we played around with at the beginning. So John has a user_id of 1. So let's grab that, switch back, and paste it in.

So John has a ID or a userID of one. So let's grab that, switch back, and paste it in. And then let's fetch all of the results. And then once again, let's pass notes to our handy dandy dd function. Okay, so come back, give it a refresh. And just like that, we have all of John's notes from the database. Perfect. Okay, so now when I load the notes view, all I have to do is loop over these notes. And we already learned about that in section one of this series. So let's do it maybe right here.

And we already learned about that in section one of this series. So let's do it maybe right here. We can say php foreach $notes as $notes, and I'm going to use the shorthand version, and foreach. Again, this should all be a recap at this point. Why don't we just use a simple list item where we say, give me the note body. Okay, so come back to Firefox, give it a refresh. And there we go. We have all of John's notes. So that means if I were to go back to TablePlus, let's add another one here.

We have all of John's notes. So that means if I were to go back to TablePlus, let's add another one here. How about thoughts on my continued learning of PHP? And again, let's say John's learning PHP, so he has an ID of one. So if I come back to Firefox and give it a refresh, now we see that as well, which is pretty cool. Okay, so now with that in mind, maybe instead of notes, we should change this to my notes. So let's go back to our controller, and yeah, let's update the heading to my notes. There we go. All right, next, maybe it would make sense for this list of notes to contain only an

There we go. All right, next, maybe it would make sense for this list of notes to contain only an excerpt, but then when you click on it, it takes you to the full note or something like that. Okay. Well, let's see. Let's go back into our view. And yeah, maybe this should be a anchor tag that takes us somewhere. We're not sure where yet. Like that.

Creating Single Note Page8:49

But you know what? Right now, our little makeshift router doesn't allow this, so we're going to keep it simple. Instead, we will send the user to something like /note, and then I will pass through the ID of the note through the query string. That's a nice and easy way to get started. So for example, if I did this, all right, so now I'll come back, and you have a look right down here. We're saying load the note page and send through ID equals 1. But notice for every single link, it says ID equals 1. So this is another case where, of course, the ID needs to be dynamic.

But notice for every single link, it says ID equals 1. So this is another case where, of course, the ID needs to be dynamic. The ID needs to be, well, the ID of the corresponding Note in the database. All right, that's the next step. So back to phpStorm, and we'll say right here, open up PHP and echo the note's ID. All right, let's see if that works. So once again, have a look here, ID equals 1, and then ID equals 3, and then ID equals 7. I think we're in business. Okay, so let's click it.

I think we're in business. Okay, so let's click it. But yeah, of course, we get a 404, because again, I haven't registered an endpoint. So that's our next step. And again, try to get into the process of what this looks like. So we start by going to the router. We register an endpoint, /note. We'll load a controller /note file. That can be copied, so notes to note, all right. And now, hmm, how do I grab that ID?

That can be copied, so notes to note, all right. And now, hmm, how do I grab that ID? Well, again, you already learned about the $_GET super global, right? So if I were to say, $_GET['id'], excuse me, there we go, one. So that is the ID of the note that we want to fetch from the database. So let's do this, $id = $_GET['id']. Okay, so now our SQL query for this page will be select * from notes where id = $id that was passed through the query string. But remember now, I'm not going to inline it like we learned a couple episodes ago. That's a big SQL injection or formatting security threat.

But remember now, I'm not going to inline it like we learned a couple episodes ago. That's a big SQL injection or formatting security threat. Instead, I will use a placeholder or a wildcard like this, ID. And then I can pass through those wildcards as the second argument, like this. Okay, but now notice that, well, we could do this if we want. But I'm creating a variable, and that variable is only referenced in a single place. So as a general guideline, you don't always have to follow this. But as a general guideline, if it's only being used once, just inline it. And again, most editors offer a way to do that automatically, as you see there. Otherwise, just do it yourself, get ID, and then I can delete this.

And again, most editors offer a way to do that automatically, as you see there. Otherwise, just do it yourself, get ID, and then I can delete this. All right, cross your fingers. We come back to Firefox, we give it a refresh, and yet there we go. We're loading the note with an ID of one. And just to prove it to you, ideas for next vacation. The note with an ID of one, ideas for next vacation. Let's do, how about number seven? All right, let's update this, or actually go back to notes. This one has an ID of seven, and there we go.

All right, let's update this, or actually go back to notes. This one has an id of seven, and there we go. It matches, very cool. But yeah, at this point, notice I'm still loading the view for all of our notes, which we don't actually want. And another problem is, well, why did that work? Did you think about that? We were loading the view for a different controller, but everything still worked. And that's because, well, we wrote a query to fetch a single note, but we did fetch all, which means give me an array of the results.

And that's because, well, we wrote a query to fetch a single note, but we did fetch all, which means give me an array of the results. Even though, because we're grabbing one by its ID, there will only ever be a single result. So we should update this to note, and then change it to not fetch all, but just fetch a single record. Okay, now, we can tweak this, and then create a new view. So yeah, as a general guideline, for every single URI that you want to respond to, there will probably be a corresponding view for it, if there's something to present to the user.

But yeah, for now, and for presentation only, I'll keep it, and there we go. Okay, so let's go back to notes. Let's look at work reminders, and again, in real life, this would probably be a fully fleshed out note about things you need to remember for work. Okay, so now let's just wrap up. I think we're done. But yeah, let's wrap up by quickly adding a button that will link us back to all of the notes. Something like this, /notes, and then we'll say, go back.

that will link us back to all of the notes. Something like this, /notes, and then we'll say, go back. All right, come back, yeah, but let's style it. Text blue, 500, I should have some general link styling here. Yeah, and then we can push this down just a little bit. Margin bottom of 6. Really quick styling, but yeah, that should be fine. Okay, so there's notes with an ID of 1, and then whatever this is, 3, and then 7, and again, these are only the notes that were created by our fancy model here, John Doe.

Fetch Notes By User

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟