laravelºó¶ËÊý¾Ý¿â´î½¨Á÷³Ì
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ÇÒÔÚwebÓ¦ÓóÌÐò¿ª·¢·½ÃæÌṩÁËÐí¶à±ãµ±¡£laravelÖ§³ÖÐí´ó¶¼¾Ý¿âÊÖÒÕ£¬°üÀ¨mysql¡¢postgresqlºÍsqlite¡£±¾ÆªÎÄÕ½«ÏÈÈÝÔõÑùʹÓÃlaravelÓëmysqlÊý¾Ý¿â¾ÙÐн»»¥£¬Ö÷Òª°üÀ¨ÒÔϼ¸¸ö°ì·¨£º
×°ÖÃMySQL
½¨ÉèÊý¾Ý¿â
ÉèÖÃLaravelÇéÐαäÁ¿
½¨ÉèǨáã
ÔËÐÐǨáã
×°ÖÃMySQL
Ê×ÏÈÒªÔÚÍâµØÅÌËã»úÉÏ×°ÖÃMySQL£¬¿ÉÒÔʹÓùٷ½µÄMySQL×°ÖóÌÐò£¬»òÕßʹÓÃÀàËÆXAMPPµÄÈí¼þ°ü¡£ÎªÁËʹÓÃLaravel£¬ÐèÒª½«MySQLµÄ×°Ö÷¾¶Ìí¼Óµ½ÅÌËã»úµÄÇéÐαäÁ¿ÖС£ÔÚWindowsϵͳÉÏ£¬¿ÉÒÔÕâÑù²Ù×÷£º
1£©·¿ª¿ØÖÆÃæ°å -> ϵͳÓëÇå¾² -> ϵͳ -> ¸ß¼¶ÏµÍ³ÉèÖà -> ÇéÐαäÁ¿
2£©ÔÚ¡°ÏµÍ³±äÁ¿¡±ÖУ¬ÕÒµ½¡°Path¡±£¬²¢½«MySQLµÄbinÎļþ¼Ð·¾¶Ìí¼Óµ½Ä©Î²¡£ÀýÈ磺
C:Program FilesMySQLMySQL Server 8.0in;
µÇ¼ºó¸´ÖÆ
½¨ÉèÊý¾Ý¿â
ʹÓÃMySQL¿Í»§¶Ë³ÌÐò£¨ºÃ±ÈMySQLÏÂÁîÐпͻ§¶Ë»òphpMyAdmin£©½¨ÉèÒ»¸öеÄÊý¾Ý¿â£¬ÀýÈ磺
CREATE DATABASE laravel_db;
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÐèÒªÔÚLaravelÏîÄ¿ÖÐÉèÖÃÊý¾Ý¿âÅþÁ¬¡£
ÉèÖÃLaravelÇéÐαäÁ¿
ʹÓÃÎı¾±à¼Æ÷·¿ªLaravelÏîÄ¿µÄ.envÎļþ£¬ÕâÊÇLaravelʹÓõÄÇéÐαäÁ¿Îļþ¡£ÔÚ¸ÃÎļþÖУ¬¿ÉÒÔÉèÖÃÓ¦ÓóÌÐòËùÐèµÄÖÖÖÖ±äÁ¿ºÍ²ÎÊý£¬°üÀ¨Êý¾Ý¿âÅþÁ¬ÐÅÏ¢¡£
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=root DB_PASSWORD=
µÇ¼ºó¸´ÖÆ
ÕâЩ²ÎÊý½ÏÁ¿ÈÝÒ×Ã÷È·¡£DB_CONNECTIONÖ¸¶¨Ê¹ÓÃÄÄÖÖÊý¾Ý¿âϵͳ£¬ÀýÈçMySQL£»DB_HOST¡¢DB_PORTºÍDB_DATABASEÖ¸¶¨ÅþÁ¬MySQLµÄÖ÷»úÃû¡¢¶Ë¿ÚºÅºÍÊý¾Ý¿âÃû³Æ£¬¶øDB_USERNAMEºÍDB_PASSWORDÔòÊÇÅþÁ¬Óû§ÃûºÍÃÜÂë¡£±ØÐèÈ·±£ÕâЩֵÓëMySQLµÄÉèÖÃÏàÆ¥Å䣬²Å¿ÉÒÔÀÖ³ÉÅþÁ¬µ½Êý¾Ý¿â¡£
½¨ÉèǨáã
ǨáãÊÇLaravelÖÐÊý¾Ý±íµÄ½¨Éè¡¢Ð޸ĺÍɾ³ýµÄ»úÖÆ¡£Í¨¹ýǨá㣬¿ÉÒÔÔÚÈκÎÇéÐÎÖÐÍê³ÉÕâЩ²Ù×÷£¬²¢ÇáËɵؽ«ÕâЩ¸ü¸ÄÓ¦ÓÃÓÚ²î±ðµÄЧÀÍÆ÷¡£
ÔÚLaravelÖУ¬Ç¨áã´æ´¢ÔÚdatabase/migrationsĿ¼ÖС£¿ÉÒÔʹÓÃArtisanÏÂÁîÐй¤¾ß½¨ÉèеÄǨáãÎļþ¡£ºÃ±È£¬ÏÂÃæÕâ¸öÏÂÁ½¨ÉèÒ»¸ö½¨Éèusers±íµÄǨá㣺
php artisan make:migration create_users_table
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚmigrationsĿ¼Öн¨ÉèÒ»¸öÐÂÎļþ£¬ÃûΪ2020_07_30_135314_create_users_table.php£¨Ê±¼ä´Á¿ÉÄÜ»áÓÐËù²î±ð£©¡£·¿ª¸ÃÎļþ¿ÉÒÔ¿´µ½LaravelÌṩÁËһЩģ°å´úÂë¡£
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÔÚup()ÒªÁìÖнç˵ÁËÒ»¸öÃûΪusersµÄ±í£¬²¢Ö¸¶¨Á˸ñíµÄ¸÷¸ö×ֶΡ£id()ÒªÁì½ç˵ÁËÖ÷¼ü£¬name()ºÍemail()ÒªÁì½ç˵ÁËͨË××Ö·û´®×ֶΣ¬timestamp()ÒªÁì½ç˵ÁËʱ¼ä´Á£¬rememberToken()ÒªÁì½ç˵ÁËÓÃÓÚ¡°¼Ç×ÅÎÒ¡±»úÖƵÄtoken×ֶΣ¬×îºóÒ»¸ötimestamps()ÒªÁì½ç˵ÁËÁ½¸öʱ¼ä´Á×ֶΣ¨created_atºÍupdated_at£©¡£
¿ÉÒÔƾ֤ÐèÒªÐÞ¸ÄÕâ¸öÄ£°å´úÂ룬ÀýÈçÌí¼ÓÆäËû×ֶλòÐÞ¸ÄÏÖÓÐ×ֶΡ£Ò»µ©×¼±¸ºÃÁËǨá㣬¾Í¿ÉÒÔÔËÐÐËü¡£
ÔËÐÐǨáã
ΪÁËÔÚÊý¾Ý¿âÖн¨Éèusers±í£¬ÐèÒªÔËÐÐLaravelµÄǨáã»úÖÆ¡£¿ÉÒÔʹÓÃÏÂÃæµÄArtisanÏÂÁîÐй¤¾ßÔËÐÐÖ¸¶¨µÄǨáãÎļþ£º
php artisan migrate --path=/database/migrations/create_users_table.php
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁ´òÓ¡Êä³öǨáãµÄ״̬£¬°üÀ¨Ö´ÐÐÀÖ³ÉÕÕ¾Éʧ°Ü£¬ÒÔ¼°¶ÔÄÇЩ±í×öÁËÏÖʵµÄÐ޸ġ£
ÏÖÔÚ£¬¾Í¿ÉÒÔÔÚLaravelÖÐʹÓÃusers±íÁË¡£¿ÉÒÔÔÚ¿ØÖÆÆ÷¡¢Ä£×Ó»òÆäËû²¿·ÖÖÐʹÓÃEloquent¹¤¾ß£¬½«Êý¾Ý²åÈë±íÖС¢ÅÌÎʺÍÅÌÎʱíÖеÄÊý¾Ý¡¢¸üÐÂÊý¾ÝµÈ¡£
×ܽá
ÒÔÉϾÍÊÇLaravelºó¶ËÊý¾Ý¿â´î½¨µÄÁ÷³Ì¡£ÔÚÏÖʵʹÓÃʱ£¬»¹ÐèÒª¸üÉîÈëµØÏàʶEloquentµÄʹÓÃÒªÁ죬ÒÔ¼°ÔõÑùÓëMySQL¡¢PostgreSQLµÈÊý¾Ý¿âϵͳ¾ÙÐиü¸ß¼¶ºÍ¸üÖØ´óµÄ½»»¥¡£ÈôÊÇʹÓÃLaravel¹¹½¨Ò»¸ö´óÐ͵ÄwebÓ¦ÓóÌÐò£¬ÕâЩ֪ʶµã¶¼ÊǺÜÊÇÖ÷ÒªµÄ¡£
ÒÔÉϾÍÊÇlaravelºó¶ËÊý¾Ý¿â´î½¨Á÷³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡