laravelÔõÑù»ñÈ¡sql
laravelÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüΪÎÒÃÇÌṩÁ˱ã½ÝµÄÒªÁìÀ´ÖÎÀíÊý¾Ý¿â»á¼ûºÍÅÌÎÊ¡£²»¹ý£¬ÔÚһЩÐèÒª¶¨ÖƵÄÓªÒµ³¡¾°ÖУ¬ÎÒÃÇÐèÒªÊÖ¶¯Éó²éºÍµ÷ÊÔÌìÉúµÄsqlÓï¾ä£¬ÒÔ±ã¸üºÃµØÏàʶ³ÌÐòµÄÔË×÷Àú³Ì¼°ÓÅ»¯sqlµÄÐÔÄÜ¡£±¾Îľͽ«ÏêϸÏÈÈÝÔÚlaravel¿ò¼ÜÖУ¬ÔõÑù»ñÈ¡sqlÓï¾ä¡£
Ò»¡¢LaravelµÄÅÌÎʹ¹½¨Æ÷
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý¡£ÅÌÎʹ¹½¨Æ÷ÊÇLaravelÖÐÊý¾Ý¿âµÄÖ÷ÒªÁýͳ²ã£¬ËüÌṩÁËһϵÁÐAPIÀ´¹¹½¨ºÍÖ´ÐÐÅÌÎÊÓï¾ä£¬Ê¹ÓÃÆðÀ´Ê®·ÖÀû±ã¡£ÀýÈ磬¿ÉÒÔʹÓÃÒÔÏ´úÂë»ñÈ¡Êý¾Ý±íÖеļͼ£º
$users = DB::table('users')->get();
µÇ¼ºó¸´ÖÆ
ʹÓÃÅÌÎʹ¹½¨Æ÷¿ÉÒÔÀû±ã¿ì½ÝµØ»ñÈ¡´ó×ÚÊý¾Ý¡£²»¹ý£¬ÕâÖÖÒªÁì²¢²»¿ÉÌṩÎÒÃÇ×îÖÕÖ´ÐеÄSQLÓï¾ä£¬ÕâʱÎÒÃǾÍÐèҪʹÓÃһЩÌØÁíÍ⹤¾ßÀ´»ñÈ¡ÌìÉúµÄSQLÓï¾ä¡£
¶þ¡¢ÆôÓÃÅÌÎÊÈÕÖ¾
LaravelÌṩÁËÒ»¸öºÜÊDZã½ÝµÄÒªÁìÀ´»ñÈ¡Ö´ÐйýµÄSQLÓï¾ä£¬ÄǾÍÊÇÆôÓÃÅÌÎÊÈÕÖ¾¡£LaravelµÄÊý¾Ý¿âÅÌÎÊÈÕÖ¾ÊÇÒ»ÖּͼËùÓÐÖ´ÐеÄÅÌÎÊÓï¾äµÄ»úÖÆ£¬°üÀ¨Ö´ÐеÄʱ¼äºÍÖ´ÐеÄЧ¹ûµÈÐÅÏ¢¡£ÔÚ¿ª·¢Ê±£¬ÆôÓÃÅÌÎÊÈÕÖ¾¿ÉÒÔÀû±ãÎÒÃǵ÷ÊÔ³ÌÐòºÍÓÅ»¯SQLµÄÐÔÄÜ¡£
ÆôÓÃÅÌÎÊÈÕÖ¾µÄÒªÁìÊ®·Ö¼òÆÓ£¬Ö»ÐèÒªÔÚLaravelÉèÖÃÎļþconfig/database.phpÖоÙÐÐÉèÖü´¿É¡£ÔÚLaravelĬÈϵÄÉèÖÃÎļþÖУ¬ÅÌÎÊÈÕ־ĬÈÏÊǹرյģ¬¿ÉÒÔÐ޸ĸÃÉèÖÃÎļþ£¬½«’log’ => ‘false’ÐÞ¸ÄΪ’log’ => ‘true’£¬¼´¿É¿ªÆôÅÌÎÊÈÕÖ¾¡£Ð޸ĺóµÄÉèÖÃÎļþÈçÏ£º
'connections' => [ 'mysql' => [ 'driver' => 'mysql', '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', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'log' => true, //¿ªÆôÅÌÎÊÈÕÖ¾ ], ],
µÇ¼ºó¸´ÖÆ
ÆôÓÃÅÌÎÊÈÕÖ¾ºó£¬Laravel»á½«ÅÌÎÊÈÕÖ¾ÉúÑÄÔÚstorage/logs/laravel.logÎļþÖС£ÎÒÃÇ¿ÉÒÔͨ¹ýÉó²é¸ÃÎļþÀ´»ñÈ¡Ö´ÐйýµÄSQLÓï¾ä£¬¿ÉÒÔÔÚLaravelµÄ¿ØÖÆ̨»òÕßµ÷ÊÔ¹¤¾ßÖÐͨ¹ýÒÔÏ´úÂë»ñÈ¡ÅÌÎÊÈÕÖ¾£º
DB::connection()->enableQueryLog(); // ÔÚÕâÀïÌÜдÐèÒªÅÌÎʵĴúÂë dd(DB::getQueryLog());
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫ÆôÓÃÅÌÎÊÈÕÖ¾£¬²¢Êä³öÅÌÎÊÈÕÖ¾¡£ÔÚ³ÌÐòµÄÖ´ÐÐÀú³ÌÖУ¬Å²ÓÃÉÏÊö´úÂë»ñÈ¡ÅÌÎÊÈÕÖ¾£¬¾Í¿ÉÒÔʵʱ¿´µ½³ÌÐòÖ´ÐеÄSQLÓï¾ä¡£
ÐèҪעÖصÄÊÇ£¬ÆôÓÃÅÌÎÊÈÕÖ¾»á¶ÔϵͳÐÔÄÜÓÐÒ»¶¨µÄÓ°Ï죬Òò´ËÔÚÉú²úÇéÐÎÖв»Ó¦¸ÃÒ»Ö±¿ªÆôÅÌÎÊÈÕÖ¾¡£±ðµÄ£¬LaravelÅÌÎÊÈÕ־ĬÈÏÖ»ÔÚ¿ª·¢ÇéÐÎÖÐÆôÓã¬Òò´ËÐèÒªÊÖ¶¯ÔÚÉèÖÃÎļþÖоÙÐÐÉèÖÃÀ´¿ªÆôÅÌÎÊÈÕÖ¾¡£
Èý¡¢Ê¹ÓÃDB facade
Laravel»¹ÌṩÁËÁíÍâÒ»ÖÖ»ñÈ¡Ö´ÐеÄSQLÓï¾äµÄÒªÁ졪¡ªÊ¹ÓÃDB facade¡£DB facadeÌṩÁËһϵÁеľ²Ì¬ÒªÁ죬¿ÉÒÔÀû±ãµØ½¨Éè¡¢Ö´ÐÐÅÌÎÊÓï¾äºÍ»ñÈ¡Ö´ÐеÄSQLÓï¾ä¡£ÀýÈ磬ͬÑùÊÇ»ñÈ¡users±íÖеÄÊý¾Ý£º
$users = DB::select('select * from users');
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÉÏÊö´úÂëºó£¬DB facade»áÌìÉúÒ»ÌõselectÓï¾ä£¬²¢Ö´ÐÐËü£¬»ñÈ¡Êý¾Ý±íÖеļͼ¡£²»¹ý£¬ÕâÖÖ·½·¨Í¬ÑùûÓÐÌṩֱ½ÓÉó²éÖ´ÐеÄSQLÓï¾äµÄ½Ó¿Ú¡£²»¹ý£¬DB facadeÌṩÁËÒ»¸ö½ÏΪ¼òÆÓµÄÒªÁìÀ´»ñÈ¡ÌìÉúµÄSQLÓï¾ä£¬ÒªÁìÈçÏ£º
$sql = DB::getQueryLog();
µÇ¼ºó¸´ÖÆ
¸ÃÒªÌå»á·µ»ØÒ»¸ö°üÀ¨ÁËËùÓÐÅÌÎÊÈÕÖ¾µÄÊý×飬ÕâЩÅÌÎʶ¼ÊÇͨ¹ýDB facadeÖ´Ðеġ£»ñÈ¡µ½¸ÃÊý×éÖ®ºó£¬¾Í¿ÉÒÔ¶ÁȡÿһÌõÈÕÖ¾µÄqueryÊôÐÔ£¬¼´¿ÉÉó²éÿ´ÎÖ´ÐеÄSQLÓï¾ä¡£
ËÄ¡¢Ê¹ÓÃORMÄ£×Ó
ÔÚLaravelÖУ¬ORMÄ£×ÓÊÇÒ»ÖÖÃæÏò¹¤¾ßµÄÊý¾Ý»á¼û·½·¨£¬Ëü°ÑÊý¾Ý±íµÄ¼Í¼ӳÉä³ÉΪһ¸ö¸öµÄ¹¤¾ß£¬Àû±ãÎÒÃǾÙÐÐÊý¾Ý¿â²Ù×÷¡£ORMÄ£×ÓÊÇLaravel¹ØÓÚÅÌÎʹ¹½¨Æ÷µÄ¸ß²ã·â×°£¬Ê¹ÓÃORMÄ£×Ó¿ÉÒÔÔ½·¢Àû±ãµØ»á¼ûÊý¾Ý¿â£¬²¢ÇÒͨ¹ýÄ£×Ó¹ØÁªÅÌÎÊÊý¾Ý±íµÄʱ¼ä£¬ÌìÉúµÄSQLÅÌÎÊÓï¾äÒ²Ô½·¢Ö±¹ÛºÍÒ×ÓÚµ÷ÊÔ¡£
ÔÚʹÓÃORMÄ£×ÓµÄÀú³ÌÖУ¬ÎÒÃÇͬÑù¿ÉÒÔ»ñÈ¡ÌìÉúµÄSQLÓï¾ä¡£LaravelÌṩÁËÒ»¸öÃûΪtoSql()µÄÒªÁ죬Ëü¿ÉÒÔÊä³öÄ£×ÓµÄÅÌÎÊÓï¾ä¡£ÀýÈ磬ÎÒÃÇÐèҪʹÓÃORMÄ£×Ó»ñÈ¡users±íÖеÄÊý¾Ý£º
$users = AppModelsUser::all();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬UserÊÇÎÒÃǽç˵µÄORMÄ£×Ó£¬Ê¹ÓÃall()ÒªÁì¼´¿É»ñÈ¡¸ÃÄ£×Ó¶ÔÓ¦µÄ±íÖеÄËùÓмͼ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýŲÓÃtoSql()ÒªÁ죬»ñÈ¡¸ÃÄ£×ÓÌìÉúµÄSQLÓï¾ä¡£ÈçÏÂËùʾ£º
$sql = AppModelsUser::all()->toSql();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë»á·µ»Ø¾ÓÉORMÄ£×Ó´¦ÀíºóµÄÅÌÎÊÓï¾ä¡£ÐèҪעÖصÄÊÇ£¬toSql()ÒªÁìÖ»ÄÜÔÚÌìÉúµÄSQLÓï¾äÖÐÌ滻ģ×ÓÖеÄռλ·û£¬²»¿ÉÖ´ÐиÃSQLÓï¾ä£¬Òò´Ë£¬¸ÃÒªÁìÖ»¿ÉÒÔÓÃÓÚÉó²éÌìÉúµÄSQLÓï¾ä£¬²»¿É×÷ΪÅÌÎÊÒªÁìµÄÌ滻Ʒ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔÚLaravel¿ò¼ÜÖУ¬ÔõÑùÀû±ãµØ»ñÈ¡Ö´ÐйýµÄSQLÓï¾ä¡£Í¨¹ýÆôÓÃÅÌÎÊÈÕÖ¾¡¢Ê¹ÓÃDB facadeºÍORMÄ£×ӵȶàÖÖÒªÁ죬¿ÉÒÔ»ñÈ¡»òÕßÉó²é³ÌÐòÖ´ÐеÄSQL£¬Àû±ãÎÒÃǾÙÐе÷ÊÔºÍÓÅ»¯³ÌÐòÐÔÄÜ¡£ËäÈ»£¬²î±ðµÄÒªÁ춼ÓÐËüµÄÊÊÓó¡¾°£¬ÐèҪƾ֤Ïêϸ³¡¾°Ñ¡Ôñ×îÊʺϵÄÒªÁì¡£ÀýÈ磬ÔÚ¿ª·¢ÇéÐÎÖУ¬¿ÉÒÔÆôÓÃÅÌÎÊÈÕÖ¾Éó²é³ÌÐòµÄÖ´ÐÐÇéÐΣ»ÔÚʹÓÃORMÄ£×Ó¾ÙÐÐÊý¾Ý»á¼ûµÄʱ¼ä£¬toSql()ÒªÁì¿ÉÒÔÀû±ãµØÉó²éÌìÉúµÄSQLÓï¾ä¡£ÔÚÏÖʵµÄÏîÄ¿ÖУ¬¿ª·¢Ö°Ô±¿ÉÒÔÎÞаʹÓÃÕâЩҪÁ죬À´Ìá¸ßSQLÅÌÎÊЧÂʺÍÌáÉý³ÌÐòÐÔÄÜ¡£
ÒÔÉϾÍÊÇlaravelÔõÑù»ñÈ¡sqlµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡