laravelµÄÊý¾Ý¿âÔÚÄÇÀï
laravel ÊÇÒ»¿îÊ¢ÐÐµÄ php ¿ò¼Ü£¬ÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§À´¿ìËÙ¹¹½¨ web Ó¦ÓóÌÐò¡£ÆäÖÐÒ»¸öºÜÊÇÖ÷ÒªµÄ¹¦Ð§¾ÍÊÇÊý¾Ý¿âÖ§³Ö£¬laravel ¿ÉÒÔÇáËɵØÓë¶àÖÖÀàÐ͵ÄÊý¾Ý¿â¾ÙÐм¯³É£¬°üÀ¨ mysql¡¢postgresql¡¢sqlite µÈ¡£ÄÇôÔÚ laravel ÖУ¬Êý¾Ý¿â¾¿¾¹ÔÚÄÇÀïÄØ£¿
×Åʵ£¬ÔÚ Laravel ÖУ¬Êý¾Ý¿â¿ÉÒÔ˵ÊÇÎÞ´¦²»ÔÚ¡£¼È¿ÉÒÔÔÚÉèÖÃÎļþÖÐÉèÖã¬Ò²¿ÉÒÔÔÚ´úÂëÖÐŲÓ㬿ÉÒÔʹÓà Laravel ×Ô¼ºµÄÊý¾Ý¿â²Ù×÷ÒªÁ죬Ҳ¿ÉÒÔʹÓõÚÈý·½Êý¾Ý¿â²Ù×÷¹¤¾ß¿â¡£ÏÂÃæÎÒÃÇÀ´ÖðÒ»Ïàʶ¡£
Êý¾Ý¿âÉèÖÃÎļþ
Laravel µÄÉèÖÃÎļþλÓÚ config Ŀ¼Ï£¬ÆäÖÐÉèÖÃÊý¾Ý¿âµÄÎļþΪ database.php¡£ÔÚ¸ÃÎļþÖУ¬Äã¿ÉÒÔÉèÖÃĬÈϵÄÊý¾Ý¿âÅþÁ¬¡¢Ö§³ÖµÄÊý¾Ý¿âÀàÐÍ¡¢Êý¾Ý¿âÖ÷»úÃû¡¢¶Ë¿ÚºÅ¡¢Óû§Ãû¡¢ÃÜÂëµÈÖݪֲÎÊý¡£¾Ù¸öÀý×Ó£¬ÈçÏÂËùʾ£º
<?php return [ 'default' => env('DB_CONNECTION', 'mysql'), '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', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ], // ... ], // ... ];
µÇ¼ºó¸´ÖÆ
´ÓÉÏÃæµÄÉèÖÃÎļþÖпÉÒÔ¿´³ö£¬Laravel Ö§³Ö¶à¸öÊý¾Ý¿âÅþÁ¬£¬¿ÉÒÔƾ֤²î±ðµÄÐèÇó¾ÙÐÐÉèÖá£Í¬Ê±£¬Laravel Ò²Ö§³Ö´ÓÇéÐαäÁ¿ÖжÁÈ¡Êý¾Ý¿âÅþÁ¬ÐÅÏ¢£¬Àû±ãÔÚ²î±ðµÄÇéÐÎÖоÙÐÐÇл»¡£
Êý¾Ý¿â²Ù×÷ÒªÁì
ÔÚ Laravel ÖУ¬²Ù×÷Êý¾Ý¿â×î³£ÓõÄÊÇʹÓà Eloquent ORM£¬ËüÊÇ Laravel ÄÚÖÃµÄ ORM£¨¹¤¾ß¹ØϵӳÉ䣩¹¤¾ß£¬ÓÃÓÚ²Ù×÷Êý¾Ý¿â±íºÍ¼Í¼£¬ÉÁ¿ª·¢Õß¿ÉÒÔÓÃÃæÏò¹¤¾ßµÄ·½·¨À´´¦Öóͷ£Êý¾Ý¿â¡£¾Ù¸öÀý×Ó£¬ÈçÏÂËùʾ£º
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = User::all(); return view('users.index', compact('users')); } // ... }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ý User::all() ÒªÁìÅÌÎÊÁËËùÓеÄÓû§Êý¾Ý£¬²¢½«Ð§¹ûת´ï¸øÁËÊÓͼ users.index äÖȾ¡£ÕâÑù¾Í¿ÉÒÔÊ¡È¥ÊÖд SQL Óï¾äµÄƶÀ§£¬ÈÃÎÒÃǸüרעÓÚÓªÒµÂß¼µÄʵÏÖ¡£
±ðµÄ£¬Laravel »¹ÌṩÁËÅÌÎʹ¹½¨Æ÷£¨Query Builder£©ºÍÔʼ SQL Óï¾äÖ´ÐеȶàÖÖÊý¾Ý¿â²Ù×÷·½·¨£¬¿ÉÒÔƾ֤ÏêϸµÄÐèÇó¾ÙÐÐÑ¡Ôñ¡£
µÚÈý·½Êý¾Ý¿â¹¤¾ß¿â
³ýÁËʹÓà Laravel ×ÔÉíµÄÊý¾Ý¿â²Ù×÷ÒªÁìÍ⣬¿ª·¢Õß»¹¿ÉÒÔʹÓÃÖÖÖÖµÚÈý·½Êý¾Ý¿â¹¤¾ß¿âÀ´¼ò»¯¿ª·¢£¬ÀýÈç Laravel Debugbar¡¢Laravel Telescope µÈ¹¤¾ß£¬»òÕßÔ½·¢µ×²ãµÄÊý¾Ý¿â²Ù×÷¹¤¾ß¿â£¬Èç Doctrine¡¢DBAL µÈ¡£ÕâЩ¹¤¾ß¿â¶¼ÌṩÁËÔ½·¢Àû±ãµÄʹÓ÷½·¨£¬¿ÉÒÔÉÁ¿ª·¢ÕßÔ½·¢¿ìËٵع¹½¨×Ô¼ºµÄ Web Ó¦ÓóÌÐò¡£
×ܽá
ÔÚ Laravel ÖУ¬Êý¾Ý¿âµÄ´úÂëÊèÉ¢ÔÚ²î±ðµÄµØ·½£¬µ«È´ºÜÊÇÀû±ãµØ¼¯³ÉÔÚÁËÕû¸ö¿ò¼ÜÖС£¿ª·¢Õß¿ÉÒÔÔÚÉèÖÃÎļþÖÐÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢£¬Ê¹Óà Eloquent ORM »òÕßÅÌÎʹ¹½¨Æ÷µÈ·½·¨²Ù×÷Êý¾Ý¿â£¬²¢Ê¹ÓõÚÈý·½¹¤¾ß¿âÀ´¼ò»¯¿ª·¢¡£ÕâЩÌØÕ÷¶¼Èà Laravel ³ÉΪÁËÒ»¸öºÜÊÇÓÅÒìµÄ Web ¿ª·¢¿ò¼Ü¡£
ÒÔÉϾÍÊÇlaravelµÄÊý¾Ý¿âÔÚÄÇÀïµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡