php - Laravel Model Factories and Eloquent; setting dates -


so have app\post model in app, defined follows:

namespace app; class post extends model  {      use softdeletes;      protected $dates = ['created_at', 'updated_at', 'published_at', 'deleted_at'];     // etc... } 

i created model factory, nice new feature of laravel 5.1 define blueprint of post follows:

$factory->define('app\post', function($faker) {     return [     'title'     => $faker->sentence,     'content'   => $faker->paragraph,     'published_at' => $faker->datetimethismonth(),     ]; }); 

as can see set published_at field random date month, generated faker instance. method used returns instance of datetime.

however when generate posts find published_at field null, suggesting datetime instance not being correctly interpreted.

i found if changed line to:

'published_at' => carbon::instance($faker->datetimethismonth())->todatetimestring(), 

which converts datetime carbon , outputs date-time string e.g. "2015-06-15 13:44:23", works correctly.

is best way define faker dates in model factories? would've thought fact defined published_at in $dates array laravel interpret datetime (or carbon) instance , save without me having provide string.

edit

i'm finding not working simple eloquent creates:

e.g. if run

app\post::create([     'title' => $title,     'content' => $faker->paragraph(4),           'published_at' => new carbon,     'created_at' => carbon::parse('2015-06-16') ]); 

the published_at , created_at field still null. ideas?

since faker returning datetime-object isn't representing date-string mysql likes (y-m-d h:i:s) field set null.

you should able access objects property date correct string this:

$faker->datetimethismonth()->format('y-m-d h:i:s') 

this should return date string '2015-06-19 13:07:07' (length=19)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -