【问题标题】:Call to a member function count() on int in Laravel?在 Laravel 中调用 int 上的成员函数 count()?
【发布时间】:2019-10-03 15:01:30
【问题描述】:

我要统计本月新创建的学生总数和本月在线学生的数量

public function index () 
  {
     $month = Carbon::now()->month;
     $members = Member::getStudents();
     $saleareas = Member::getSaleAreas();
     Order::getTotalCardOnSaleAreas($saleareas);
     foreach ($members as $member) {
        $newstudent = $member->created_at->month;
        $online = $member->updated_at->month;
        if($month == $newstudent){
            $members = $members->count();
        }
        if($month == $online){
            $onl= $members->count(); // error           
        }
        $orders = Order::where('member_id',$member->id)->get();
        foreach ($orders as $order) {
            $neworder = $order->created_at->month;
            if($month == $neworder && $order->status = 'active'){
                $orders = $orders->count();
            };
        }
     }
      return view('admin.dashboard.index', compact('members','orders','onl','saleareas'));
        }

出现错误:调用 int 上的成员函数 count()

【问题讨论】:

标签: php laravel


【解决方案1】:

因为第一个 if,

if($month == $newstudent){
            $members = $members->count(); //here, the $members changed to an int
}

然后您尝试计算现在为整数的 $members,因此您会收到 Call to a member function count() on int 错误。

我认为你可以这样计算数据:

$total = count(DB::select(DB::raw("Select * from table where *put your condition here* ")));

【讨论】:

  • 如何解决?
猜你喜欢
  • 1970-01-01
  • 2021-04-28
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2021-08-07
  • 2017-02-07
  • 1970-01-01
相关资源
最近更新 更多