尊龙凯时人生就是搏

laravel ͨ¹ýsqlÅÌÎÊ

laravelÊÇÒ»¸öÊ¢ÐÐµÄ php¿ò¼Ü £¬¿ÉÒÔÉÁ¿ª·¢Ö°Ô±¿ìËÙ¹¹½¨¸ßЧ¡¢¿ÉÀ©Õ¹µÄ web Ó¦ÓóÌÐò ¡£µ±ÄãÐèÒª´ÓÊý¾Ý¿âÖÐÅÌÎÊÊý¾Ýʱ £¬ÔÚlaravelÖÐʹÓÃsqlÅÌÎʺܳ£¼û ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖÐÖ´ÐÐsqlÅÌÎÊ ¡£

×¼±¸ÊÂÇé

ÔÚ×îÏÈʹÓÃLaravelÖ´ÐÐSQLÅÌÎÊ֮ǰ £¬ÎÒÃÇÐèÒªÏÈÉèÖúÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢ ¡£ÔÚLaravelÖÐ £¬ÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢ºÜ¼òÆÓ £¬Ö»ÐèÒª·­¿ª .envÎļþ²¢ÐÞ¸ÄÒÔÏÂÐÅÏ¢¼´¿É£º

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
DB_PASSWORD=password

µÇ¼ºó¸´ÖÆ

ÎÒÃÇÖ»ÐèÒªÐÞ¸ÄÕâЩ²ÎÊý £¬Laravel½«»á×Ô¶¯ÅþÁ¬µ½ÎÒÃǵÄÊý¾Ý¿â £¬²¢Ê¹ÓÃÎÒÃÇÌṩµÄ²ÎÊý¾ÙÐÐÅþÁ¬ ¡£ÏÖÔÚ £¬ÎÒÃÇÒѾ­×¼±¸ºÃ×îÏÈÖ´ÐÐSQLÅÌÎÊÁË ¡£

Ö´ÐÐÅÌÎÊ

Óм¸ÖÖÒªÁì¿ÉÒÔÔÚLaravelÖÐÖ´ÐÐSQLÅÌÎÊ ¡£ÒÔÏÂÊÇһЩ³£ÓõÄÒªÁì ¡£

ʹÓà DB Àà

LaravelµÄDBÀàÌṩÁËһϵÁпÉÓÃÓÚÖ´ÐÐ SQLÅÌÎʵÄÒªÁì ¡£Ê×ÏÈ £¬ÎÒÃÇÐèÒªÔÚÎÒÃǵĿØÖÆÆ÷»òÄ£×ÓÖÐÒýÈëDBÀࣺ

use IlluminateSupportFacadesDB;

µÇ¼ºó¸´ÖÆ

Ò»µ©ÎÒÃÇÒýÈëÁË DB Àà £¬ÎÒÃǾͿÉÒÔʹÓÃÒÔÏÂÒªÁìÀ´Ö´ÐÐ SQL ÅÌÎÊ ¡£

ÅÌÎÊ

DB ÀàµÄ select() ÒªÁì¿ÉÒÔÓÃÓÚÅÌÎÊÊý¾Ý ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

$data = DB::select('SELECT * FROM users');

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇÅÌÎÊÁË users±íÖеÄËùÓÐÊý¾Ý £¬²¢½«Ð§¹û´æ´¢ÔÚ $data ±äÁ¿ÖÐ ¡£Ä㻹¿ÉÒÔʹÓòÎÊýÀ´¶¯Ì¬µØ¹¹½¨ÅÌÎÊ£º

$id = 1;
$data = DB::select('SELECT * FROM users WHERE id = ?', [$id]);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇʹÓÃÁËÒ»¸öռλ·û ?À´ÌåÏÖ $id ²ÎÊýµÄλÖà £¬È»ºó½«¸Ã²ÎÊýת´ï¸ø select() ÒªÁìµÄµÚ¶þ¸ö²ÎÊý ¡£

²åÈë

DB ÀàµÄ insert() ÒªÁì¿ÉÒÔÓÃÓÚ²åÈëÊý¾Ý ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

$name = 'John Doe';
$email = 'johndoe@example.com';
DB::insert('INSERT INTO users (name, email) VALUES (?, ?)', [$name, $email]);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇÏò users ±íÖвåÈëÁËÒ»ÌõÊý¾Ý ¡£

¸üÐÂ

DB ÀàµÄ update() ÒªÁì¿ÉÒÔÓÃÓÚ¸üÐÂÊý¾Ý ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

$id = 1;
$email = 'johndoe@example.com';
DB::update('UPDATE users SET email = ? WHERE id = ?', [$email, $id]);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃǽ« users ±íÖÐ id Ϊ1µÄÓû§µÄemailÊôÐÔ¸üÐÂΪ johndoe@example.com ¡£

ɾ³ý

DB ÀàµÄ delete() ÒªÁì¿ÉÒÔÓÃÓÚɾ³ýÊý¾Ý ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

$id = 1;
DB::delete('DELETE FROM users WHERE id = ?', [$id]);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃǽ« users ±íÖÐidΪ1µÄÓû§É¾³ý ¡£

ʹÓà Query Builder

LaravelÌṩÁËÁíÒ»ÖÖÖ´ÐÐ SQLÅÌÎʵķ½·¨ £¬¼´Query Builder ¡£Query Builder ÊÇÒ»ÖÖÁ´Ê½ÅÌÎÊÓï·¨ £¬¿ÉÒÔÓÃÓÚ¹¹½¨ÖØ´óµÄÅÌÎÊÓï¾ä ¡£

ÅÌÎÊ

ÒÔÏÂÊÇʹÓà Query Builder ÅÌÎÊÊý¾ÝµÄʾÀý£º

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

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇÅÌÎÊÁË users ±íÖеÄËùÓÐÊý¾Ý £¬²¢½«Ð§¹û´æ´¢ÔÚ $data ±äÁ¿ÖÐ ¡£Ä㻹¿ÉÒÔʹÓÃÁ´Ê½ÒªÁ춯̬µØ¹¹½¨ÅÌÎÊ£º

$data = DB::table('users')
->select('name', 'email')
->where('id', '=', 1)
->get();

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇʹÓà select() ÒªÁìÑ¡ÔñÅÌÎÊЧ¹ûÖеÄÁÐ £¬Ê¹Óà where() ÒªÁìÌí¼Ó WHERE ×Ó¾ä ¡£

²åÈë

ÒÔÏÂÊÇʹÓà Query Builder ²åÈëÊý¾ÝµÄʾÀý£º

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

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇÏò users ±íÖвåÈëÁËÒ»ÌõÊý¾Ý ¡£

¸üÐÂ

ÒÔÏÂÊÇʹÓà Query Builder ¸üÐÂÊý¾ÝµÄʾÀý£º

$email = 'johndoe@example.com';
DB::table('users')
   ->where('id', 1)
   ->update(['email' => $email]);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃǽ« users ±íÖÐidΪ1µÄÓû§µÄemailÊôÐÔ¸üÐÂΪ johndoe@example.com ¡£

ɾ³ý

ÒÔÏÂÊÇʹÓà Query Builder ɾ³ýÊý¾ÝµÄʾÀý£º

DB::table('users')->where('id', 1)->delete();

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃǽ« users ±íÖÐidΪ1µÄÓû§É¾³ý ¡£

×ܽá

ÔÚ±¾ÎÄÖÐ £¬ÎÒÃÇÏÈÈÝÁËÔõÑùÔÚLaravelÖÐÖ´ÐÐSQLÅÌÎÊ ¡£Ê¹ÓÃDBÀà»òQuery Builder £¬ÎÒÃÇ¿ÉÒÔÇáËɵØÅÌÎÊ¡¢²åÈë¡¢¸üкÍɾ³ýÊý¾Ý ¡£Ê¹ÓÃLaravelµÄÕâЩ¹¦Ð§ £¬¿ÉÒÔÈÃÎÒÃÇÔ½·¢¸ßЧ¡¢¿É¿¿µÄ¹¹½¨ Web Ó¦ÓóÌÐò ¡£

ÒÔÉϾÍÊÇlaravel ͨ¹ýsqlÅÌÎʵÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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