laravel ÅÌÎʼ¸¸ö×Ö¶Î
ÔÚ¿ª·¢ laravel Ó¦ÓóÌÐòʱ£¬ÅÌÎÊÊý¾Ý¿âÊÇÒ»¸ö·Ç¾³£¼ûµÄ²Ù×÷£¬ÓÈÆäÊǹØÓÚÄÇЩÐèÒª·ºÆð´ó×ÚÊý¾ÝµÄ web Ó¦ÓóÌÐò¡£¿ÉÊÇ£¬ÓÐʱÎÒÃÇÖ»ÐèҪѡÔñһЩÌض¨µÄÁУ¬¶ø²»ÊÇÕû¸ö±íÖеÄËùÓÐÁС£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚ laravel ÖÐÅÌÎʼ¸¸ö×ֶΡ£
ʹÓÃÅÌÎʹ¹½¨Æ÷
Laravel ÌṩÁËÒ»¸öÇ¿Ê¢µÄÅÌÎʹ¹½¨Æ÷£¬¿ÉÒÔ×ÊÖúÎÒÃÇÀû±ãµØÓëÊý¾Ý¿â¾ÙÐн»»¥¡£ÎÒÃÇ¿ÉÒÔʹÓà select ÒªÁìÀ´ÏÞÖÆÎÒÃÇËùÐèµÄÁС£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö users ±í£¬ÆäÖаüÀ¨ id£¬name ºÍ email Èý¸ö×ֶΡ£ÈôÊÇÎÒÃÇÖ»ÐèҪѡÔñÕâЩ×Ö¶ÎÖеÄÁ½¸ö£¬ÎÒÃÇ¿ÉÒÔÕâÑùд£º
$users = DB::table('users')->select('name', 'email')->get();
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓà select ÒªÁìÖ¸¶¨ÁË name ºÍ email ÁС£·µ»ØµÄ $users ¹¤¾ß½«Ö»°üÀ¨ÕâÁ½¸öÁС£
ËäÈ»£¬ÎÒÃÇÒ²¿ÉÒÔÑ¡ÔñʹÓà select À´Ñ¡ÔñËùÓеÄÁУ¬È»ºóʹÓà exclude ÒªÁìÀ´É¨³ýÎÒÃDz»ÐèÒªµÄÁС£ÀýÈ磺
$users = DB::table('users')->select('*')->exclude('id')->get();
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÊ×ÏÈʹÓà select ÒªÁìÑ¡ÔñËùÓеÄÁУ¬È»ºóʹÓà exclude ÒªÁìɨ³ýµôÁË id ÁС£·µ»ØµÄ $users ¹¤¾ß½«Ö»°üÀ¨ name ºÍ email ÁС£
ʹÓà Eloquent
Eloquent ÊÇ Laravel ÖеÄÒ»¸ö ORM£¨¹¤¾ß¹ØϵӳÉ䣩¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃǸüÀû±ãµØÔÚ PHP ÖвÙ×÷Êý¾Ý¿â¡£Eloquent ¾ßÓÐÓëÅÌÎʹ¹½¨Æ÷ÀàËƵÄÒªÁ죬¿ÉÒÔÈÃÎÒÃÇÔÚÅÌÎÊÖнö°üÀ¨ËùÐèµÄÁС£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÃûΪ User µÄ Eloquent Ä£×Ó£¬Ëü¶ÔÓ¦×Å users ±í£º
namespace App; use IlluminateDatabaseEloquentModel; class User extends Model { protected $table = 'users'; }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓà select ÒªÁìÀ´½ö°üÀ¨ËùÐèµÄÁУº
$users = User::select('name', 'email')->get();
µÇ¼ºó¸´ÖÆ
»òÕßʹÓà exclude ÒªÁìɨ³ý²»ÐèÒªµÄÁУº
$users = User::exclude('id')->get();
µÇ¼ºó¸´ÖÆ
ÕâЩҪÁìÓëÅÌÎʹ¹½¨Æ÷ÌṩµÄÒªÁìºÜÊÇÏàËÆ¡£
½áÂÛ
ÅÌÎÊÊý¾Ý¿âÊÇÒ»¸ö³£¼ûµÄ²Ù×÷£¬¶øÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷ºÍ Eloquent Ä£×ÓÀ´ÇáËɵØÏÞÖÆÎÒÃÇËùÐèµÄÁС£Õâ²»µ«¿ÉÒÔïÔÌÊý¾Ý´«ÊäµÄÊýÄ¿£¬»¹¿ÉÒÔÌá¸ßÎÒÃÇÓ¦ÓóÌÐòµÄÐÔÄÜ¡£Ï£Íû±¾ÎĶÔÄãÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇlaravel ÅÌÎʼ¸¸ö×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡