laravelÔõô¶ÁдÊèÉ¢
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¦Ð§ºÍ¹¤¾ßÀ´¿ª·¢ºÍά»¤webÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸öÒªº¦µÄ¹¦Ð§ÊÇÊý¾Ý¿â¶ÁдÊèÉ¢¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÓ¦ÓóÌÐòÖÐʵÏÖ¶ÁдÊèÉ¢¡£
ʲôÊǶÁдÊèÉ¢£¿
ÔڹŰåµÄÓ¦ÓóÌÐòÉè¼ÆÖУ¬Ó¦ÓóÌÐòͨ³£½«ËùÓÐÊý¾Ý¿â²Ù×÷·¢Ë͵½µ¥¸öÊý¾Ý¿âЧÀÍÆ÷ÉÏ¡£Õâ¾ÍÒâζ×Å£¬ÎÞÂÛÊǶÁÈ¡Êý¾ÝÕÕ¾ÉдÈëÊý¾Ý£¬¶¼ÐèҪͨ¹ýͳһ¸öÊý¾Ý¿âЧÀÍÆ÷¾ÙÐС£ÔÚ¸ßÁ÷Á¿µÄÓ¦ÓóÌÐòÖУ¬Õâ»áµ¼ÖÂÊý¾Ý¿âЧÀÍÆ÷µÄ¸ºÔعý¸ß£¬´Ó¶øµ¼ÖÂÐÔÄÜϽµ¡£Òò´Ë£¬Ò»ÖÖ½â¾ö¼Æ»®ÊÇÒýÈëÊý¾Ý¿â¶ÁдÊèÉ¢¡£
Êý¾Ý¿â¶ÁдÊèÉ¢Êǽ«¶ÁÈ¡²Ù×÷ÓëдÈë²Ù×÷ÍÑÀëµÄÒ»ÖÖ·½·¨¡£Í¨³££¬Ó¦ÓóÌÐò»á½«¶ÁÈ¡²Ù×÷·¢Ë͵½Ö÷Êý¾Ý¿âЧÀÍÆ÷£¬¶ø½«Ð´Èë²Ù×÷·¢Ë͵½´ÓÊý¾Ý¿âЧÀÍÆ÷¡£ÕâÑù£¬Ôڸ߲¢·¢ÇéÐÎÏ£¬Êý¾Ý¿âЧÀÍÆ÷²»ÔÙÔâÊÜËùÓеĶÁд¸ºÔØ£¬´Ó¶øÌá¸ßÐÔÄܺͿÉÉìËõÐÔ¡£
ΪʲôÐèÒªÊý¾Ý¿â¶ÁдÊèÉ¢£¿
ÔÚ¸ßÁ÷Á¿µÄÓ¦ÓóÌÐòÖУ¬Êý¾Ý¿â²Ù×÷ÊÇÒ»¸öºÜÊǺÄʱµÄÀú³Ì¡£²¢ÇÒ£¬ÈôÊÇÊý¾Ý¿âЧÀÍÆ÷³ÉΪƿ¾±£¬Õû¸öÓ¦ÓóÌÐòµÄÐÔÄܽ«»áϽµ¡£Ê¹ÓÃÊý¾Ý¿â¶ÁдÊèÉ¢¿ÉÒÔ½«¶ÁÈ¡²Ù×÷תÒƵ½Ö÷Êý¾Ý¿âЧÀÍÆ÷Ö®ÍâµÄÒ»¸ö»ò¶à¸ö´ÓЧÀÍÆ÷ÉÏ£¬´Ó¶øÌá¸ßÐÔÄܺͿÉÉìËõÐÔ¡£
ÔõÑùʵÏÖ¶ÁдÊèÉ¢£¿
ÔÚLaravelÓ¦ÓóÌÐòÖУ¬¿ÉÒÔͨ¹ý¶ÔÊý¾Ý¿âÉèÖþÙÐиü¸ÄÀ´ÆôÓöÁдÊèÉ¢¡£ÒÔÏÂÊÇһЩ°ì·¨£º
ÉèÖÃÊý¾Ý¿âÅþÁ¬
ÔÚconfig/database.phpÎļþÖУ¬¿ÉÒÔÕÒµ½¹ØÓÚÊý¾Ý¿âÅþÁ¬µÄÉèÖÃÐÅÏ¢¡£ÔÚÕâÀ¿ÉÒÔ½ç˵Ö÷Êý¾Ý¿âЧÀÍÆ÷µÄÅþÁ¬²ÎÊý£º
'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], ],
µÇ¼ºó¸´ÖÆ
Ìí¼Ó´ÓÅþÁ¬
ÒªÆôÓöÁдÊèÉ¢£¬ÐèÒªÌí¼ÓÒ»¸ö´ÓÅþÁ¬¡£¿ÉÒÔͨ¹ý¸´ÖÆÓëÖ÷ÅþÁ¬ÏàͬµÄ²ÎÊýÀ´½¨Éè´ÓÅþÁ¬£¬Ö»Ðè¸ü¸ÄÅþÁ¬µÄÖ÷»úÃûºÍƾ֤¡£
'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], 'mysql_read' => [ 'driver' => 'mysql', 'host' => env('DB_READ_HOST', 'localhost'), 'database' => env('DB_READ_DATABASE', 'forge'), 'username' => env('DB_READ_USERNAME', 'forge'), 'password' => env('DB_READ_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], ],
µÇ¼ºó¸´ÖÆ
ÉèÖÃÊý¾Ý¿âЧÀÍ
¿ÉÒÔÔÚconfig/database.phpÎļþÖнç˵Êý¾Ý¿âЧÀ͵ÄÃû³ÆºÍ´ÓЧÀÍÆ÷µÄÉèÖÃÐÅÏ¢¡£ÔÚÕâÀ¿ÉÒÔʹÓÃÒ»¸öÊý×éÀ´½ç˵¶à¸ö´ÓЧÀÍÆ÷£¬Ã¿¸ö´ÓЧÀÍÓþßÓÐ×Ô¼ºµÄÖ÷»úÃûºÍƾ֤¡£
'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], 'mysql_read' => [ 'driver' => 'mysql', 'host' => env('DB_READ_HOST', 'localhost'), 'database' => env('DB_READ_DATABASE', 'forge'), 'username' => env('DB_READ_USERNAME', 'forge'), 'password' => env('DB_READ_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], ], 'service' => [ 'mysql' => [ 'write' => 'mysql', 'read' => [ 'mysql_read', ], ], ],
µÇ¼ºó¸´ÖÆ
Ñ¡Ôñ´ÓЧÀÍÆ÷
ÔÚÓ¦ÓóÌÐòÖУ¬¿ÉÒÔʹÓÃÒÔÏÂÒªÁì´Ó¶à¸ö´ÓЧÀÍÆ÷ÖÐÑ¡ÔñÒ»¸ö£º
// »ñÈ¡¶ÁÈ¡µÄÄ£×Ó $model = new Post; // ´ÓËùÓдÓЧÀÍÆ÷ÖÐËæ»úÑ¡ÔñÒ»¸ö $results = $model->on('mysql_read')->inRandomOrder()->get();
µÇ¼ºó¸´ÖÆ
»òÕß¿ÉÒÔʹÓÃÒÔÏÂÒªÁìÀ´ÊÖ¶¯Ñ¡ÔñÒ»¸ö´ÓЧÀÍÆ÷£º
// »ñÈ¡¶ÁÈ¡µÄÄ£×Ó $model = new Post; // ÊÖ¶¯Ñ¡ÔñµÚÒ»¸ö´ÓЧÀÍÆ÷ $config = config('database.connections.mysql_read'); $conn = DB::connection('mysql_read'); $conn->setPdo($conn->createConnector($config)->connect()); $results = $model->setConnection($conn)->inRandomOrder()->get();
µÇ¼ºó¸´ÖÆ
×ܽá
ÉèÖöÁдÊèÉ¢¿ÉÒÔ½«Ó¦ÓóÌÐòÖеĶÁÈ¡²Ù×÷·Ö·¢µ½´ÓЧÀÍÆ÷ÉÏ£¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺͿÉÉìËõÐÔ¡£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬ÊµÏÖ¶ÁдÊèÉ¢ÒªÁì¼òÆÓ£¬Ö»ÐèÒª¸ü¸ÄÊý¾Ý¿âÅþÁ¬ÉèÖò¢Ìí¼Ó´ÓÅþÁ¬¼´¿É¡£È»ºó¿ÉÒÔʹÓÃÅÌÎÊÌìÉúÆ÷ºÍEloquent ORMÑ¡Ôñ´ÓЧÀÍÆ÷¡£
ÒÔÉϾÍÊÇlaravelÔõô¶ÁдÊèÉ¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡