laravel ÉèÖñíǰ׺
laravel ÊÇÒ»¸öÊ¢ÐÐµÄ php ¿ò¼Ü£¬±»ÆÕ±éµØÓ¦ÓÃÔÚ web Ó¦ÓóÌÐòµÄ¿ª·¢ÖС£ÔÚ laravel ÖУ¬ÎÒÃÇ¿ÉÒÔºÜÊÇÀû±ãµØ²Ù×÷Êý¾Ý¿â£¬²»µ«Ö§³Ö¶àÖÖÀàÐ͵ÄÊý¾Ý¿â£¬»¹ÌṩÁ˸»ºñµÄ orm ʵÏÖ£¬ºÃ±È eloquent orm£¬Ê¹µÃÎÒÃÇ¿ÉÒÔºÜÊǸßЧµØÓëÊý¾Ý¿â¾ÙÐн»»¥¡£
ÔÚһЩÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄÜÐèÒª¸ø Laravel µÄ±íÌí¼ÓһЩǰ׺£¬ÕâÑù¿ÉÒÔÀû±ãÎÒÃǶԲî±ðµÄ±í¾ÙÐÐÇø·Ö£¬ÓÐÖúÓÚ¸üºÃµØÖÎÀíÊý¾Ý¿âµÄ½á¹¹¡£½ÓÏÂÀ´£¬±¾ÎĽ«ÏÈÈݼ¸ÖÖÉèÖà Laravel ±íǰ׺µÄÒªÁì¡£
ÒªÁìÒ»£ºÍ¨¹ýÉèÖÃÎļþÉèÖÃ
ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸Ä config/database.php Îļþ£¬À´ÉèÖñíµÄǰ׺¡£ÏêϸµÄ£¬¿ÉÒÔÔÚ¶ÔÓ¦µÄÊý¾Ý¿âÉèÖÃÏîÖÐÌí¼ÓÒ»¸ö prefix ÊôÐÔ£¬ÓÃÀ´Ö¸¶¨±íµÄǰ׺¡£ÀýÈ磺
'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' => 'my_', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃÇͨ¹ýÔÚ mysql ÉèÖÃÏîÖÐÌí¼ÓÁËÒ»¸ö prefix ÊôÐÔÀ´Ö¸¶¨±íµÄǰ׺£¬ÕâÀïµÄǰ׺Ϊ my_¡£
ÒªÁì¶þ£ºÍ¨¹ýÄ£×ÓÉèÖÃ
³ýÁËͨ¹ýÉèÖÃÎļþÉèÖñíǰ׺£¬ÎÒÃÇ»¹¿ÉÒÔͨ¹ýÐÞ¸Ä Eloquent ORM Ä£×ÓÖÐµÄ $table ÊôÐÔÀ´ÉèÖñíǰ׺¡£ÏêϸµÄ£¬ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÖÐÌí¼ÓÏÂÃæÕâÒ»ÐдúÂ룺
protected $table = 'my_table';
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽ« $table ÊôÐÔÉèÖÃΪ my_table£¬ÕâÀïµÄ my_ ǰ׺¾ÍÊÇÖ¸¶¨µÄ±íǰ׺¡£
²»¹ý£¬ÕâÖÖÒªÁìÐèÒª¶Ôÿ¸öÄ£×Ó¶¼¾ÙÐÐÐ޸ģ¬×èÖ¹¸÷¸öÄ£×ÓÖбíÃûµÄ³åÍ»£¬ÐèÒªÉèÖòî±ðµÄǰ׺¡£
ÒªÁìÈý£ºÍ¨¹ý DB ÀàÉèÖÃ
³ýÁËÒÔÉÏÁ½ÖÖÒªÁ죬ÎÒÃÇ»¹¿ÉÒÔʹÓà Laravel ÌṩµÄ DB ÀàÀ´ÉèÖñíǰ׺¡£ÏêϸµÄ£¬¿ÉÒÔÔÚ SQL Óï¾äÖÐʹÓà DB::table ÒªÁìÀ´Ö¸¶¨±íÃû£¬ÀýÈ磺
DB::table('my_table')->insert(['name' => 'test']);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁË DB::table ÒªÁìÀ´Ö¸¶¨±íÃûΪ my_table£¬ÕâÀïµÄ my_ ǰ׺¾ÍÊÇÖ¸¶¨µÄ±íǰ׺¡£ÎÒÃÇ¿ÉÒÔÔÚ SQL Óï¾äÖÐí§ÒâʹÓà DB::table ÒªÁìÖ¸¶¨±íǰ׺£¬´Ó¶øÎÞа¡¢Àû±ãµØÖÎÀíÊý¾Ý¿â½á¹¹¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÈýÖÖ³£¼ûµÄÉèÖà Laravel ±íǰ׺µÄÒªÁ죺ͨ¹ýÉèÖÃÎļþÉèÖá¢Í¨¹ýÄ£×ÓÉèÖá¢Í¨¹ý DB ÀàÉèÖá£ÕâÈýÖÖÒªÁì¸÷ÓÐÓÅÈõµã£¬ÐèҪƾ֤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵÄÒªÁì¡£ÎÞÂÛÑ¡ÔñÄÄÖÖÒªÁ죬¶¼ÐèҪעÖرíǰ׺µÄΨһÐÔ£¬ÒÔÃâÒýÆðÃüÃû³åÍ»¡£Í¬Ê±£¬±íǰ׺µÄÉèÖÃÒ²¿ÉÒÔÔÚ Laravel ÖкܺõØÓëÊý¾Ý¿âǨáã¡¢Êý¾ÝÌî³äµÈ¹¦Ð§ÍŽáʹÓ㬺ñȿÉÒÔÔÚǨáã¾ç±¾ÖÐÖ¸¶¨±íǰ׺£¬´Ó¶ø×Ô¶¯µØ½¨Éè´øǰ׺µÄ±í½á¹¹¡£
ÒÔÉϾÍÊÇlaravel ÉèÖñíǰ׺µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡