laravel¿ò¼ÜÊý¾Ý¿âÐÞ¸Ä
laravelÊÇÒ»¿îºÜÊÇÊܽӴýµÄphp webÓ¦ÓóÌÐò¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§ºÍ¹¤¾ß£¬ÒÔ¼ò»¯Êý¾Ý¿âÐ޸ĵÄÀú³Ì¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖlaravel¿ò¼ÜµÄÊý¾Ý¿âÐÞ¸ÄÏà¹ØÄÚÈÝ¡£
Êý¾Ý¿âǨáã
ÔÚLaravel¿ò¼ÜÖУ¬Êý¾Ý¿âǨáãÊÇÒ»¸öÀû±ãµÄ¹¤¾ß£¬ÓÃÓÚ½«Êý¾Ý¿âµÄ½á¹¹´ÓÒ»Öְ汾תÒƵ½ÁíÒ»ÖÖ°æ±¾¡£Í¨¹ýʹÓÃǨá㣬Äú¿ÉÒÔÇáËɵؽ¨ÉèºÍÐÞ¸Ä±í¡¢Ìí¼Ó»òɾ³ýÁС¢¸ü¸ÄË÷ÒýºÍÔ¼ÊøµÈ¡£Ç¨áãÎļþ´æ´¢ÔÚ¡°database/migrations¡±Ä¿Â¼Ï¡£Ã¿¸öǨáãÎļþ´ú±íÒ»¸öÊý¾Ý¿âǨáã²Ù×÷£¬²¢°üÀ¨ÓÃÓÚÐÞ¸ÄÊý¾Ý¿âµÄ¡°up¡±ºÍ¡°down¡±ÒªÁ졣ͨ¹ýÔËÐС°php artisan migrate¡±ÏÂÁ½«×Ô¶¯Ö´ÐÐËùÓÐδִÐеÄǨáã²Ù×÷¡£
ÒÔÏÂÊÇÒ»¸öʾÀýǨáãÎļþ£¬ÓÃÓÚ½¨Éè¡°users¡±±í£º
use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
µÇ¼ºó¸´ÖÆ
¸ÃǨáãÎļþʹÓá°Schema¡±ÀàµÄ¾²Ì¬ÒªÁ콨ÉèÁËÒ»¸ö¡°users¡±±í£¬°üÀ¨×ÔÔöID¡¢Ãû³Æ¡¢Î¨Ò»µç×ÓÓʼþ¡¢ÃÜÂëºÍʱ¼ä´ÁµÈÁС£ÔÚ¡°down¡±ÒªÁìÖУ¬É¾³ýÁË¡°users¡±±í¡£
Êý¾ÝÌî³ä
ÔÚLaravel¿ò¼ÜÖУ¬Êý¾ÝÌî³äÊÇÖ¸ÏòÊý¾Ý¿âÌí¼Ó²âÊÔÊý¾ÝµÄÀú³Ì¡£Ê¹ÓÃÊý¾ÝÌî³ä¿É¼ò»¯²âÊÔºÍÑÝʾÊý¾Ý¿â²Ù×÷µÄÀú³Ì¡£Êý¾ÝÌî³äÎļþ´æ´¢ÔÚ¡°database/seeds¡±Ä¿Â¼Ï¡£Ö´ÐС°php artisan db:seed¡±ÏÂÁî¿ÉÒÔÌî³äÊý¾Ý¡£
use IlluminateDatabaseSeeder; use IlluminateSupportFacadesDB; use IlluminateSupportFacadesHash; use AppUser; class UsersTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { DB::table('users')->delete(); $users = array( array( 'name' => 'admin', 'email' => 'admin@admin.com', 'password' => Hash::make('password'), ), array( 'name' => 'user', 'email' => 'user@user.com', 'password' => Hash::make('password'), ), ); foreach ($users as $user) { User::create($user); } } }
µÇ¼ºó¸´ÖÆ
¸ÃÎļþ½¨ÉèÁËÁ½¸öÓû§£¬²¢½«ËüÃÇÌí¼Óµ½Êý¾Ý¿âÖС£×¢ÖØ£¬¡°password¡±ÁÐʹÓÃÁËLaravelµÄ¡°Hash¡±Àà¾ÙÐмÓÃÜ¡£ÔÚÔËÐС°php artisan db:seed¡±ÏÂÁîÖ®ºó£¬¡°users¡±±í½«°üÀ¨ÕâÁ½¸öÓû§µÄ¼Í¼¡£
Ä£×Ó¹Øϵ
ÔÚLaravel¿ò¼ÜÖУ¬Í¨¹ýÔÚÄ£×ÓÖ®¼ä½¨Éè¹ØÁª£¬¿ÉÒÔÇáËÉ»á¼ûÊý¾Ý¿âÖеÄÏà¹ØÊý¾Ý¡£ÓÐÈýÖÖÀàÐ͵ĹØϵ£ºÒ»¶ÔÒ»¡¢Ò»¶Ô¶àºÍ¶à¶Ô¶à¡£ÔÚÄ£×ÓÖнç˵Ïà¹Ø¹Øϵºó£¬Äú¿ÉÒÔÏñ»á¼ûÄ£×Ó×Ô¼ºÒ»Ñù»á¼û¹ØÁªÊý¾Ý¡£
ÀýÈ磬ÈôÊÇÄúÓÐÒ»¸ö¡°User¡±Ä£×ÓºÍÒ»¸ö¡°Post¡±Ä£×Ó£¬Äú¿ÉÒÔÔÚ¡°User¡±Ä£×ÓÖнç˵һ¸öÒ»¶Ô¶à¹Øϵ£¬²¢ÇáËɵػá¼ûÓû§½¨ÉèµÄËùÓÐÌû×Ó£º
class User extends Model { /** * Get the posts for the user. */ public function posts() { return $this->hasMany(Post::class); } } $user = User::find(1); foreach ($user->posts as $post) { echo $post->title; }
µÇ¼ºó¸´ÖÆ
¸Ã´úÂë´ÓÊý¾Ý¿âÖмìË÷Ò»¸öÓû§£¬È»ºóͨ¹ý¸ÃÓû§µÄ¡°posts¡±ÊôÐÔ»á¼û¸ÃÓû§½¨ÉèµÄËùÓÐÌû×Ó¡£¿ÉÒÔÔÚ¡°Post¡±Ä£×ÓÖнç˵һ¸ö·´Ïò¹Øϵ£¬ÒÔ±ãÔÚ»á¼ûÌû×ÓʱÇáËɵػá¼ûÆ佨ÉèÓû§µÄÐÅÏ¢¡£
×ܽá
LaravelÌṩÁËÐí¶àÀû±ãµÄ¹¤¾ßºÍ¹¦Ð§£¬ÓÃÓÚ¼ò»¯Êý¾Ý¿âÐ޸ĵÄÀú³Ì¡£Í¨¹ýʹÓÃǨá㣬Äú¿ÉÒÔÇáËɵؽ¨ÉèºÍÐÞ¸Ä±í¡¢Ìí¼Ó»òɾ³ýÁС¢¸ü¸ÄË÷ÒýºÍÔ¼ÊøµÈ¡£Ê¹ÓÃÊý¾ÝÌî³ä¿É¼ò»¯²âÊÔºÍÑÝʾÊý¾Ý¿â²Ù×÷µÄÀú³Ì¡£Í¨¹ýÔÚÄ£×ÓÖ®¼ä½¨Éè¹ØÁª£¬¿ÉÒÔÇáËÉ»á¼ûÊý¾Ý¿âÖеÄÏà¹ØÊý¾Ý¡£ÔÚÄú×îÏȾÙÐÐÊý¾Ý¿âÐÞ¸Ä֮ǰ£¬ÇëÈ·±£±¸·ÝÖ÷ÒªµÄÊý¾Ý£¬²¢Ð¡ÐIJÙ×÷¡£
ÒÔÉϾÍÊÇlaravel¿ò¼ÜÊý¾Ý¿âÐ޸ĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡