我正在使用它来从CSV中提取一些facebook数据
https://github.com/Maatwebsite/Laravel-Excel
public function lifetime_likes_by_gender_and_age_f18_24()
{
$result = $this->reader->select(array('lifetime_likes_by_gender_and_age_f18_24'))->get();
return (int)$result->last()->sum();
}
public function lifetime_likes_by_gender_and_age_f25_34()
{
$result = $this->reader->select(array('lifetime_likes_by_gender_and_age_f25_34'))->get();
return (int)$result->last()->sum();;
}
public function lifetime_likes_by_female()
{
return $this->lifetime_likes_by_gender_and_age_f18_24() + $this->lifetime_likes_by_gender_and_age_f25_34();
}
问题是第一个值返回12,第二个值是112.所以答案应该是124,但它返回136,这表明它加倍了第一个值。
我为什么以及如何解决这个问题的原因? PHP版本是7。
答案 0 :(得分:0)
我猜测select
方法会向读者介绍一个关于所选列的状态。
因此,当你运行18_24方法时,你只需选择它,但是当你运行25_34时,之前的列从未被取消选择,所以你再次选择它们。
请你试试
public function lifetime_likes_by_gender_and_age_f18_24()
{
$result = $this->reader->get(array('lifetime_likes_by_gender_and_age_f18_24'));
return (int)$result->last()->sum();
}
public function lifetime_likes_by_gender_and_age_f25_34()
{
$result = $this->reader->get(array('lifetime_likes_by_gender_and_age_f25_34'));
return (int)$result->last()->sum();
}
我会阅读更多的库源代码,但应该是它。