ÔõÑùʹÓÃLaravelµÄÌî³äÊý¾Ý¹¦Ð§
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ÌṩÁËÐí¶àÓÐÓõĹ¦Ð§ºÍ¹¤¾ß£¬ÆäÖÐÒ»¸öÖ÷ÒªµÄÌØÕ÷ÊÇÌî³äÊý¾Ý¡£Ìî³äÊý¾ÝÊÇÖ¸½«Êý¾Ý¿â±íÖеÄÊý¾ÝÓÃһЩԤ½ç˵µÄÖµÌî³ä£¬ÒÔ±ã²âÊԺͿª·¢¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravelµÄÌî³äÊý¾Ý¹¦Ð§¡£
Ò»¡¢×¼±¸ÊÂÇé
ÔÚʹÓÃLaravelµÄÌî³äÊý¾Ý¹¦Ð§Ö®Ç°£¬ÐèÒªÏȽ¨ÉèÒ»¸öÊý¾Ý¿â±íºÍÒ»¸öEloquentÄ£×Ó¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£º
php artisan make:model User -m
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁÔÚappĿ¼Ï½¨ÉèÒ»¸öUserÄ£×ÓºÍÒ»¸öÊý¾Ý¿âǨáãÎļþxxxx_xx_xx_xxxxxx_create_users_table.php¡£ÎÒÃÇÐèÒªÔÚǨáãÎļþÖнç˵±í½á¹¹£º
public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
µÇ¼ºó¸´ÖÆ
È»ºóÔËÐÐÊý¾Ý¿âǨá㣺
php artisan migrate
µÇ¼ºó¸´ÖÆ
Õ⽫½¨ÉèÒ»¸öÃûΪusersµÄÊý¾Ý±í¡£
¶þ¡¢Ìî³äÊý¾Ý
LaravelÌṩÁËÒ»¸ödb:seedÏÂÁîÓÃÓÚÌî³äÊý¾Ý¡£ÎÒÃÇ¿ÉÒÔÔÚdatabase/seedsĿ¼Ï½¨ÉèÒ»¸öSeederÀ࣬¼ÌÐøIlluminate\Database\SeederÀ࣬²¢ÊµÏÖrunÒªÁì¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýUserSeeder.php£º
use Illuminate\Database\Seeder; use App\User; class UserSeeder extends Seeder { public function run() { factory(User::class, 50)->create(); } }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëʹÓÃLaravelµÄ¹¤³§Ä£Ê½ÌìÉúÁË50¸öUserÄ£×Ó£¬²¢½«ËüÃÇ´æ´¢µ½Êý¾Ý¿âÖС£
ÔÚrunÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelµÄÅÌÎʹ¹½¨Æ÷ºÍEloquentÄ£×ÓÀ´¶ÔÊý¾Ý¿â¾ÙÐвÙ×÷¡£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇʹÓÃÁËfactoryº¯ÊýÀ´½¨Éè50¸öUserÄ£×ÓʵÀý£¬²¢½«ËüÃÇÉúÑĵ½Êý¾Ý¿âÖС£
Èý¡¢Å²ÓÃSeeder
½¨ÉèºÃÁËSeederÀàÖ®ºó£¬ÎÒÃÇÐèҪŲÓÃËüÀ´Ìî³äÊý¾Ý¡£ÎÒÃÇ¿ÉÒÔÔÚdatabase/seeds/DatabaseSeeder.phpÎļþÖÐŲÓÃËùÓÐÐèÒªÌî³äµÄÊý¾Ý¡£Õâ¸öÀàÊÇÒ»¸öÖ÷ÒªµÄseeder£¬ËüÓÃÓÚŲÓÃÆäËûSeederÀà¡£
use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run() { $this->call(UserSeeder::class); } }
µÇ¼ºó¸´ÖÆ
ŲÓÃUserSeederÀàºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÁîÐÐÖ´ÐÐdb:seedÏÂÁîÀ´Ìî³äÊý¾Ý£º
php artisan db:seed
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»á×Ô¶¯ÕÒµ½ËùÓнç˵µÄSeederÀ࣬²¢ÔËÐÐËüÃǵÄrunÒªÁì¡£
ËÄ¡¢Ê¹ÓÃFactory
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËLaravelµÄ¹¤³§Ä£Ê½À´ÌìÉú50¸öUserÄ£×ÓʵÀý¡£LaravelµÄ¹¤³§Ä£Ê½¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØÌìÉú²âÊԺͿª·¢ËùÐèµÄÊý¾Ý¡£
ÎÒÃÇ¿ÉÒÔʹÓÃmakeºÍcreateÒªÁ콨ÉèÄ£×Ó¡£makeÒªÁìÓÃÓÚ½¨ÉèÒ»¸öÔÝʱµÄ¡¢Î´ÉúÑĵ½Êý¾Ý¿âµÄÄ£×ÓʵÀý£¬createÒªÁìÓÃÓÚ½¨ÉèÒ»¸öÉúÑĵ½Êý¾Ý¿âµÄÄ£×ÓʵÀý¡£
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃFactoryÀàÀ´½ç˵¡¢×¢²áºÍʹÓù¤³§¡£FactoryÀàµÄʹÓúÜÊǼòÆÓ£¬ÎÒÃÇÖ»ÐèÒªÔÚdatabase/factoriesĿ¼Ï½¨ÉèÒ»¸ö¹¤³§Îļþ£¬Í¨¹ýdefineÒªÁì½ç˵һ¸öÄ£×Ó¼°ÆäÊôÐÔ¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýUserFactory.php£º
use Faker\Generator as Faker; use App\User; $factory->define(User::class, function (Faker $faker) { return [ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'password' => bcrypt('password'), ]; });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇʹÓÃÁËFakerÀàÀ´ÌìÉúËæ»úµÄÄ£×ÓÊý¾Ý£¬Ä£ÄâÕæʵµÄÓû§Êý¾Ý¡£Õâ¸ö¹¤³§½ç˵ÁËname¡¢emailºÍpasswordÈý¸öÊôÐÔ¡£
µ±ÎÒÃÇÐèҪʹÓÃÕâ¸ö¹¤³§Ê±£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
use App\User; $user = factory(User::class)->create();
µÇ¼ºó¸´ÖÆ
Õâ»á½¨ÉèÒ»¸öеÄUserÄ£×ÓʵÀý£¬²¢½«ÆäÉúÑĵ½Êý¾Ý¿âÖС£ÎÒÃÇÒ²¿ÉÒÔʹÓÃmakeÒªÁìÀ´½¨ÉèÒ»¸öδÉúÑĵ½Êý¾Ý¿âµÄÄ£×ÓʵÀý£º
use App\User; $user = factory(User::class)->make();
µÇ¼ºó¸´ÖÆ
Õâ¸öÄ£×ÓʵÀý½«°üÀ¨Ëæ»úÌìÉúµÄÊôÐÔÖµ£¬ÎÒÃÇ¿ÉÒÔÔÚ²âÊԺͿª·¢ÖÐʹÓÃËü¡£
Îå¡¢×ܽá
LaravelÌṩÁËÀû±ãµÄÌî³äÊý¾Ý¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʹÓÃSeederÀ´ÌìÉú²âÊÔÊý¾Ý£¬Í¬Ê±Ê¹Óù¤³§Ä£Ê½À´ÌìÉúÄ£×ÓʵÀý¡£Í¨¹ýÕâ¸ö¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔÇáËɵØÌî³äÊý¾Ý¿â±í£¬ÒÔ±ãÓÚ²âÊԺͿª·¢¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravelµÄÌî³äÊý¾Ý¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡