I have been trying to get seeding set up with the first table on my site using the model factory. The seeds that I entered manually work, but I wanted to make sure I could seed using the factory, as well, for future tests and all that jazz. The manual entries work just fine, but creating using the model factory results in an SQL statement that is only updating the timestamps. It inserts a blank row (except for the timestamps), then fails because of a unique constraint on one of the columns, so it can't enter another blank row. I've not been able to figure out why it's not generating the correct inserts, even though the factory is actually creating a good set of data. (As tested by printing out the array.) Any help on how to troubleshoot this would be most welcome.
Model Factory:
$factory->define(App\Models\Site::class, function (Faker\Generator $faker) {
$site = new App\Models\Site;
$siteTypes = array_keys($site->siteTypes);
$return = [
'site_name' => $faker->word
, 'site_url' => $faker->url
, 'site_type' => $faker->randomElement($siteTypes)
, 'is_active' => round(rand(0,1))
, 'created_at' => $faker->unixTime
, 'updated_at' => $faker->unixTime
];
return $return;
});
DatabaseSeeder.php:
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Models\Site;
class DatabaseSeeder extends Seeder
{
public function run() {
Model::unguard();
DB::delete('delete from sites');
DB::statement('alter table sites auto_increment = 1');
$this->call(SiteTableSeeder::class);
DB::statement('alter table sites auto_increment = 10000');
Model::reguard();
}
}
SiteTableSeeder:
class SiteTableSeeder extends Seeder
{
public function run()
{
<snip out the other manual seeds>
DB::table('sites')->insert([
'site_id' => 9999,
'site_name' => 'Generic Online Site',
'site_url' => 'http://generic.nextworth.com',
'site_type' => 'online',
'is_active' => 1,
]);
factory('App\Models\Site', 5)->create();
}
}