尊龙凯时人生就是搏

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¡¢ÍøÕ¾µØͼ