ÔõÑùÔÚ Laravel Ä£×ÓÅÌÎÊÖÐɨ³ý²»ÏëÒªµÄ×Ö¶Î
laravel ÊÇÒ»¿îºÜÊÇÊ¢ÐÐµÄ php web ¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÇ¿Ê¢¶øÎÞаµÄÊý¾Ý¿â²Ù×÷¹¦Ð§¡£ÔÚʹÓà laravel ÅÌÎÊÊý¾Ýʱ£¬¾³£ÐèҪɸѡ²¢·µ»ØijЩָ¶¨µÄ×ֶΣ¬µ«ÓÐЩÇéÐÎÏ£¬ÎÒÃÇÐèҪɨ³ýijЩ×ֶΣ¬²»ÒªËüÃÇ·ºÆðÔÚÅÌÎÊЧ¹ûÖС£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚ laravel Ä£×ÓÅÌÎÊÖÐɨ³ý²»ÏëÒªµÄ×ֶΡ£
Ê×ÏÈ£¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel ÌṩµÄ select() ÒªÁìÀ´Ö¸¶¨ÅÌÎʵÄ×ֶΣ¬ÀýÈ磺
$users = User::select('name', 'email')->get();
µÇ¼ºó¸´ÖÆ
ÕâÑù»á·µ»ØÒ»¸ö°üÀ¨Ã¿¸öÓû§µÄ name ºÍ email ×ֶεÄÜöÝÍ¡£¿ÉÊÇÈôÊÇÎÒÃÇÐèҪɨ³ýһЩ×ֶΣ¬¸ÃÔõô×öÄØ£¿ÏÂÃæÊÇÁ½ÖÖ·½·¨£º
ÒªÁìÒ»£ºÉ¨³ý×Ö¶Î
ÎÒÃÇ¿ÉÒÔʹÓà select() ÒªÁìÀ´Ö¸¶¨Òª·µ»ØµÄËùÓÐ×ֶΣ¬È»ºóʹÓà except() ÒªÁìÀ´½«²»ÐèÒªµÄ×Ö¶Îɨ³ýµô¡£ÀýÈ磺
$users = User::select('id', 'name', 'email', 'password') ->get() ->map(function ($user) { return collect($user->toArray()) ->except(['password']) ->all(); });
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃÇÊ×ÏÈʹÓà select() ÒªÁìÖ¸¶¨Òª·µ»ØµÄËùÓÐ×ֶΣ¬È»ºóʹÓà get() ÒªÁìÀ´Ö´ÐÐÅÌÎÊ¡£½Ó×ÅÎÒÃÇʹÓà map() ÒªÁìÀ´¶ÔÅÌÎÊЧ¹û¾ÙÐд¦Öóͷ££¬½«Ã¿¸öÓû§µÄÐÅϢת»»ÎªÒ»¸ö¹ØÁªÊý×飬²¢Ê¹Óà except() ÒªÁ콫ÆäÃÜÂë×Ö¶Îɨ³ýµô¡£
ÒªÁì¶þ£ºÒþ²Ø×Ö¶Î
Laravel »¹ÌṩÁËÒ»¸ö¸üΪ¼òÆÓµÄÒªÁ죬¾ÍÊÇʹÓÃÄ£× $hidden ÊôÐÔÀ´Òþ²Ø²»ÐèÒªÊä³öµÄ×ֶΡ£ÀýÈ磺
class User extends Model { protected $hidden = ['password']; }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«Óû§Ä£× $hidden ÊôÐÔÉèÖÃΪ [‘password’]£¬ÕâÑùÔÚÅÌÎÊʱ£¬Laravel ½«×Ô¶¯½«ÃÜÂë×Ö¶Îɨ³ýÔÚЧ¹ûÖ®Íâ¡£
ÐèҪעÖصÄÊÇ£¬ÈôÊÇÎÒÃÇÐèÒªÊä³öij¸ö±»Òþ²ØµÄ×ֶΣ¬¿ÉÒÔÔÚÅÌÎÊʱʹÓà makeVisible() ÒªÁìÀ´ÁýÕÖÄ£× $hidden ÊôÐÔ¡£ÀýÈ磺
$user = User::find(1); $user->makeVisible(['password']);
µÇ¼ºó¸´ÖÆ
Õ⽫»áʹ»ñÈ¡µÄ $user ¹¤¾ß°üÀ¨ÃÜÂë×ֶΡ£
×ÜÖ®£¬ÒÔÉÏÁ½ÖÖÒªÁ춼¿ÉÒÔ×ÊÖúÎÒÃÇÔÚ Laravel Ä£×ÓÅÌÎÊÖÐɨ³ý²»ÏëÒªµÄ×ֶΡ£ÔÚʹÓÃʱ£¬ÎÒÃÇ¿ÉÒÔƾ֤ÏÖÕæÏàÐÎÀ´Ñ¡ÔñʹÓÃÄÄÖÖÒªÁì¡£
ÒÔÉϾÍÊÇÔõÑùÔÚ Laravel Ä£×ÓÅÌÎÊÖÐɨ³ý²»ÏëÒªµÄ×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡