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