Laravel Model Factory and Eloquent; setting dates

So, I have a model App\Postin my application, which is defined as follows:

namespace App;
class Post extends Model 
{

    use SoftDeletes;

    protected $dates = ['created_at', 'updated_at', 'published_at', 'deleted_at'];
    // etc...
}

I created a factory model, a new new function, like Laravel 5.1, to define a circuit Postas follows:

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

As we can see, I set the field published_atas a random date this month generated by the instance Faker. The method used returns an instance DateTime.

However, when I generate some Post, I find the field published_atto be zero, suggesting that the instance is DateTimemisinterpreted.

I found that if I changed this line to:

'published_at' => Carbon::instance($faker->dateTimeThisMonth())->toDateTimeString(),

DateTime Carbon, , . "2015-06-15 13:44:23", .

? , published_at $dates Laravel DateTime ( Carbon) .

Edit

, Eloquent:

.

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

published_at created_at - . ?

+4
2

Faker DateTime-Object, , ​​ mysql, (Y-m-d H:i:s), null.

date, :

$faker->dateTimeThisMonth()->format('Y-m-d H:i:s')

, string '2015-06-19 13:07:07' (length=19)

+2

Carbon .

, published_at created_at $fillable.

.

0

All Articles