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

Legacy Accessors and Appends0:00

All right, last but not least, let's review some changes to how you might prepare Eloquent model mutators and accessors. So here in my routes file, I've set up a dummy route for a Post that simply returns the Post model as JSON. So if I visit any random Post, this is what I get. And this is Firefox, so it formats it nicely for me. Okay, so in the past, maybe we want to add an accessor, and that would take the shape of get, and then the name of the accessor, followed by attribute. So maybe we want to add a path to where you can view the Post. This can be useful in many cases. We might call it getPathAttribute, and that would return either a URI or maybe a route name. So maybe post.show, and then we provide it the post. Yeah, again, this can often be useful. Now, I don't have a route name yet, so let's name it here. Now, of course, at the moment, though, we've set up an accessor, but that will not be converted to

Laravel 9 Attribute Accessors0:53

Now, I don't have a route name yet, so let's name it here. Now, of course, at the moment, though, we've set up an accessor, but that will not be converted to JSON by default. So we see no difference there. Now, if you do want this appended as part of the JSON, then you would use the append property. append path. All right, now if I come back and refresh, notice that we do see it now, and that's great. So yeah, nothing is changing here. This is not going away, but what I think you'll find is for Laravel 9 and up, there will be a more recommended path. Instead, you simply give it the name path, and this is going to return an attribute. And notice I'm choosing this one here, eloquent cast attribute. Now, what's neat here is this method name will take the place of your accessor as well as a mutator if you have one. But in this case, we only have an accessor, so I could rewrite this like so. Return attribute get, and notice it

method name will take the place of your accessor as well as a mutator if you have one. But in this case, we only have an accessor, so I could rewrite this like so. Return attribute get, and notice it wants a callable here. So in our case, we want a route to post.show, and then we provide the object. Okay, so this is the Laravel 9 way, and this is the legacy way. Both will continue to work, but I like this approach a little more. It's more in the Laravel spirit. It's simpler. It doesn't require odd conventions of get and then attribute, and if you don't do that right, it's not going to work. You don't have to worry about that anymore. So if I get rid of this, it should still work. Come back to Firefox, give it a refresh, and it does. Okay, now notice we're calling this static method get, but behind the scenes, it's simply instantiating an attribute and passing through a callable function, which means you could alternatively write this as return new attributes, and we'll

Getter/Setter Attribute Basics2:35

but behind the scenes, it's simply instantiating an attribute and passing through a callable function, which means you could alternatively write this as return new attributes, and we'll give it our getter here. Again, functionally identical, it still works. And what's basically going on here is when you instantiate attributes, it wants a callable for your getter, your accessor, and then another callable for your setter or your mutator. So when we call this static method, notice it simply omits the setter because you don't have one. If you instead called a set method, it would omit the accessor because you don't have one. Or you could provide both. Let's see an example of that using something else. Why don't we go to User? Why don't we set up an accessor at the bottom for, I don't know, their name or their username? Here, let's do this. I will often add a username to my users table. Something like that. Okay, so maybe when we create a new User,

Combined Mutator and Accessor3:27

bottom for, I don't know, their name or their username? Here, let's do this. I will often add a username to my users table. Something like that. Okay, so maybe when we create a new User, when it's saved to the database, it should always be lowercase. But when you pull it from the database, maybe you want to, by default, capitalize the first letter. Okay, we could use mutators and accessors for that. Here's how. We're going to return an attribute. That's important. And we will do something like this. Return a new attribute where the getter, and that will always, by the way, accept the value that would be in that column. So this is the person's username, and we're going to format it. You see words. And then the second argument, as we saw, would be the setter. So when you set it or save it to the database, we're going to do strtolower. All right, let's see it in action. Now, real quick, let's go to my UserFactory, and I will add a username field.

Testing with Factory and Seed4:21

you set it or save it to the database, we're going to do strtolower. All right, let's see it in action. Now, real quick, let's go to my UserFactory, and I will add a username field. This faker username. Okay, so let's rerun the migrations. And then, oh, I also need to seed. So let's seed using PostSeeder. Okay, so now that should give me a thousand records. And we'll see our users here now include a new username. All right, so let's check it out. And we'll just do this one in line. If I grab a User, and I request their username, notice the first letter is capitalized. On the other hand, if I try to create a new User, and I set the username to something that's in all caps, if we actually take a look, as part of the mutating, it has been converted to lowercase. Yeah, so think about it. In the past, what you would have needed to do for this example is have two methods, you would have needed to do getUsernameAttribute, and

Named Arguments and Variations5:16

converted to lowercase. Yeah, so think about it. In the past, what you would have needed to do for this example is have two methods, you would have needed to do getUsernameAttribute, and then another one for setUsernameAttribute. It's kind of clunky, isn't it? Instead, we can stick with something like this. And in fact, if you're using PHP 8, you can also use named arguments. So notice right here, get and set, we can be explicit about that, get set. And that just adds a little bit more readability. And also keep in mind, when you take this approach, if you only have a setter, then you could do this. Or like I demonstrated earlier, you could just use the attributeSet or the attributeGet method that you see here. It's up to you. But either way, even if it feels a little different from what you're used to, I think this is a much better implementation.

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