尊龙凯时人生就是搏

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ