Laravel Scope查询和显示计数

时间:2017-01-01 05:05:12

标签: laravel

User.php

public function queryCountusers($query){
            $query->where(['confirmation' => true])->whereIn('role_id', [2, 3])->count();
        }

UserController.php

 public function index() {
//        $users = App\User::popular()->active()->orderBy('created_at')->get();
        $count_user = User::countusers();
        return view('admin.index', compact('count_user'));
    }

main.blade.php

 <div class="tile-stats tile-red">
                <div class="icon"><i class="entypo-users"></i></div>
                <div class="num" data-start="0" data-end="{{$count_user}}" data-postfix="" data-duration="1500" data-delay="0">0</div>
                <h3>Registered users</h3>
                <p>so far in our blog, and our website.</p>
            </div>

问题是我有错误

  

调用未定义的方法   照亮\数据库\查询\生成器:: countusers()

请帮我解决这个问题。 THakns

1 个答案:

答案 0 :(得分:0)

如果您尝试使用scopes,那么

更改

public function queryCountusers($query){
        $query->where(['confirmation' => true])->whereIn('role_id', [2, 3])->count();
    }

public function scopeCountusers($query){
        return $query->where(['confirmation' => true])->whereIn('role_id', [2, 3])->count();
    }