laravelÅÌÎÊÊý¾Ý½µÐò
laravelÊÇÒ»¸öÊ¢ÐеĿªÔ´php¿ò¼Ü£¬±»ÆÕ±éÓÃÓÚ¿ª·¢webÓ¦ÓóÌÐò¡£ÈôÊÇÄãÕýÔÚʹÓÃlaravel¿ª·¢Ó¦ÓóÌÐò²¢ÐèÒªÅÌÎÊÊý¾Ý½µÐòÅÅÁУ¬ÄÇôÕâƪÎÄÕ¾ÍÊÇΪÄã×¼±¸µÄ¡£
ÔÚLaravelÖУ¬Äã¿ÉÒÔʹÓÃEloquent ORM»òQuery BuilderÀ´ÅÌÎÊÊý¾Ý¿âÖеÄÊý¾Ý¡£ÕâÁ½ÖÖÒªÁ춼ÌṩÁ˽µÐòÅÅÁÐÅÌÎʵļòÆÓÒªÁì¡£
ʹÓÃEloquent ORMÅÌÎÊÊý¾Ý½µÐò
Eloquent ORMÊÇLaravelÖеÄÒ»ÖÖ¹¤¾ß¹ØϵӳÉ䣨ORM£©ÊÖÒÕ£¬Ëü¿ÉÒÔ×ÊÖúÄ㽫Êý¾Ý¿âÖеÄÊý¾ÝÓ³Éäµ½PHP¹¤¾ßÖУ¬´Ó¶ø¸üÈÝÒ׵ضÔÊý¾Ý¾ÙÐвÙ×÷¡£
ÈôÊÇÄãÏëʹÓÃEloquent ORMÅÌÎÊÊý¾Ý½µÐòÅÅÁУ¬¿ÉÒÔÔÚÅÌÎÊÒªÁìºóŲÓÃorderByDesc()ÒªÁ죬¸ÃÒªÁ콫ƾָ֤¶¨µÄÁжÔЧ¹û¼¯¾ÙÐнµÐòÅÅÁС£
¼ÙÉèÄãÓÐÒ»¸öÃûΪ”users”µÄÊý¾Ý¿â±í£¬ÆäÖаüÀ¨ÁËÓû§µÄID¡¢ÐÕÃûºÍ×¢²áʱ¼ä¡£ÒÔÏÂÊÇÒ»¸öʹÓÃEloquent ORMÅÌÎÊÊý¾Ý½µÐòÅÅÁеÄʾÀý£º
$users = AppUser::orderByDesc('created_at')->get();
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ«Æ¾Ö¤”created_at”ÁжԔusers”±í¾ÙÐнµÐòÅÅÁв¢·µ»ØЧ¹û¼¯¡£
ÐèҪעÖصÄÊÇ£¬orderByDesc()ÒªÁìÖ»ÊÊÓÃÓÚEloquent ORMÅÌÎÊ£¬ÈôÊÇÄãʹÓÃQuery BuilderÀ´ÅÌÎÊÊý¾Ý£¬ÔòÐèҪʹÓÃorderBy()ÒªÁ첢ת´ï”desc”²ÎÊý¡£
ʹÓÃQuery BuilderÅÌÎÊÊý¾Ý½µÐò
Query BuilderÊÇLaravelÖеÄÁíÒ»ÖÖÅÌÎÊÒªÁ죬¿ÉÒÔÔÚ²»Ê¹ÓÃORMµÄÇéÐÎÏÂÖ±½ÓÅÌÎÊÊý¾Ý¿â¡£
ÈôÊÇÄãÏëʹÓÃQuery BuilderÅÌÎÊÊý¾Ý½µÐòÅÅÁУ¬¿ÉÒÔÔÚÅÌÎÊÒªÁìºóŲÓÃorderBy()ÒªÁ죬²¢ÔÚ²ÎÊýÖÐת´ïÄãÏëÒª¾ÙÐнµÐòÅÅÁеÄÁÐÃûÒÔ¼°”desc”Òªº¦×Ö¡£
ÒÔÏÂÊÇÒ»¸öQuery Builder½µÐòÅÅÁÐÅÌÎʵÄʾÀý£º
$users = DB::table('users') ->orderBy('created_at', 'desc') ->get();
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ«Æ¾Ö¤”created_at”ÁжԔusers”±í¾ÙÐнµÐòÅÅÁв¢·µ»ØЧ¹û¼¯¡£
ʹÓÃorderBy()ÒªÁìÒ²¿ÉÒÔ¾ÙÐÐÉýÐòÅÌÎÊ£¬Ö»Ð轫”desc”²ÎÊý¸ü¸ÄΪ”asc”¡£Ä¬ÈÏÇéÐÎÏ£¬orderBy()ÒªÁìÒÔÉýÐò·½·¨¾ÙÐÐÅÌÎÊ¡£
½áÂÛ
ÎÞÂÛÄãÊÇʹÓÃEloquent ORMÕÕ¾ÉQuery BuilderÀ´ÅÌÎÊÊý¾Ý£¬Laravel¶¼ÌṩÁ˼òÆÓµÄÒªÁìÀ´¾ÙÐнµÐòÅÅÁС£Í¨¹ýѧϰ±¾ÎÄÌṩµÄÒªÁ죬Äã¿ÉÒÔÇáËɵضÔÊý¾Ý¿âÖеÄÊý¾Ý¾ÙÐÐÅÅÐò£¬´Ó¶ø¸üºÃµØÖª×ãÓ¦ÓóÌÐòµÄÒªÇó¡£
ÒÔÉϾÍÊÇlaravelÅÌÎÊÊý¾Ý½µÐòµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡