laravelÓÅ»¯ÅÌÎÊÓï¾ä
laravel ÊÇÒ»¿î¹¦Ð§Ç¿Ê¢¡¢Ò×ÓÚά»¤µÄ php ¿ò¼Ü£¬ÆÕ±éÓ¦ÓÃÓÚ web ¿ª·¢¡£È»¶ø£¬Ëæ×ÅÊý¾ÝµÄÔöÌí£¬ÅÌÎÊÓï¾äµÄÐÔÄÜ»áÖð½¥±äµÃ»ºÂý£¬Õâ¿ÉÄÜ»áÓ°ÏìÓ¦ÓõÄÏìӦʱ¼äºÍÓû§ÌåÑé¡£±¾ÎĽ«ÏÈÈÝÔõÑùÓÅ»¯ laravel ÖеÄÅÌÎÊÓï¾ä£¬ÒÔÌáÉýÓ¦ÓõÄÐÔÄÜ¡£
ʹÓúÏÊ浀 Eloquent ÅÌÎÊÒªÁì
Laravel ÌṩÁËÒ»Ì×Ç¿Ê¢µÄ ORM£¨¹¤¾ß¹ØϵӳÉ䣩¿ò¼Ü£¬¼´ Eloquent¡£Eloquent ÅÌÎÊÒªÁìͨ³£±ÈÊÖ¶¯±àдÅÌÎÊÓï¾ä¸ü¾«Á·£¬²¢ÇÒ¿ÉÒÔÇáËÉ´¦ÀíÖØ´óµÄ¹ØϵÅÌÎÊ¡£Laravel ÖÐµÄ Eloquent ÅÌÎÊÒªÁìÒ²Òò´Ë³ÉΪ×îÊܽӴýµÄÊý¾Ý»á¼û·½·¨Ö®Ò»¡£ÒÔÏÂÊÇһЩ³£¼ûµÄ Eloquent ÅÌÎÊÒªÁ죺
all()£º·µ»ØËùÓÐÇкÏÌõ¼þµÄ¼Í¼¡£
find()£º·µ»Øµ¥¸ö¼Í¼£¬Æ¾Ö¤Ö÷¼ü²éÕÒ¡£
where()£ºÓÃÓÚÉèÖÃÅÌÎÊÌõ¼þ¡£
orderBy()£ºÆ¾Ö¤Ö¸¶¨×Ö¶ÎÅÅÐò¡£
select()£ºÖ¸¶¨ÅÌÎÊÒª·µ»ØµÄÁС£
join()£ºÓÃÓÚÅþÁ¬±í¡£
groupBy()£ºÆ¾Ö¤×ֶηÖ×é¡£
count()£º·µ»ØÇкÏÌõ¼þµÄ¼Í¼Êý¡£
ÆäÖУ¬Ê¹Óà where() ÒªÁìÉèÖÃÅÌÎÊÌõ¼þºÜÊÇÖ÷Òª¡£¿ÉÒÔƾ֤ÐèÒªÉèÖöà¸öÌõ¼þ£¬²¢Ê¹ÓÃһϵÁеÄÂß¼ÔËËã·ûÍŽáʹÓá£ÀýÈ磬ÒÔÏ´úÂëÅÌÎÊËùÖøÃû³ÆÒÔ¡°j¡±¿ªÍ·£¬ÄêËê´óÓÚ 18 ËêµÄÓû§£º
$users = DB::table('users') ->where('name', 'like', 'j%') ->where('age', '>', 18) ->get();
µÇ¼ºó¸´ÖÆ
×èÖ¹ N+1 ÅÌÎÊÎÊÌâ
ÔÚ Laravel ÖУ¬N+1 ÅÌÎÊÎÊÌâÖ¸µÄÊÇÔÚÅÌÎÊÒ»¸ö±íµÄͬʱ£¬¶Ô¹ØÁª±íµÄÊý¾Ý¾ÙÐÐÁË N ´Îµ¥¶ÀÅÌÎÊ¡£ÕâÖÖÅÌÎÊ·½·¨»áµ¼ÖÂÐÔÄÜϽµ£¬Ó¦¸Ã¾¡¿ÉÄÜ×èÖ¹¡£ÀýÈ磬ÒÔÏ´úÂëÅÌÎÊËùÓÐÎÄÕ¼°Æä×÷Õߣº
$posts = AppPost::all(); foreach ($posts as $post) { echo $post->title; echo $post->author->name; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë»áÕë¶Ôÿ¸öÎÄÕ¶¼Ö´ÐÐÒ»´ÎÅÌÎÊ£¬ÒÔ»ñÈ¡×÷ÕßµÄÃû³Æ¡£Òª×èÖ¹ÕâÖÖÇéÐΣ¬¿ÉÒÔʹÓà with() ÒªÁìÒ»´ÎÐÔ¼ÓÔØËùÓÐÏà¹ØÊý¾Ý¡£ÀýÈ磺
$posts = AppPost::with('author')->get(); foreach ($posts as $post) { echo $post->title; echo $post->author->name; }
µÇ¼ºó¸´ÖÆ
ÕâÑù¾ÍÖ»ÐèÒªÖ´ÐÐÁ½¸öÅÌÎÊ¡£Çë×¢ÖØ£¬with() ÒªÁì¿ÉÒÔ½ÓÊܶà¸ö²ÎÊý¡£
ʹÓÃË÷Òý
Êý¾Ý¿âÖеÄË÷Òý¿ÉÒÔÏÔ×ÅÌá¸ßÅÌÎÊËÙÂÊ£¬Ó¦¸Ã¾¡¿ÉÄܵØʹÓá£ÔÚ Laravel ÖУ¬¿ÉÒÔʹÓÃǨáãÎļþÌí¼ÓË÷Òý¡£ÒÔÏÂʾÀýΪ users ±íÌí¼ÓÃûΪ¡°email_index¡±µÄË÷Òý£º
Schema::table('users', function (Blueprint $table) { $table->index('email', 'email_index'); });
µÇ¼ºó¸´ÖÆ
ÔÚÅÌÎÊʱ£¬¿ÉÒÔʹÓà Laravel µÄÅÌÎʹ¹½¨Æ÷À´Ö¸¶¨ÒªÊ¹ÓõÄË÷Òý¡£ÀýÈ磬ÒÔÏ´úÂ뽫Õë¶Ô email_index Ë÷ÒýÖ´ÐÐÅÌÎÊ£º
$users = DB::table('users')->where('email', '=', $email)->useIndex('email_index')->get();
µÇ¼ºó¸´ÖÆ
»º´æÅÌÎÊЧ¹û
ÔÚijЩÇéÐÎÏ£¬Laravel ÖеÄÅÌÎÊÓï¾ä¿ÉÄܺÜÖØ´ó»òÕߺܺÄʱ¡£ÔÚÕâÖÖÇéÐÎÏ£¬Ó¦¸Ã˼Á¿»º´æÅÌÎÊЧ¹û£¬ÒÔ×èÖ¹Öظ´ÅÌÎÊ¡£ Laravel ÌṩÁ˶àÖÖ»º´æÇý¶¯³ÌÐò£¬°üÀ¨Îļþ¡¢Êý¾Ý¿âºÍ Memcached¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÊý¾Ý¿â»º´æµÄʾÀý£º
$users = Cache::remember('users', $minutes, function () { return DB::table('users')->get(); });
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫ÔÚ»º´æÖд洢Óû§Êý¾Ý£¬²¢ÔÚ»º´æÓâÆÚÇ°·µ»Ø¸ÃÊý¾Ý¡£ÔÚÏÂÒ»¸öÇëÇóÖУ¬½«»áÖØÐÂÖ´ÐÐÅÌÎʲ¢´æ´¢ÐµÄЧ¹û¡£
ʹÓÃÔÉúÅÌÎÊ
ËäÈ»×èֹʹÓÃÔÉú SQL ÅÌÎÊÊÇÒ»¸öºÃµÄʵ¼ù£¬µ«ÔÚijЩÇéÐÎÏ£¬ÔÉúÅÌÎÊÈÔÈ»¿ÉÒÔÌá¸ßÅÌÎÊÐÔÄÜ¡£×ÝÈ»ÔÚʹÓÃÔÉú SQL ÅÌÎÊʱ£¬Ò²¿ÉÒÔʹÓà Laravel µÄÅÌÎʹ¹½¨Æ÷¹¹½¨ÅÌÎÊÓï¾ä¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÔÉú SQL ÅÌÎʵÄʾÀý£º
$users = DB::select('SELECT * FROM users WHERE name = ?', ['John']);
µÇ¼ºó¸´ÖÆ
ÔÚʹÓÃÔÉú SQL ÅÌÎÊʱ£¬Ó¦¸ÃÉóÉ÷´¦ÀíÊäÈë²ÎÊý£¬ÒÔ×èÖ¹ SQL ×¢Èë¹¥»÷¡£
½áÂÛ
Laravel ÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢¡¢Ò×ÓÚά»¤µÄ PHP ¿ò¼Ü£¬ÔÚ´¦Àí´ó×ÚÊý¾Ýʱ¿ÉÄÜ»áÓöµ½ÐÔÄÜÎÊÌâ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËһЩÓÅ»¯ Laravel ÅÌÎÊÓï¾äµÄÒªÁ죬°üÀ¨Ê¹Óà Eloquent Ä£×Ó¡¢×èÖ¹ N+1 ÅÌÎÊ¡¢Ê¹ÓÃË÷Òý¡¢»º´æÅÌÎÊЧ¹ûºÍʹÓÃÔÉúÅÌÎʵȡ£Í¨¹ýÕâЩҪÁ죬¿ÉÒÔÌáÉýÓ¦ÓõÄÐÔÄÜ£¬²¢Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇlaravelÓÅ»¯ÅÌÎÊÓï¾äµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡