Playing
02. Basic scaffolding

Transcript

00:00
So we're starting out with a completely fresh Laravel project. And before we do anything, we're just
00:06
going to make sure we have got Redis installed and we can access the command line interface. And I'm going to show you a couple of ways that you can get Redis working if you don't already.
00:17
So if I go ahead and bring up a terminal here, I can type in redis-cli. And that boots me into the Redis command line interface where I can run a series of commands.
00:27
For example, I could set name here to Alex. And then I can go ahead and get the name. And of course, that gives me Alex. So if that's the case for you and you already
00:37
have Redis set up, then you're good to go. Now, I go ahead and manage this using Laravel Herd. So I have a service created just here. And I've created this Redis service.
00:47
And we have access to this port here, which we can configure directly within our Laravel application. If you don't have that available, you can also use something like db-engine.
00:57
And you can create a service here, start it up, and we get pretty much exactly the same thing. So however you go ahead and set Redis up, make sure you have it running and you
01:05
can boot into the command line interface so we can inspect what we're storing within Laravel. The second most important thing to do is head over to your EMV file and just
01:14
make sure that you have Redis configured properly here. Redis client should be phpRedis. The host should be whichever host you're running on. Usually, we don't have a password
01:23
when we're running this locally. And of course, we want to make sure this maps up to the correct port as well. We don't need to worry about things like the cache driver
01:31
or anything like that because this isn't really caching. We're just going to directly access our Redis instance and store everything that we need in there. OK, so now that we've got this working,
01:42
let's go ahead and set up a comment model, which is what we're going to be applying our likes to. But as I've said, this can apply to any model that you create. So let's go ahead and make out a model here.
01:52
And we'll call this comment. We'll go ahead and create a migration alongside of this as well. So let's open up the Create Comments table.
02:00
And if you are not new to Laravel, this will be pretty obvious to you. But we basically just want a couple of items on our comments. So let's go ahead and set some text in here.
02:09
That's going to be the comment body. And while we're here, we could go ahead and apply this to the user that posted this as well. So let's go ahead and set up a foreign ID here
02:18
and just go ahead and constrain that. OK, so we can go ahead and run phpArtisanMigrate. And sure enough, if we open up our database, we should now have that comments table in there.
02:28
Great. OK, so we're going to need some fake comments. We didn't do this when we set the model up. So let's go ahead and make a factory in here.
02:36
And we'll call this CommentFactory. And if we head over to that CommentFactory, we should now be able to define out a fake body in here so we can generate these out.
02:46
So to do that, let's go ahead and use the fake helper. Let's go and generate a sentence here. And we'll just say 20 words. So let's go ahead and boot up a tinker session.
02:56
And let's generate out a fake user. So let's say user, factory, and create. And that will pop a fake user in the database for us. If we just go over to there, you can
03:07
see that we've got this user here. We can use that to sign in to check this. We can also create another user a little bit later to test this out as well.
03:14
So the next thing is now we've got our comments factory. And let's go into our models directory. Under our comment, do exactly the same thing. Create a factory instance.
03:25
But this time, we want to generate out a few comments. We want to create these. But then we want to supply that user ID, because these need to be by a specific user.
03:35
So it doesn't matter too much for now. But now that we've done that, if we hop over to our comments, we've got three comments ready to go. Great.
03:42
OK, let's exit out of this. And we will go over to our web routes. And we'll create out a endpoint for this. So let's go ahead and return a view here.
03:51
We haven't created this just yet, but we can do. So let's call this comments index. And down to that, we want to pass a bunch of comments through.
03:59
So let's pass down all of our comments. And we could probably scope them by the latest order as well. Great. OK, so let's create out a view for this.
04:08
So let's say make view comments.index. And that's created. Let's open that comments index view up. And we can just start to iterate through these.
04:19
So let's go ahead and create a for each loop here. We'll say for each comments as comment. Create out a wrapper for this. And of course, we want to output the comment body in here.
04:32
Let's put this into a paragraph tag just to space things out a little bit. And then down here, we're going to have a form which will contain a button to like something.
04:41
OK, so I think that's just about it for now. The last thing I just want to cover is fake signing in a user. So let's go over to our web routes again.
04:50
And at the top here, we're going to go and use the auth facade to log a user in using their ID. Since we don't have authentication in our Laravel app that we've set up, we're just
05:01
going to go ahead and fake this for now. But of course, in your own applications, you're going to be signed in. So I'm also just before this going
05:07
to pull in the line to sign a user out in case we want to switch over. And then we can head back over to our index here. And at the top, just go ahead and create something out
05:16
that greets the currently authenticated user. So let's say, hey. And then let's just grab the currently authenticated user and output their name.
05:25
OK, let's go over and give that a refresh. And yeah, so we're signed in. So we should now be able to like each of these things once we start to build up all of that functionality.
6 episodes 21 mins

Overview

Let’s skip the database and build the ability to like any model in Laravel, using Redis.

Traditionally you’d reach for the database for this kind of thing, but as you load more models and start performing checks within relationships — things begin to slow down.

With a key-value store like Redis, tracking users who have liked comments (or anything) keeps everything ridiculously fast.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.