laravel ÅÌÎÊijЩ×Ö¶Î
laravelÊÇÒ»¸öÊ¢ÐеÄphp web¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶à±ã½ÝµÄ¹¦Ð§ºÍ¹¤¾ßÀ´¼ò»¯¿ª·¢Á÷³Ì¡£ÆäÖÐÒ»¸ö×î³£ÓõĹ¦Ð§ÊÇÅÌÎÊÊý¾Ý¿âÖеÄÊý¾Ý¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖÐÅÌÎÊijЩ×ֶΡ£
Ê×ÏÈ£¬ÈÃÎÒÃǼÙÉèÎÒÃÇÓÐÒ»¸öÃûΪ¡°users¡±µÄ±í£¬ÆäÖаüÀ¨ÒÔÏÂ×ֶΣºid¡¢name¡¢email¡¢password¡¢created_at ºÍ updated_at¡£ÏÖÔÚÎÒÃÇÒªÅÌÎÊËùÓÐÓû§µÄÃû³ÆºÍµç×ÓÓʼþ£¬ÒԱ㽫ÕâЩÐÅϢչʾÔÚÍøÕ¾µÄÓû§ÁбíÉÏ¡£
ÔÚLaravelÖУ¬ÎÒÃÇʹÓÃEloquent ORMÀ´ÓëÊý¾Ý¿â¾ÙÐн»»¥¡£ORM´ú±í¹¤¾ß¹ØϵӳÉ䣬ËüÔÊÐíÎÒÃÇʹÓÃPHP¹¤¾ßÀ´ÌåÏÖÊý¾Ý¿â±íÖеÄÐС£ÒªÅÌÎÊijЩ×ֶΣ¬ÎÒÃÇ¿ÉÒÔʹÓÃEloquentµÄselectÒªÁì¡£
ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룬ÑÝʾÔõÑùÅÌÎÊ¡°users¡±±íÖÐËùÓÐÓû§µÄÃû³ÆºÍµç×ÓÓʼþ£º
use AppUser; $users = User::select('name', 'email')->get(); foreach ($users as $user) { echo $user->name . ' - ' . $user->email . '<br>'; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËUserÄ£×ÓÀàÀ´ÅÌÎÊÊý¾Ý¿â¡£Ê×ÏÈ£¬ÎÒÃÇʹÓÃselectÒªÁìÀ´Ö¸¶¨ÎÒÃÇÒªÅÌÎʵÄ×ֶΣ¬È»ºóʹÓÃgetÒªÁìÀ´»ñȡЧ¹û¡£×îºó£¬ÎÒÃÇʹÓÃforeachÑ»·À´±éÀúЧ¹û£¬²¢Êä³öÿ¸öÓû§µÄÃû³ÆºÍµç×ÓÓʼþ¡£
ÈôÊÇÄúÒªÅÌÎʶà¸ö±í£¬²¢ÇÒÕâЩ±íÖÐÓÐÏàͬµÄ×Ö¶ÎÃû³Æ£¬ÄÇôÄúÐèҪʹÓñíÓÖÃûÀ´Çø·ÖËüÃÇ¡£ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룬ÑÝʾÔõÑùÅÌÎÊ¡°users¡±±íºÍ¡°orders¡±±íÖÐËùÓÐÓû§µÄÃû³ÆºÍ¶©µ¥ºÅÂ룺
use AppUser; use AppOrder; $users = User::select('users.name', 'orders.order_number') ->join('orders', 'users.id', '=', 'orders.user_id') ->get(); foreach ($users as $user) { echo $user->name . ' - ' . $user->order_number . '<br>'; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËjoinÒªÁìÀ´ÅþÁ¬¡°users¡±±íºÍ¡°orders¡±±í£¬²¢Ê¹ÓÃselectÒªÁìÀ´Ö¸¶¨ÅÌÎÊ×ֶΡ£ÎÒÃÇʹÓÃÁ˱íÓÖÃû¡°users¡±ºÍ¡°orders¡±£¬ÒÔÇø·ÖÕâÁ½¸ö±íÖоßÓÐÏàͬÃû³ÆµÄ×ֶΡ£
×ÜÖ®£¬ÔÚLaravelÖÐÅÌÎÊijЩ×Ö¶ÎÊǺÜÊǼòÆӵġ£Äú¿ÉÒÔʹÓÃEloquent ORMÌṩµÄselectÒªÁìÀ´ÅÌÎÊÖ¸¶¨µÄ×ֶΣ¬ÉõÖÁ¿ÉÒÔÅþÁ¬¶à¸ö±íÀ´»ñÈ¡ËùÐèµÄЧ¹û¡£
ÒÔÉϾÍÊÇlaravel ÅÌÎÊijЩ×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡