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

Add Expires Column0:00

Now, in my .env file, you can see I've set up an SQLite database. And that's because we'll be discussing Eloquent date casting. So let's go to the default create_users_table migration, and we'll add something here, like maybe a user expires in your system. They sign up, but at some point their account expires. Something like that. So we'll say a timestamp for expiresAt. And we'll now give this a run. Okay. And then one final thing, because we've added a new column here, let's go to UserFactory

Update User Factory0:31

Okay. And then one final thing, because we've added a new column here, let's go to UserFactory and just add a new item here. expiresAt will be, let's use the today helper, and we'll say maybe add a month. If you're not familiar with today, let's take a look at that. It's on Illuminate\Foundation\Helpers, and it's just giving you a Carbon instance for today. So that means I can do whatever I want there. I can add days to it, I can add a month, I can add a number of months, anything I want. Let's stick with that.

Test Data in Tinker0:58

I can add days to it, I can add a month, I can add a number of months, anything I want. Let's stick with that. Okay. So now let's boot up php artisan tinker, and I will quickly whip up a User using a model factory here. And we can't see the carbon instance there, but yeah, if I were just to grab a fresh instance from the database, here's what we have. So expiresAt is one month from the time of this recording. Okay, great. But what if we want to configure that?

Configure Attribute Casting1:23

Okay, great. But what if we want to configure that? And this is what's new in Laravel 5.6. We could go to our User model, and as you may be familiar with, we have attribute casting here. And we'll say expiresAt, let's do this as a date. And yeah, now we can specify the exact format. So maybe you want the year, the month, and the day. Now this is possible on a per attribute basis. Let's try it.

Now this is possible on a per attribute basis. Let's try it. php artisan tinker. Once again, find the User. And yeah, now any time it gets cast to an array or to JSON, like when you return from a controller or fetch it from an API, you'll see that expiresAt has been updated. And once again, let's just do toArray in this case. Yeah, now it's using the proper format, which means if you wanted to, all three of these could have their own unique format. They're not bound together.

Choose Date Format Codes2:13

could have their own unique format. They're not bound together. And you can configure it much more easily in 5.6. So if we switch over to something like Safari, here's that standard date page on the phpDocs. Probably one of the most visited pages ever for the phpDocs. But yeah, you can refer to whatever you need here. So maybe, you know, maybe, I don't know, maybe you just want the day of the week. Maybe when you create an account, it only lasts for a handful of days. I don't know. But yeah, in this case, you would use a lowercase l to grab that.

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