Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Model Factories and Eloquent; setting dates

So I have an App\Post model in my app, 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 model factory, a nice new feature as of Laravel 5.1 to define the blueprint of a Post as 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 published_at field to be a random date this month, as generated by the Faker instance. The method used returns an instance of DateTime.

However when I generate some Posts I find the published_at field to be null, suggesting that the DateTime instance is not being correctly interpreted.

I found that if I changed that line to:

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

which essentially converts the DateTime to a Carbon and then outputs a date-time string e.g. "2015-06-15 13:44:23", it works correctly.

Is this really the best way to define faker dates in model factories? I would've thought the fact that I defined published_at in the $dates array Laravel would interpret the DateTime (or Carbon) instance and save it without me having to provide a string.

Edit

I'm finding that this is also not working for simple Eloquent creates:

E.g. if I run

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

The published_at and created_at field are still null. Any ideas?

like image 954
harryg Avatar asked Jun 19 '15 10:06

harryg


People also ask

What is the difference between seeder and factory in Laravel?

Database seeder is used to populate tables with data. Model factories is a convenient centralized place to define how your models should be populated with fake data.

What is use Hasfactory in Laravel?

It is a trait that links a Eloquent model to a model factory. Factories are normally used in testing when wanted test-data for a specific model. You can read more about factories in Laravel here: https://laravel.com/docs/9.x/database-testing#model-factories and here: https://laravel.com/docs/9.x/eloquent-factories.

What is faker in Laravel?

Faker generates fake or dummy data for your application and has built-in capability to generate fake data of the following types: Base. Lorem Ipsum Text. Person.


1 Answers

Since Faker is returning a DateTime-Object which isn't representing a date-string like mysql likes it (Y-m-d H:i:s) the field will be set to null.

You should however be able to access the objects property date to get the correct string like this:

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

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

like image 153
Tim Avatar answered Sep 23 '22 20:09

Tim