29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Eloquent: Mutators 476<br />

.<br />

13 */<br />

14 protected $dates = ['created_at', 'updated_at', 'disabled_at'];<br />

15 }<br />

When a column is considered a date, you may set its value to a UNIX timestamp, date string (Y-m-d),<br />

date-time string, and of course a DateTime / Carbon instance, and the date’s value will automatically<br />

be correctly stored in your database:<br />

.<br />

1 $user = App\User::find(1);<br />

2<br />

3 $user->disabled_at = Carbon::now();<br />

4<br />

5 $user->save();<br />

As noted above, when retrieving attributes that are listed in your $dates property, they will<br />

automatically be cast to Carbon¹⁸⁷ instances, allowing you to use any of Carbon’s methods on your<br />

attributes:<br />

.<br />

1 $user = App\User::find(1);<br />

2<br />

3 return $user->disabled_at->getTimestamp();<br />

If you need to customize the format of your timestamps, set the $dateFormat property on your<br />

model. This property determines how date attributes are stored in the database, as well as their<br />

format when the model is serialized to an array or JSON:<br />

.<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!