×ðÁú¿­Ê±

laravelÅþÁ¬´æ´¢Àú³Ì

ÔÚʹÓÃlaravel¿ª·¢µÄÀú³ÌÖУ¬ÓÐʱÐèÒªÅþÁ¬Êý¾Ý¿âÖеĴ洢Àú³Ì£¬ÒÔ±ãÖ´ÐÐһЩÌض¨µÄÊý¾Ý¿â²Ù×÷  ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravelÅþÁ¬´æ´¢Àú³ÌÒÔ¼°ÆäÖеÄһЩעÖØÊÂÏî  ¡£

ÉèÖÃÊý¾Ý¿âÅþÁ¬

Ê×ÏÈ£¬ÐèÒªÔÚconfig/database.phpÎļþÖÐÉèÖÃÊý¾Ý¿âÅþÁ¬£¬ÒÔ±ãLaravel¶ÔÊý¾Ý¿â¾ÙÐÐÏìÓ¦µÄ²Ù×÷  ¡£ÔÚ¸ÃÎļþÖÐÕÒµ½’mysql’ÅþÁ¬ÉèÖã¬ÐÞ¸ÄÆäÖеIJÎÊýÈçÏ£º

'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', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
    'options' => array(
        PDO::ATTR_EMULATE_PREPARES => true,
    ),
],

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬ÐèҪעÖصÄÊÇ£¬ÈôÊÇÊý¾Ý¿âÅþÁ¬Ê¹ÓÃÁË×Ô½ç˵µÄ¶Ë¿Ú¡¢Óû§ÃûºÍÃÜÂ룬ÐèÒªÔÚ.envÎļþÖоÙÐÐÏìÓ¦µÄÉèÖã¬ÈçÏÂËùʾ£º

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mydatabase
DB_USERNAME=myusername
DB_PASSWORD=mypassword

µÇ¼ºó¸´ÖÆ

½¨Éè´æ´¢Àú³Ì

ÔÚ¾ÙÐд洢Àú³ÌÅþÁ¬Ö®Ç°£¬ÐèÒªÏÈÔÚÊý¾Ý¿âÖн¨Éè´æ´¢Àú³Ì  ¡£ÒÔMySQLΪÀý£¬ÔÚMySQLµÄÏÂÁîÐлò¹¤¾ßÖУ¬¿ÉÒÔÖ´ÐÐÀàËÆÈçϵÄSQLÓï¾ä½¨Éè´æ´¢Àú³Ì£º

CREATE PROCEDURE my_procedure (IN id INT, IN name VARCHAR(255), OUT result INT)
BEGIN
    -- ÔÚ´Ë´¦±àд´æ´¢Àú³ÌµÄÏêϸ´úÂë
    -- ÀýÈ磺½«idºÍname²åÈëµ½Êý¾Ý¿â±íÖУ¬²¢½«²åÈëµÄÊý¾ÝµÄÖ÷¼ü·µ»Øµ½result±äÁ¿
    INSERT INTO my_table(id, name) VALUES (id, name);
    SET result = LAST_INSERT_ID();
END

µÇ¼ºó¸´ÖÆ

ÔÚ½¨Éè´æ´¢Àú³Ìʱ£¬ÐèҪעÖØÊÂÏîÈçÏ£º

procedureÒªº¦×Ö£ºËµÃ÷½¨ÉèµÄÊÇ´æ´¢Àú³Ì £»

´æ´¢Àú³ÌÃû³Æ£º´Ë´¦Îªmy_procedure £»

²ÎÊýÁÐ±í£º´Ë´¦ÓÐ3¸ö²ÎÊý£¬»®·ÖΪid¡¢nameºÍresult £»

´æ´¢Àú³Ì´úÂ룺ÔÚBEGINºÍENDÒªº¦×ÖÖ®¼ä±àд´æ´¢Àú³ÌµÄÏêϸ´úÂë £»

OUT±äÁ¿£ºÌåÏÖ·µ»ØÖµ£¬´Ë´¦Îªresult  ¡£

½¨Éè´æ´¢Àú³ÌµÄÏêϸʵÏÖƾ֤ÏÖʵӪҵÐèÇó¾ÙÐбàд  ¡£

ÅþÁ¬´æ´¢Àú³Ì

ÔÚLaravelÖÐÅþÁ¬´æ´¢Àú³ÌÐèҪʹÓÃDBÀà  ¡£½ÓÄÉÒÔÏ´úÂë¼´¿ÉÅþÁ¬´æ´¢Àú³Ì£º

$pdo = DB::connection()->getPdo();
$stmt = $pdo->prepare("CALL my_procedure(?, ?, @result)");
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->bindParam(2, $name, PDO::PARAM_STR);
$stmt->execute();
$select = $pdo->query("SELECT @result AS result");
$result = $select->fetch(PDO::FETCH_ASSOC)['result'];

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬ÐèҪעÖصÄÊÇ£º

ʹÓÃDB::connection()ÒªÁì»ñÈ¡Êý¾Ý¿âÅþÁ¬ £»

ͨ¹ýgetPdo()ÒªÁì»ñÈ¡PDO¹¤¾ß £»

ʹÓÃprepare()ÒªÁì×¼±¸ÒªÖ´ÐеÄSQLÓï¾ä£¬ÆäÖС°?¡¢ ?ºÍ@result¡±»®·Ö¶ÔÓ¦´æ´¢Àú³ÌÖеÄ3¸ö²ÎÊý £»

ʹÓÃbindParam()ÒªÁ콫±äÁ¿$idºÍ$name°ó¶¨µ½µÚ1¡¢2¸ö²ÎÊýÉÏ £»

ŲÓÃexecute()ÒªÁìÖ´Ðд洢Àú³Ì £»

ʹÓÃquery()ÒªÁìÖ´ÐÐÅÌÎÊ £»

½«ÅÌÎÊЧ¹û´æ´¢µ½$result±äÁ¿ÖÐ  ¡£

ÐèҪעÖصÄÊÇ£¬ÔÚ´æ´¢Àú³ÌÖÐʹÓõIJÎÊý¸öÊýºÍÀàÐͱØÐèÓëLaravelÖÐÖ´Ðд洢Àú³ÌʱµÄ²ÎÊý¸öÊýºÍÀàÐͼá³ÖÒ»Ö  ¡£ÈôÊDZ£´æ·×ÆçÖ£¬¿ÉÄܻᵼÖÂÅþÁ¬´æ´¢Àú³Ìʧ°Ü  ¡£

×ܽá

ÒÔÉϾÍÊÇÅþÁ¬´æ´¢Àú³ÌµÄÏêϸ°ì·¨ºÍ×¢ÖØÊÂÏî  ¡£ÔÚÏÖʵµÄ¿ª·¢ºÍµ÷ÊÔÀú³ÌÖУ¬ÐèҪƾ֤ÏÖÕæÏàÐξÙÐÐÏìÓ¦µÄÉèÖúÍÐ޸ģ¬ÒÔʵÏÖÖ¸¶¨µÄÊý¾Ý¿â²Ù×÷Ч¹û  ¡£Ï£Íû±¾ÎĶԶÁÕßÔÚʹÓÃLaravelÅþÁ¬´æ´¢Àú³Ì·½ÃæÓÐËù×ÊÖú  ¡£

ÒÔÉϾÍÊÇlaravelÅþÁ¬´æ´¢Àú³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

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

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

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