Last active
November 6, 2020 08:23
-
-
Save moradi-morteza/e2241b455955a6d10516fa0181ea906c to your computer and use it in GitHub Desktop.
[seed] #LaravelT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// without Factory---------------- | |
// 1- command create | |
php artisan make:seed UserTableSeeder | |
//2- classes | |
class DatabaseSeeder extends Seeder{ | |
public funtion run(){ | |
Schema::disableForeignKeyConstraints(); | |
$this->call(UserTableSeeder::class); | |
Schema::enableForeignKeyConstraints(); | |
} | |
} | |
class UserTableSeeder extends Seeder{ | |
public function run(){ | |
\App\User::truncate(); // this need disable Foreign key in DatabaseSeeder.php | |
foreach($this->getdata() as $item){ | |
\App\User::create($item); | |
}} | |
private function getdata(){return [ | |
['name'=>'morteza','email'=>'[email protected]','password'=>bcrypt('112859')], | |
['name'=>'mabnagroupte','email'=>'[email protected]','password'=>bcrypt('112859')] | |
];} | |
} | |
// 3-run seed | |
php artisan db:seed --class=UserTableSeeder | |
// Use Factory --------------------------------------- | |
php artian make:seed UserTableSeeder | |
php artisan make:factory UserFactory --model=User | |
class DatabaseSeeder extends Seeder{ | |
public funtion run(){ | |
Schema::disableForeignKeyConstraints(); | |
$this->call(UserTableSeeder::class); | |
Schema::enableForeignKeyConstraints(); | |
} | |
} | |
public function run() | |
{ | |
\App\User::truncate(); // remove all previous data | |
$users = factory(\App\User::class,80)->create(); | |
} | |
php artisan db:seed | |
// another way | |
class ProfileTableSeeder extends Seeder | |
{ | |
public function run() | |
{ | |
\App\ProfileModel::truncate(); // remove all previous data | |
// you can send your optimize data like this : | |
$userIds=\App\User::pluck('id')->toArray(); // get list of ids | |
foreach($userIds as $userid){ | |
$users = factory(\App\Profile::class,1)->create([ | |
'user_id'=>$userid; | |
]); | |
} | |
} | |
} | |
//ProfileFactory | |
$factory->define(\App\PostModel::class, function (Faker $faker) { | |
return [ | |
'age' => rand(10,100), | |
'bio' => $faker->realText(100), | |
'score' => rand(1,10), | |
// user_id will be get from sedder line 69 | |
]; | |
}); | |
// add random post | |
class PostTableSeeder extends Seeder | |
{ | |
public function run() | |
{ | |
\App\PostModel::truncate(); // remove all previous data | |
// you can send your optimize data like this : | |
$userIds=\App\User::pluck('id')->toArray(); // get list of ids | |
for($i=0;$i<50;$i++){ | |
$users = factory(\App\Post::class,1)->create([ | |
'user_id'=>$array_random($userid); | |
]); | |
} | |
} | |
} | |
//PostFactory | |
$factory->define(\App\PostModel::class, function (Faker $faker) { | |
return [ | |
'title' => $faker->realText(20), | |
'des' => $faker->realText(100), | |
// user_id will be get from sedder line 97 | |
]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment