Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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.

0

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.

0

... 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?

Last updated 8 years ago.
0

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);
Last updated 8 years ago.
0

Thanks for the hints, it worked. I pasted the code here

http://pastebin.com/zn8WpeAR

0

Sign in to participate in this thread!

Eventy

Your banner here too?

chriz74x chriz74x Joined 8 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.