It's because you use the Query Builder to insert. The timestamps is a part of Eloquent.
If you need the timestamps you have to do it manually or use Eloquent to insert your data.
So it's because the seeder is not "creating" the data in the DB but "inserting" it. So, the point is here:
DB::table('areas')->insert($areas);
how would you change that to use the 'create' method instead? If it's possible.
... ok let's say this line
$models = csv_to_array($csvFile);
creates some arrays like this
Array ( [brand_id] => 1 [model_name] => some_name [color_id] => 5 [price] => 7 [currency] => USD
)
then DB::table('models')->insert($models);
insert that data into the DB
the point is the method used to create and not insert the data is
Models::create(array ('brand_id' => '1' , 'model_name' => some_name, ... ) ... );
So the point here is to create some sort of for loop to create the models one by one reading from $models. The point is that I can't figure that out. Anyone?
If you have created a model for your table you can just change this line:
DB::table('[models]')->insert($[models]);
to this:
foreach ($models as $model) {
Model::create($model);
}
Or you can add created_at and updated_at manually to your array by looping through them and creating a new array:
$newArr = [];
foreach ($models as $key => $model) {
$newArr[$key] = $model;
$newArr[$key]['created_at'] = \Carbon\Carbon::now();
$newArr[$key]['updated_at'] = \Carbon\Carbon::now();
}
DB::table('models')->insert($newArr);
Thanks for the hints, it worked. I pasted the code here
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community