laravelµÄdbÔÚÄÄ
ÔÚlaravelÖУ¬dbÊÇÖ¸Êý¾Ý¿â£¬Ò»Ñùƽ³£´æ·ÅÔÚmysql¡¢postgresql¡¢sqliteµÈ¹ØϵÐÍÊý¾Ý¿âÖС£ÄÇô£¬ÔÚlaravel¿ò¼ÜÖУ¬dbÔÚÄÇÀïÄØ£¿
Laravel¿ò¼ÜÖеÄÊý¾Ý¿âÉèÖÃ
ÔÚLaravelÖУ¬Êý¾Ý¿âÉèÖÃÎļþλÓÚ/config/database.php£¬ÆäĬÈϵÄÉèÖÃÈçÏ£º
‘default’ => env(‘DB_CONNECTION’, ‘mysql’),
‘connections’ => [
'sqlite' => [ 'driver' => 'sqlite', 'url' => env('DATABASE_URL'), 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ], 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ], 'pgsql' => [ 'driver' => 'pgsql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'prefix_indexes' => true, 'schema' => 'public', 'sslmode' => 'prefer', ], 'sqlsrv' => [ 'driver' => 'sqlsrv', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '1433'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'prefix_indexes' => true, ],
µÇ¼ºó¸´ÖÆ
],
ÔÚĬÈÏÉèÖÃÖУ¬defaultÌåÏÖĬÈϵÄÊý¾Ý¿âÁ´½ÓÀàÐÍÊÇmysql£¬connectionsÏÂÃæµÄ×ÓÊý×é½ç˵ÁËËÄÖÖÊý¾Ý¿âÁ´½Ó·½·¨£ºsqlite¡¢mysql¡¢pgsql¡¢sqlsrv¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇóÌí¼Ó¸ü¶àµÄÊý¾Ý¿âÁ´½Ó·½·¨¡£
Laravel¿ò¼ÜÖеÄDB½Ó¿Ú
Laravel¿ò¼ÜÖÐΪÎÒÃÇÌṩÁ˺ÜÊǶàµÄDB½Ó¿Ú£¬ÆäÖÐ×îÖ÷ÒªµÄDB½Ó¿ÚÊÇIlluminateDatabaseEloquentModel£¬ËüΪÎÒÃÇÌṩÁ˶ÔÊý¾Ý¿â¾ÙÐÐÅÌÎʺͲÙ×÷µÄ»ù±¾ÒªÁ죬È磺all()¡¢create()¡¢update()¡¢where()¡¢orderBy()µÈ¡£
ÔÚʹÓÃLaravel¾ÙÐÐÊý¾Ý¿â²Ù×÷ʱ£¬ÎÒÃÇÐèÒªÏÈÒýÈëDB½Ó¿Ú£¬Í¨³£µÄд·¨Îª£º
use IlluminateDatabaseEloquentModel;
È»ºóÎÒÃǾͿÉÒÔ¶ÔÊý¾Ý¿â¾ÙÐвÙ×÷£¬ÀýÈ磺
$user = User::find(1);
$user->name = ‘new name’;
$user->save();
ÉÏÃæµÄ´úÂë¾ÍÊǸüÐÂÁËIDΪ1µÄÓû§µÄÐÕÃû¡£
Laravel¿ò¼ÜÖеÄDBʵÏÖ
Laravel¿ò¼ÜÖУ¬DBµÄʵÏÖ»ùÓÚPDO£¨PHP Data Objects£©£¬PDOÊÇPHPÖÐÒ»¸öÇáÁ¿¼¶µÄÊý¾Ý¿âÁýͳ²ã£¬ÓÃÓÚ·â×°²î±ðÊý¾Ý¿âÖ®¼äµÄ²î±ð¡£PDOÁýͳµôÁ˲î±ðÊý¾Ý¿âÖ®¼äÓï·¨ÉϵIJî±ð£¬ÈÃÎÒÃÇÄܹ»Ê¹ÓÃÏàͬµÄ´úÂëÕë¶Ô²î±ðµÄÊý¾Ý¿â¾ÙÐвÙ×÷¡£
Laravel¿ò¼ÜÖÐʹÓÃDBʱ£¬×ÅʵҲÊÇͨ¹ýPDOÀ´¶ÔÊý¾Ý¿â¾ÙÐвÙ×÷¡£ÕâÀïҪ˵Ã÷µÄÊÇ£¬Laravel¿ò¼ÜÖÐʹÓõÄPDO²¢²»ÊÇPHP×Ô´øµÄPDO£¬¶øÊÇLaravel×Ô¼ºÊµÏÖµÄPDOÀ©Õ¹£¬Ëü¶ÔÔÉúPDO×öÁËһЩÍØÕ¹ºÍ·â×°£¬ÌṩÁ˸üÒ×ÓᢸüÎÞаµÄAPI¡£
ÒÔÉϾÍÊÇLaravel¿ò¼ÜÖÐDBµÄÏà¹ØÄÚÈÝ£¬°üÀ¨Êý¾Ý¿âµÄÉèÖá¢DB½Ó¿ÚºÍDBʵÏֵȡ£ÔÚʹÓÃLaravel¾ÙÐпª·¢Ê±£¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèÇóÎÞаµÄÉèÖúÍʹÓÃDB£¬À´Íê³ÉÖÖÖÖÖØ´óµÄÊý¾Ý¿â²Ù×÷¡£
ÒÔÉϾÍÊÇlaravelµÄdbÔÚÄĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡