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

Introducing toggle method0:00

Okay, next up, let's talk about a new method on the belongsToMany relationship called toggle. So this gives us the ability to add or attach an item in the pivot table if it doesn't exist already, or if it does exist, it will remove it. So it's just a simple toggle method. For those situations where you want to say, if the User has favorited the Post, then they must want to unfavorite it. So user, favorites, detach the post. Otherwise, though, if they have not favorited, then they want to attach it. Like this.

Favorite pivot table setup0:28

Otherwise, though, if they have not favorited, then they want to attach it. Like this. So yes, sometimes you may encounter this scenario, and you really have two ways to deal with it. You could have two separate endpoints to favorite and unfavorite an item, or you could have one endpoint that simply toggles the existence of the favorites pivot table row. So let me give you an example of this. Here's what I have set up for you. We have a User, and we have Posts, and a User can favorite the Posts. So you'll see I have a favorites pivot table that I created that simply references the

We have a User, and we have Posts, and a User can favorite the Posts. So you'll see I have a favorites pivot table that I created that simply references the user_id and the post_id that they are favoriting, right? So that means we could do something like this. We can have a relationship down here called favorites. Let me give you some more real estate. And that relationship is a belongsToMany, a many-to-many, belongsToMany Post. Now though, I don't use the convention for naming pivot tables, so like it should be post_user. Eh, that's not overly usable.

Attaching and detaching favorites1:26

post-user. Eh, that's not overly usable. I instead give it something more meaningful called favorites. Okay, so now, yeah, we could do something like this. Let's do php artisan tinker. And very quickly, let me whip up a post. Let's do a few actually. All right, so we have three posts and three different users. So we could do this. So let's say $user first, and we're going to have the user favorite the very first post

So we could do this. So let's say appUser first, and we're going to have the user favorite the very first post we find. All right, so this would be the equivalent of them hitting a like button or something like that. All right, well, I can say user favorites attach. This is nothing new, by the way. This has always existed. But yeah, this is the normal way to do it. So now, if we were to say user favorites, we can see that we have added a new record.

But yeah, this is the normal way to do it. So now, if we were to say user favorites, we can see that we have added a new record to that pivot table that has the user ID and the related post ID. So now, if you want to remove it, yeah, you have to do the inverse. You'd have to say detach, and you have to know that you want to call detach. So if we get a fresh instance and fetch the favorites, yeah, now that has been removed. And it's important that you set this up properly because if you attach it and then you do it again but you don't have any database constraints, well, you end up in a situation where you could have multiple records in that pivot table with the exact same user ID and post ID relationship.

Using toggle in Laravel2:39

could have multiple records in that pivot table with the exact same user ID and post ID relationship. Now, in my case, it would throw an exception, and that's because in our migration, I have a composite primary key set up here. So that would protect you. But still, yeah, nonetheless, you have to know if you're attaching or detaching. And in some scenarios, you may not know that. But nonetheless, you still have to know whether you are attaching or detaching. So here's what you can do now in Laravel 5.3. You could say user favorites and just toggle the existence of the post.

So here's what you can do now in Laravel 5.3. You could say user favorites and just toggle the existence of the post. So now we can see it removed that one record. Let's try it. User fresh favorites. And there's no favorites for the user. Let's run it again. And now we attached it. So yeah, this is all it does. If it exists in the pivot table, then remove it.

Toggle vs sync3:25

So yeah, this is all it does. If it exists in the pivot table, then remove it. If it doesn't exist in the pivot table, then add it. And do note, this is not the same thing as sync. So synchronize, well, in fact, let me just show you. Right now, the User likes this one. Why don't we add one more? App\Post skipToFirst. Some other post. Okay.

Some other post. Okay. So now this User likes two different Posts. Well, if we were to say user.favorites.sync(), and then we just provide, how about the Post with an ID of one? Well, yeah, that's not going to do exactly what we want. That's going to, in this case, remove all other favorites that are not the one that you referenced in your array. So now if we call sync(), yeah, it's not going to do anything, but that's not exactly what we want.

So now if we call sync, yeah, it's not going to do anything, but that's not exactly what we want. We want to toggle the existence of it. So that's why we're using the toggle method in this case. So now it's gone. Now it's back. Now it's gone. Now it's back. So yeah, you could use this for everything from toggling a pivot record for a User's favorite Post, or a Video the User wants to watch later, or a Comment that the User has.

Common toggle use cases4:26

So yeah, you could use this for everything from toggling a pivot record for a User's favorite Post, or a video the User wants to watch later, or a Comment that the User has voted up. There's lots of use cases for this, so have fun.

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