×ðÁú¿­Ê±¹ÙÍøµÇ¼

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ÔÚÄĵÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ