0

我的存储库代码是这样的:

public function getStatusList()
{
    $query = UsersBank::where('user_id', '=', auth()->user()->id)
                      ->where('status', '=', 1)
                      ->count();
    dd($query);               
}

dd($query) = 1 的结果是真的

但我尝试这样的自我:

public function getStatusList()
{
    $query = self::where('user_id', '=', auth()->user()->id)
                      ->where('status', '=', 1)
                      ->count();
    dd($query);               
}

dd($query) = 4 的结果是假的

为什么使用self时,结果不正确?

4

2 回答 2

2

self 仅适用于静态类/属性,我认为您的不是。改用 $this。

像这样

$query = $this->where('user_id', '=', auth()->user()->id)
              ->where('status', '=', 1)
              ->count();
于 2017-01-22T23:22:20.990 回答
0

阅读手册: http: //php.net/manual/en/language.functions.php

如果你使用静态方法,你只能使用 self::staticMethod(); 或命名空间。

如果你使用非静态方法,你可以使用 $this->method();

于 2017-01-23T11:12:59.493 回答