title: Laravel Model Factories and Eloquent; setting dates
body: <p>So I have an <code>App\Post</code> model in my app, defined as follows:</p>
  
  <pre><code>namespace App;
  class Post extends Model 
  {
  
      use SoftDeletes;
  
      protected $dates = ['created_at', 'updated_at', 'published_at', 'deleted_at'];
      // etc...
  }
  </code></pre>
  
  <p>I created a model factory, a nice new feature as of Laravel 5.1 to define the blueprint of a <code>Post</code> as follows:</p>
  
  <pre><code>$factory-&gt;define('App\Post', function($faker) {
      return [
      'title'     =&gt; $faker-&gt;sentence,
      'content'   =&gt; $faker-&gt;paragraph,
      'published_at' =&gt; $faker-&gt;dateTimeThisMonth(),
      ];
  });
  </code></pre>
  
  <p>As we can see I set the <code>published_at</code> field to be a random date this month, as generated by the <code>Faker</code> instance. The method used returns an instance of <code>DateTime</code>.</p>
  
  <p>However when I generate some <code>Post</code>s I find the <code>published_at</code> field to be null, suggesting that the <code>DateTime</code> instance is not being correctly interpreted.</p>
  
  <p>I found that if I changed that line to:</p>
  
  <pre><code>'published_at' =&gt; Carbon::instance($faker-&gt;dateTimeThisMonth())-&gt;toDateTimeString(),
  </code></pre>
  
  <p>which essentially converts the <code>DateTime</code> to a <code>Carbon</code> and then outputs a date-time string e.g. <code>"2015-06-15 13:44:23"</code>, it works correctly.</p>
  
  <p>Is this really the best way to define faker dates in model factories? I would've thought the fact that I defined <code>published_at</code> in the <code>$dates</code> array Laravel would interpret the <code>DateTime</code> (or <code>Carbon</code>) instance and save it without me having to provide a string.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I'm finding that this is also not working for simple Eloquent creates:</p>
  
  <p>E.g. if I run</p>
  
  <pre><code>App\Post::create([
      'title' =&gt; $title,
      'content' =&gt; $faker-&gt;paragraph(4),      
      'published_at' =&gt; new Carbon,
      'created_at' =&gt; Carbon::parse('2015-06-16')
  ]);
  </code></pre>
  
  <p>The <code>published_at</code> and <code>created_at</code> field are still null. Any ideas?</p>