Playing
19. Eloquent incrementOrCreate

Transcript

00:00
Okay let's talk about the increment or create method that's been added to Eloquent. Now if you are used to working with Laravel you'll know that we have methods like first or create, first or new, all of them kind of really helpful methods that get us away from doing any kind of
00:17
additional logic and cluttering up our code. So this is just another addition in that set of things which means that we can just write much cleaner code and of course it's a lot faster. Okay let's take a look at the example that we're going to be working with and this is an example from something that I have previously worked on so it's a real life example that we're looking at here
00:37
and that is that we have some sort of visits table. Now this could be bound to some sort of model it doesn't really matter I'm just keeping it really simple here where we log a visit account of a visit based on an IP address or some other unique thing. So the goal here is to insert a record based on the user's IP address and then increment the count but what we don't want to end
01:01
up with is multiple IP address records being created. So let's take this from the very basics and just go back and start to do this. So I've already got the model created here and let's just go ahead and create a record here so let's store our record. So let's say IP address in here and let's go ahead and grab this just from request IP and let's go ahead and add in the count here
01:25
and let's just set this to one. Now we know that by doing this that's just going to create a record over and over again which is not what we want so this is not what we want. We want to insert this first and then increment this count. Let's go ahead and do that now in another way and then we'll look at the increment or create method. So another way to do this is to go ahead and assign
01:47
the visit that we first create some sort of variable. So let's say visit. This time though we're going to say first or create. Now what that will do is it will allow us to provide two arrays in here. The first is going to be the kind of lookup and we can have multiple in here if we want to. So for us it's going to be the IP address of the user that's going to be the unique thing.
02:09
Then it's going to be the things that we insert based on that. So let's just go ahead and fill this out and I'm just going to add a count of one in here and then let's just see what happens. So if we just go over and give the browser a refresh a few times what's happened is the first time this was inserted it was created. The second time around it already existed so nothing really
02:30
happened. So it will just grab the first record. Now because of that what we can then do, remember this is the old way of doing things, we're going to look at increment or create very very shortly which is going to really speed this up. Then we need to introduce some additional logic. So now we need to say well if this wasn't recently created and we can use the was recently created
02:52
property of this model instance then we want to go ahead and increment the count on there. So basically every time this now runs this route runs or this could be tucked behind some sort of action or method somewhere this will now grab the first visit based on the IP address and it will increment it. That's the exact functionality that we need. So I'm going to refresh this a few times
03:16
and there we go you can see that this now bumps up and if the IP address then changed or of course someone else hit this site their IP address will be logged and their unique account will be logged as well. So of course that's a lot of explanation just to then go into what we're about to look at but hopefully that makes sense. This is what we would have to do previously. I'm going to comment
03:40
this out, I'm going to head over to the database and get rid of this record and now let's take a look at increment or create. So it sort of works in exactly the same way as first or create but the second argument to this is just the thing that we want to increment. So let's go ahead and say visit increment or create. The first is the array of any of the attributes that we want to look up
04:06
that are unique. So in our case once again it's going to be the IP address so we can just grab that from the request object. Now the second one we don't provide an array like we did for first or create because of course we're not inserting records we're just incrementing records. So in our case we want to increment that count value that column so we just provide the column name
04:27
as the second argument that is all we need to do. So we have now gone from this here having to look this up check if it was recently created then specifically increment it to just saying increment or create and just passing this data in. These two things that we've just looked at do exactly the same thing. So let's go over to the browser and let's give this a refresh a few times and there
04:49
we go you can see it's done exactly the same thing. I'm going to do that a few more times as well just so you can see that that bumps up. So a super helpful addition here but there is a little bit more we can do with this if you need to customize the behavior. So if we head over to increment or create let's take a look at what this looks like behind the scenes. So you can see here
05:10
that we've got the attributes that we want to look up and the column that we want to increment. We've got a default which will be the initial value that we insert when this is first created then we have a step so this will be how much this is incremented by. By default this is one and then we have an optional last array of extra data that we want to insert at the same time.
05:35
So technically we could say we want the first one to be one but then we want this to increment by 10 but we also want to insert some other data in here as we go ahead and do this. So you can go ahead and fill that in. Let's just take a look at this working so let's come over and delete this record here go back over and just give this a refresh a few times. If we head over you can see that this
05:55
is now 41 which means the first was one the first insertion was one and then we incremented this by 10 each time. So you can play around with this depending on your needs of course I don't need that so I'm just going to go ahead and get rid of that. You'll also notice as well if we do open increment or create that the default column assumes the column name of count so we didn't
06:19
we don't even need to provide that in that wasn't intentional but you don't need to do that. Okay so lastly let's just take a look actually what this increment or create method does. Now you can see it does exactly the same thing as we looked at at the start of this video. It goes ahead and uses first or create but it taps this so it puts it into taps this will return first or create
06:45
and then it will go ahead and allow us to continue to do something within this function. That's purely just stylistic preference and how this works but this does exactly the same thing so you can see if the instance was not recently created go ahead and increment this and obviously it looks a little bit more complex here because we're just passing in all the data
07:02
from the method signature but pretty much does exactly the same thing as we have just looked at over here but obviously it is now nicely tucked away behind a method. So there we go that is increment or create really really useful when you need it to avoid having to write all of this logic when you need to increment something after it's been created by a unique column.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

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

Episode discussion

No comments, yet. Be the first!