laravelÊý×éת»»³É¹¤¾ß
Ëæ×Ålaravel¿ò¼ÜµÄ¿ìËÙÉú³¤£¬ËüÒѳÉΪphp¿ª·¢Ö°Ô±µÄÊ×Ñ¡¿ò¼ÜÖ®Ò»¡£laravel¿ò¼ÜÌṩÁËÐí¶àÊÊÓõĹ¦Ð§£¬ÆäÖÐÖ®Ò»ÊÇÊý×éÓ빤¾ßÖ®¼äµÄת»»¡£
ͨ³££¬ÔÚPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÊý×éºÍ¹¤¾ßÀ´´æ´¢²¢´¦ÀíÊý¾Ý¡£¿ÉÊÇ£¬ÔÚijЩÇéÐÎÏ£¬½«Êý×éת»»³É¹¤¾ß»ò½«¹¤¾ßת»»³ÉÊý×éÊÇÐëÒªµÄ¡£ÔÚLaravel¿ò¼ÜÖУ¬ÕâЩ²Ù×÷±äµÃºÜÊÇÈÝÒס£
ÏÂÃæÎÒÃÇÀ´¿´¿´ÔõÑù½«Ò»¸öÊý×éת»»³ÉÒ»¸ö¹¤¾ß¡£
PHPÖн«Êý×éת»»Îª¹¤¾ßµÄÒªÁì
ÔÚÔÉúµÄPHPÖУ¬½«Êý×éת»»³É¹¤¾ßͨ³£ÐèҪʹÓÃÇ¿ÖÆÀàÐÍת»»¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ뽫Êý×éת»»Îª¹¤¾ß£º
$array = [ "name" => "John", "age" => 30, "email" => "john@example.com" ]; $object = (object) $array;
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×ÏȽç˵ÁËÒ»¸ö°üÀ¨Èý¸öÔªËصÄÊý×顣Ȼºó£¬ÎÒÃÇʹÓÃÇ¿ÖÆÀàÐÍת»»½«´ËÊý×éת»»Îª¹¤¾ß¡£ÏÖÔÚ£¬Äú¿ÉÒÔÔÚ¹¤¾ßÉÏʹÓá°->¡±·ûºÅƾ֤ÊôÐÔÃû³Æ»á¼û¸Ã¹¤¾ßµÄÔªËØ¡£
ÔÚLaravelÖн«Êý×éת»»Îª¹¤¾ßµÄÒªÁì
Laravel¿ò¼ÜÌṩÁ˸ü±ã½ÝµÄ·½·¨À´×ª»»Êý×éΪ¹¤¾ß¡£ÎÒÃÇ¿ÉÒÔʹÓÃLaravel CollectionÀàÖеġ°toObject¡±ÒªÁ콫Êý×éת»»Îª¹¤¾ß¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪͨ¹ý¡°collect¡±º¯Êý½«Êý×éת»»ÎªLaravelÜöÝÍ£º
$array = [ "name" => "John", "age" => 30, "email" => "john@example.com" ]; $collection = collect($array);
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔʹÓÃtoObjectÒªÁ콫LaravelÜöÝÍת»»Îª¹¤¾ß£º
$object = $collection->toObject();
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ»¹¿ÉÒÔÔÚ½«Êý×éת»»Îª¹¤¾ßʱָ¶¨Ò»¸öÀà×÷Ϊ¹¤¾ßµÄ»ù´¡£º
$object = $collection->mapInto(User::class)->toObject();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÖ¸¶¨¡°User¡±Àà×÷Ϊ¹¤¾ßµÄ»ù´¡¡£Çë×¢ÖØ£¬ÎÒÃÇÐèҪʹÓá°mapInto¡±ÒªÁ콫Êý¾ÝÓ³Éäµ½¸ÃÀàµÄʵÀýÖС£
´¦Àí¹¤¾ß
Ò»µ©ÎÒÃǽ«Êý×éת»»Îª¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÔڸù¤¾ßÉÏÖ´ÐÐÖݪֲÙ×÷¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÉó²é¹¤¾ßµÄÊôÐÔ£º
echo $object->name; // Êä³ö: John
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ»¹¿ÉÒÔÐ޸Ť¾ßµÄÊôÐÔ£º
$object->name = "Jane"; echo $object->name; // Êä³ö: Jane
µÇ¼ºó¸´ÖÆ
ÔÚLaravelÖн«¹¤¾ßת»»ÎªÊý×éµÄÒªÁì
ͬÑù£¬ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÜöÝÍÖеġ°toArray¡±ÒªÁ콫¹¤¾ßת»»ÎªÊý×é¡£
ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ뽫¹¤¾ßת»»ÎªÊý×飺
$array = $object->toArray();
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔÏñ´¦ÀíÊý×éÒ»Ñù´¦Àí¸ÃÊý×é¡£
½áÂÛ
½«Êý×éÓ빤¾ßÏ໥ת»»ÊÇÎÒÃÇÔÚPHP¿ª·¢Öо³£ÐèÒª¾ÙÐеIJÙ×÷Ö®Ò»¡£ÔÚLaravel¿ò¼ÜÖУ¬½«Êý×éת»»Îª¹¤¾ßÉõÖÁ½«¹¤¾ßת»»ÎªÊý×鶼±äµÃºÜÊÇÈÝÒס£Laravel¿ò¼ÜÌṩÁËÒªÁìÀ´ÊµÏÖÕâЩ²Ù×÷£¬²¢ÇÒÕâЩҪÁìʹ´úÂë¸ü¾ß¿É¶ÁÐԺͿÉά»¤ÐÔ¡£ÒÔÊÇÒ»¶¨ÒªÕÆÎÕÕâЩҪÁ죬ÓÅ»¯ÄúµÄLaravel´úÂ룡
ÒÔÉϾÍÊÇlaravelÊý×éת»»³É¹¤¾ßµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡