×ðÁú¿­Ê±

laravel sum ²î±ðÌõ¼þ

laravelÊÇÒ»ÖÖÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄº¯ÊýºÍÓï·¨À´Ìá¸ß¿ª·¢Ð§ÂÊ ¡£ÆäÖУ¬sumº¯ÊýÊÇÔÚlaravelÖзǾ­³£ÓõÄÒ»¸öº¯Êý£¬¿ÉÒÔ¶ÔÒ»¸öÜöÝÍÖеÄÖµ¾ÙÐÐÇóºÍ²Ù×÷ ¡£¿ÉÊÇÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇÓÐʱÐèÒªÔÚ²î±ðµÄÌõ¼þϾÙÐÐÇóºÍ£¬Õâʱ¸ÃÔõÑùÓ¦¶ÔÄØ£¿±¾ÎĽ«ÏÈÈÝlaravelÔõÑùÔÚ²î±ðÌõ¼þϾÙÐÐsumÇóºÍ²Ù×÷ ¡£

Ò»¡¢¼òÆÓÇéÐÎϵÄsum²Ù×÷

ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔºÜÀû±ãµØ¶ÔÒ»¸öÜöÝ;ÙÐÐsum²Ù×÷£¬Ö»ÐèŲÓÃsumº¯Êý¼´¿É ¡£ÀýÈ磬¶ÔÒ»¸öÊý×é¾ÙÐÐÇóºÍ£º

$sum = collect([1, 2, 3, 4, 5])->sum();

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬$sumµÄֵΪ15£¬¼´1+2+3+4+5µÄºÍ ¡£

ÀàËƵØ£¬ÈôÊÇÎÒÃÇÓÐÒ»¸ö¹¤¾ßÊý×飬ÏëÒª¶ÔÆäÖеÄij¸öÊôÐÔ¾ÙÐÐÇóºÍ£¬¿ÉÒÔÕâÑù²Ù×÷£º

$sum = collect([
    ['name' => 'Alice', 'score' => 80],
    ['name' => 'Bob', 'score' => 90],
    ['name' => 'Charlie', 'score' => 70]
])->sum('score');

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬$sumµÄֵΪ240£¬¼´80+90+70µÄºÍ ¡£

¶þ¡¢Ìõ¼þÇóºÍ²Ù×÷

ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃǾ­³£ÐèÒªÔÚ²î±ðµÄÌõ¼þϾÙÐÐsum²Ù×÷£¬Õâʱ¾ÍÐèÒªÓõ½LaravelÖеÄwhereº¯Êý ¡£whereº¯Êý¿ÉÒÔƾָ֤¶¨µÄÌõ¼þ¹ýÂËÜöÝÍÖеÄÖµ ¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÓû§Êý×飬ÿ¸öÓû§¶¼ÓÐÐÕÃûºÍÄêËêÁ½¸öÊôÐÔ£¬ÏëÒªÅÌËãËùÓÐÄêËê´óÓÚ18ËêµÄÓû§ÄêËêÖ®ºÍ£¬¿ÉÒÔÕâÑù²Ù×÷£º

$users = collect([
    ['name' => 'Alice', 'age' => 20],
    ['name' => 'Bob', 'age' => 16],
    ['name' => 'Charlie', 'age' => 25]
]);

$sum = $user->where('age', '>', 18)->sum('age');

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬$sumµÄֵΪ45£¬¼´20+25µÄºÍ ¡£

ÀàËƵØ£¬ÈôÊÇÎÒÃÇÓÐÒ»¸ö¶©µ¥Êý×飬ÿ¸ö¶©µ¥¶¼ÓÐÒ»¸ö״̬ÊôÐÔ£¬ÏëÒªÅÌËãËùÓÐÒÑÍê³É¶©µ¥µÄ½ð¶îÖ®ºÍ£¬¿ÉÒÔÕâÑù²Ù×÷£º

$orders = collect([
    ['id' => 1, 'status' => 'created', 'amount' => 10],
    ['id' => 2, 'status' => 'completed', 'amount' => 20],
    ['id' => 3, 'status' => 'cancelled', 'amount' => 30],
    ['id' => 4, 'status' => 'completed', 'amount' => 40]
]);

$sum = $orders->where('status', 'completed')->sum('amount');

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬$sumµÄֵΪ60£¬¼´20+40µÄºÍ ¡£

Èý¡¢Æ¾Ö¤¶à¸öÌõ¼þÇóºÍ

ÓÐʱ¼ä£¬ÎÒÃÇÐèҪƾ֤¶à¸öÌõ¼þ¾ÙÐÐsum²Ù×÷ ¡£Õâʱ¿ÉÒÔʹÓÃLaravelÖеÄwhereº¯ÊýµÄÁ´Ê½Å²ÓÃÀ´ÊµÏÖ ¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÓû§Êý×飬ÿ¸öÓû§¶¼ÓÐÐÕÃû¡¢ÄêËê¡¢µØÇøÈý¸öÊôÐÔ£¬ÏëÒªÅÌËãËùÓеØÇøΪ±±¾©¡¢ÄêËê´óÓÚ18ËêµÄÓû§ÄêËêÖ®ºÍ£¬¿ÉÒÔÕâÑù²Ù×÷£º

$users = collect([
    ['name' => 'Alice', 'age' => 20, 'area' => 'Beijing'],
    ['name' => 'Bob', 'age' => 16, 'area' => 'Shanghai'],
    ['name' => 'Charlie', 'age' => 25, 'area' => 'Beijing']
]);

$sum = $user->where('age', '>', 18)->where('area', 'Beijing')->sum('age');

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬$sumµÄֵΪ20£¬¼´AliceµÄÄêËê ¡£

ÀàËƵØ£¬ÈôÊÇÎÒÃÇÓÐÒ»¸ö¶©µ¥Êý×飬ÿ¸ö¶©µ¥¶¼ÓÐÒ»¸ö״̬ÊôÐÔºÍÒ»¸öÈÕÆÚÊôÐÔ£¬ÏëÒªÅÌËãËùÓÐÒÑÍê³É¶©µ¥ÖÐ2019Äê1ÔÂ1ÈÕÖ®ºóµÄ¶©µ¥½ð¶îÖ®ºÍ£¬¿ÉÒÔÕâÑù²Ù×÷£º

$orders = collect([
    ['id' => 1, 'status' => 'created', 'amount' => 10, 'date' => '2019-01-01'],
    ['id' => 2, 'status' => 'completed', 'amount' => 20, 'date' => '2019-01-05'],
    ['id' => 3, 'status' => 'cancelled', 'amount' => 30, 'date' => '2018-12-31'],
    ['id' => 4, 'status' => 'completed', 'amount' => 40, 'date' => '2019-02-01']
]);

$sum = $orders->where('status', 'completed')->where('date', '>', '2019-01-01')->sum('amount');

µÇ¼ºó¸´ÖÆ

Õâ¸öÀý×ÓÖУ¬$sumµÄֵΪ20£¬¼´µÚ¶þ¸ö¶©µ¥µÄ½ð¶î ¡£

×ÛÉÏËùÊö£¬LaravelµÄsumº¯ÊýºÍwhereº¯ÊýºÜÊÇÀû±ãÊÊÓ㬿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËÙµØÍê³ÉÌõ¼þÇóºÍ²Ù×÷ ¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤²î±ðµÄÐèÇóÎÞаÔËÓÃÕâЩº¯Êý£¬Ìá¸ß¿ª·¢Ð§ÂÊ ¡£

ÒÔÉϾÍÊÇlaravel sum ²î±ðÌõ¼þµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿