laravel ÐÞ¸ÄÊý¾Ý¿â
laravelÊÇÒ»¿î¼«ÊܽӴýµÄphp webÓ¦ÓóÌÐò¿ò¼Ü£¬ËüÌṩÁ˸»ºñµÄ¹¤¾ßºÍ¹¦Ð§£¬Ê¹¿ª·¢Ö°Ô±Äܹ»ÇáËɹ¹½¨³ö¸ßЧ¡¢Ò×ÓÚά»¤µÄwebÓ¦ÓóÌÐò¡£¶øÆäÖÐÒ»¸öÒªº¦µÄ¹¦Ð§¾ÍÊÇÓëÊý¾Ý¿âµÄ½»»¥²Ù×÷¡£ÔÚʹÓÃlaravel¾ÙÐпª·¢Ê±£¬ÎÒÃǾ³£ÐèÒª¶ÔÊý¾Ý¿â¾ÙÐÐÐ޸ĻòÕ߸üУ¬±¾ÆªÎÄÕ¾ͽ«´ø¸÷ÈËÏêϸÏàʶlaravelÐÞ¸ÄÊý¾Ý¿âµÄÏà¹Ø²Ù×÷¡£
Ò»¡¢LaravelÊý¾Ý¿âǨáã
LaravelÌṩÁËÊý¾Ý¿âǨá㣨Migration£©¹¦Ð§£¬Ê¹µÃÎÒÃÇ¿ÉÒÔÇáËɵÄÖÎÀíÊý¾Ý¿âµÄ°æ±¾¿ØÖÆ¡£Í¨¹ýÊý¾Ý¿âǨá㣬ÎÒÃÇ¿ÉÒÔ½ç˵Êý¾Ý¿â±í½á¹¹µÄת±ä£¬²¢Í¨¹ý¿ò¼ÜÌṩµÄÏÂÁî¿ìËÙǨáãÖÁа汾µÄÊý¾Ý¿â¡£Ïêϸ°ì·¨ÈçÏ£º
½¨ÉèÊý¾Ý¿âǨáãÎļþ
LaravelµÄǨáãÎļþ´æ´¢ÔÚdatabase/migrationsĿ¼Ï£¬ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÀ´ÌìÉúǨáãÎļþ¡£Ö´ÐÐÒÔÏÂÏÂÁî¿ÉÒÔ½¨ÉèÒ»¸öÃûΪcreate_users_tableµÄǨáãÎļþ£º
php artisan make:migration create_users_table
±àдǨáã¾ç±¾
·¿ª¸Õ¸ÕÌìÉúµÄǨáãÎļþ£¨Î»ÓÚdatabase/migrationsĿ¼Ï£©£¬¿ÉÒÔ¿´µ½ÓÐÒ»¸öupº¯ÊýºÍÒ»¸ödownº¯Êý¡£upº¯ÊýÓÃÓÚÖ´ÐÐǨáã²Ù×÷£¬¶ødownº¯ÊýÓÃÓÚ×÷·ÏǨáã²Ù×÷¡£
ÎÒÃÇ¿ÉÒÔÔÚupº¯ÊýÖбàд½¨ÉèÊý¾Ý¿â±íµÄ¾ç±¾£¬ÀýÈ磺
Schema::create(‘users’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->string(’email’)->unique();
$table->timestamp(’email_verified_at’)->nullable();
$table->string(‘password’);
$table->rememberToken();
$table->timestamps();
});
¸Ã¾ç±¾½«½¨ÉèÒ»¸öÃûΪusersµÄÊý¾Ý±í£¬¸Ã±í°üÀ¨id¡¢name¡¢email¡¢email_verified_at¡¢password¡¢remember_tokenºÍtimestampsµÈ7¸ö×ֶΡ£
ÔËÐÐǨáã¾ç±¾
±àдÍêǨáã¾ç±¾ºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´ÔËÐÐǨáã¾ç±¾£º
php artisan migrate
´Ëʱ£¬Laravel¿ò¼Ü»á×Ô¶¯Æ¾Ö¤ÎÒÃDZàдµÄ½ÅÔÀ´½¨Éè±í½á¹¹£¬ÈôÊÇÊý¾Ý¿âÖÐÒѾ±£´æÁËÏà¹Ø±í£¬¿ò¼Ü»á×Ô¶¯Ìø¹ý²»»áÖ´ÐС£
¶þ¡¢LaravelÊý¾Ý¿âÌî³ä
Êý¾Ý¿âÌî³äÊÇLaravelÌṩµÄÁíÒ»¸öÖ÷Òª¹¦Ð§£¬¿ÉÒÔ×ÊÖúÎÒÃÇÔÚ¿ª·¢¡¢²âÊÔºÍÉú²úÇéÐÎÖÐÓüÙÊý¾ÝÌî³äÊý¾Ý¿â¡£ÀýÈ磬µ±ÎÒÃÇÐèÒª²âÊÔÊý¾ÝµÄÅÌÎÊÐÔÄÜʱ£¬¿ÉÒÔʹÓÃÌî³äÀ´Ìî³ä´ó×ÚÊý¾Ý¡£
½¨ÉèÊý¾ÝÌî³äÎļþ
ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÀ´½¨ÉèÌî³äÎļþ£¬ÈçÏ£º
php artisan make:seeder UsersTableSeeder
±àдÌî³ä¾ç±¾
½«±àдµÄÌî³ä¾ç±¾·Åµ½database/seedsĿ¼Ï£¬ÔÚUsersTableSeederÖбàдÈçÏ´úÂ룺
use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesHash;
use IlluminateSupportStr;
class UsersTableSeeder extends Seeder
{
/** * Run the database seeds. * * @return void */ public function run() { DB::table('users')->insert([ 'name' => Str::random(10), 'email' => Str::random(10).'@gmail.com', 'password' => Hash::make('password'), ]); }
µÇ¼ºó¸´ÖÆ
}
¸Ã¾ç±¾»áÏòusers±íÖвåÈëÒ»Ìõ¼Í¼£¬°üÀ¨name¡¢email¡¢passwordÈý¸ö×ֶΣ¬ÆäÖÐnameºÍemail×ֶεÄÖµÊÇËæ»úÌìÉúµÄ¡£
ÔËÐÐÌî³ä¾ç±¾
ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´ÔËÐÐÌî³ä¾ç±¾£º
php artisan db:seed
´Ëʱ£¬Laravel¿ò¼Ü»á×Ô¶¯Å²ÓÃÎÒÃDZàдµÄÌî³ä¾ç±¾£¬È»ºó½«Êý¾Ý²åÈëµ½users±íÖС£
Èý¡¢LaravelÊý¾Ý¿â²Ù×÷
³ýÁËʹÓÃÊý¾Ý¿âǨáãºÍÊý¾ÝÌî³äÀ´ÐÞ¸ÄÊý¾Ý¿âÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃLaravelÌṩµÄDBÀàÀ´¾ÙÐÐÊý¾Ý²Ù×÷¡£
²åÈëÊý¾Ý
DB::table(‘users’)->insert([
'name' => 'John Doe', 'email' => 'johndoe@example.com', 'password' => Hash::make('password'),
µÇ¼ºó¸´ÖÆ
]);
¸Ã¾ç±¾»áÏòusers±íÖвåÈëÒ»Ìõ¼Í¼£¬°üÀ¨name¡¢email¡¢passwordÈý¸ö×ֶΣ¬ÆäÖÐnameºÍemail×ֶεÄÖµ¶¼ÊÇÖ¸¶¨µÄ¡£
¸üÐÂÊý¾Ý
DB::table(‘users’)
->where('id', 1) ->update(['votes' => 1]);
µÇ¼ºó¸´ÖÆ
¸ÃÓï¾ä½«»á½«idΪ1µÄ¼Í¼ÖеÄvotes×Ö¶ÎÖµÉèΪ1¡£
ɾ³ýÊý¾Ý
DB::table(‘users’)->where(‘votes’, ‘delete();
¸ÃÓï¾ä½«»áɾ³ývotes×Ö¶ÎֵСÓÚ100µÄËùÓмͼ¡£
ËÄ¡¢½áÓï
±¾ÎÄÏÈÈÝÁËLaravelÔõÑùÐÞ¸ÄÊý¾Ý¿â£¬»®·ÖÏÈÈÝÁËÊý¾Ý¿âǨáã¡¢Êý¾Ý¿âÌî³äºÍÊý¾Ý¿â²Ù×÷µÈÈýÖÖ·½·¨¡£LaravelÖд¦ÀíÊý¾Ý¿âµÄ¼òÆӺ͸ßЧÐÔʹÆä³ÉΪÁËÊ¢ÐеÄWeb¿ò¼ÜÖ®Ò»£¬¹ØÓÚ¿ª·¢Ö°Ô±À´Ëµ£¬Ñ§Ï°ÕâЩÏà¹Ø¹¦Ð§Ò²ÊǺÜÊÇÐëÒªµÄ¡£
ÒÔÉϾÍÊÇlaravel ÐÞ¸ÄÊý¾Ý¿âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡