尊龙凯时人生就是博

laravel dbÀàÔõôÓÃ

laravelÊÇÒ»¸öÊ¢ÐеÄphp web¿ò¼Ü£¬ÒòÆäÓÅÑŵÄÓï·¨¡¢Àû±ãµÄ¿ª·¢ºÍ¸ßЧµÄÐÔÄܶø±¸ÊÜ¿ª·¢ÕßµÄÇàíù¡£ÔÚlaravel¿ª·¢ÖУ¬Êý¾Ý¿â²Ù×÷ÓÈΪÖ÷Òª¡£laravelÌṩÁËÒ»¸ödbÀ࣬ÓÃÓÚ²Ù×÷Êý¾Ý¿â£¬±¾ÎĽ«ÏêϸÏÈÈÝlaravel dbÀàµÄʹÓá£

DBÀàµÄ»ù±¾Ó÷¨

ÔÚLaravel¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýDBÀàÇáËɵؾÙÐÐÊý¾Ý¿â²Ù×÷¡£ÔÚʹÓÃDBÀà֮ǰ£¬ÐèÒªÔÚ¿ØÖÆÆ÷ÖÐÒýÈëÃüÃû¿Õ¼ä£º

use IlluminateSupportFacadesDB;

µÇ¼ºó¸´ÖÆ

ͨ¹ýDBÀ࣬ÎÒÃÇ¿ÉÒÔ¾ÙÐÐÅÌÎÊ¡¢²åÈë¡¢¸üС¢É¾³ýµÈ²Ù×÷¡£ÒÔÏÂÊÇÊý¾Ý¿âÅÌÎʵÄʾÀý£º

$users = DB::table('users')->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇ´Óusers±íÖлñÈ¡ËùÓÐÓû§µÄ¼Í¼¡£ÈôÊÇÒª»ñÈ¡µ¥Ìõ¼Í¼£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

$user = DB::table('users')->where('name', 'John')->first();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫´Óusers±íÖлñÈ¡µÚÒ»ÌõnameΪJohnµÄ¼Í¼¡£

DBÀàµÄÅÌÎʽṹÆ÷

DBÀàµÄÒªº¦ÊÇÅÌÎʽṹÆ÷¡£ÅÌÎʽṹÆ÷¿ÉÒÔͨ¹ýÁ´Ê½ÒªÁì¹¹½¨ÅÌÎÊ£¬ºÜÊÇÎÞаÀû±ã¡£ÒÔÏÂÊÇһЩʾÀý£º

a. whereÌõ¼þÅÌÎÊ

$users = DB::table('users')->where('name', '=', 'John')->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀý´Óusers±íÖÐÅÌÎÊname¼´ÊÇ’John’µÄ¼Í¼¡£¿ÉÒÔͨ¹ýµÚ¶þ¸ö²ÎÊýÀ´Ö¸¶¨Ìõ¼þÀàÐÍ£¬Èç’>’¡¢'<‘¡¢’>=’¡¢'<=’µÈ¡£

b. orWhereÌõ¼þÅÌÎÊ

$users = DB::table('users')->where('name', '=', 'John')->orWhere('name', '=', 'Mary')->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇ´Óusers±íÖÐÅÌÎÊname¼´ÊÇ’John’»ò’name’¼´ÊÇ’Mary’µÄ¼Í¼¡£

c. ¶à¸öWhereÌõ¼þÅÌÎÊ

$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['name', '<>', 'John']
])->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇ´Óusers±íÖÐÅÌÎÊstatus¼´ÊÇ1²¢ÇÒname²»¼´ÊÇJohnµÄ¼Í¼¡£

d. whereBetweenÌõ¼þÅÌÎÊ

$users = DB::table('users')->whereBetween('votes', [1, 100])->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇ´Óusers±íÖÐÅÌÎÊvotesÔÚ1-100Ö®¼äµÄ¼Í¼¡£

e. whereInÌõ¼þÅÌÎÊ

$users = DB::table('users')->whereIn('id', [1, 2, 3])->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇ´Óusers±íÖÐÅÌÎÊidÔÚ1¡¢2¡¢3Öеļͼ¡£

f. orderByÅÅÐòÅÌÎÊ

$users = DB::table('users')->orderBy('name', 'desc')->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇƾ֤nameµ¹ÐòÅÅÁÐÅÌÎÊusers±íÖеļͼ¡£

DBÀàµÄ²åÈë¡¢¸üС¢É¾³ý²Ù×÷

Laravel DBÀ಻µ«Ö§³ÖÅÌÎʲÙ×÷£¬»¹Ö§³Ö²åÈë¡¢¸üкÍɾ³ý²Ù×÷¡£ÒÔÏÂÊÇһЩʾÀý£º

a. Êý¾Ý²åÈë

DB::table('users')->insert([
    'email' => 'john@example.com',
    'name' => 'John Doe'
]);

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÏòusers±íÖвåÈëÒ»ÌõemailΪjohn@example.com£¬nameΪJohn DoeµÄ¼Í¼¡£

b. Êý¾Ý¸üÐÂ

DB::table('users')->where('id', 1)->update(['votes' => 1]);

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀý¸üÐÂÁËidΪ1µÄÓû§µÄͶƱÊýΪ1¡£

c. Êý¾Ýɾ³ý

DB::table('users')->where('votes', '<', 100)->delete();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýɾ³ývotesСÓÚ100µÄËùÓÐÓû§¡£

DBÀàµÄÊÂÎñ²Ù×÷

Êý¾Ý²Ù×÷Àú³ÌÖУ¬ºÜ¿ÉÄܻᱬ·¢ÒâÍ⣬ºÃ±ÈÔÚ²åÈëÊý¾Ýʱ·ºÆð¹ýʧ¡£Õâʱ¼ä¾ÍÐèÒª»Ø¹öÊÂÎñ£¬ÒÔÈ·±£Êý¾ÝµÄÍêÕûÐÔ¡£Laravel DBÀàÖ§³ÖÊÂÎñ²Ù×÷¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

DB::transaction(function () {
    DB::table('users')->update(['votes' => 1]);
    DB::table('posts')->delete();
});

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇÒ»¸ö¼òÆÓµÄÊÂÎñ£¬Ëü¸üÐÂusers±íÖеÄËùÓÐÓû§µÄͶƱÊýΪ1£¬²¢É¾³ýposts±íÖеÄËùÓмͼ¡£ÈôÊÇÔÚÊÂÎñÀú³ÌÖб¬·¢¹ýʧ£¬Ôò»á×Ô¶¯»Ø¹ö²¢¼á³ÖÊý¾ÝµÄÍêÕûÐÔ¡£

DBÀàµÄÅþÁ¬ºÍ±íÃû

ÔÚ´ó´ó¶¼ÇéÐÎÏ£¬Laravel DBÀà»á×Ô¶¯ÅþÁ¬Ä¬ÈϵÄÊý¾Ý¿âºÍ±í£¬¿ÉÊÇÔÚijЩÇéÐÎÏ£¬ÎÒÃÇÐèÒªÊÖ¶¯Ö¸¶¨ÅþÁ¬ºÍ±íÃû¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

$users = DB::connection('connection-1')->table('users')->get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏʾÀýÊÇÔÚµÚÒ»¸öÅþÁ¬ÖÐÏÔʾ»ñÈ¡users±íÖеÄËùÓÐÊý¾Ý¡£ÎÒÃÇ¿ÉÒÔÔÚconfig/database.phpÎļþÖÐÉèÖöà¸öÅþÁ¬ÐÅÏ¢¡£

½áÂÛ

ͨ¹ýLaravel DBÀ࣬ÎÒÃÇ¿ÉÒÔÀû±ãµØ¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£´Ó¼òÆÓµÄÊý¾ÝÅÌÎʵ½ÖØ´óµÄÊÂÎñ²Ù×÷£¬Laravel DBÀ඼ÌṩÁËÏìÓ¦µÄÒªÁìºÍÓï·¨¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèҪ͎áʹÓÃÅÌÎʽṹÆ÷¡¢ÊÂÎñºÍÅþÁ¬£¬ÒÔʵÏÖÎÞа¸ßЧµÄÊý¾Ý²Ù×÷¡£

ÒÔÉϾÍÊÇlaravel dbÀàÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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