【问题标题】:Why self not working on the eloquent query (laravel 5.3)?为什么 self 不处理雄辩的查询(laravel 5.3)?
【发布时间】:2017-06-07 10:09:39
【问题描述】:

我的仓库代码是这样的:

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时,结果不正确?

【问题讨论】:

  • self 仅适用于静态类/属性尝试 $this

标签: php laravel laravel-5.3 self laravel-eloquent


【解决方案1】:

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

这样

$query = $this->where('user_id', '=', auth()->user()->id)
              ->where('status', '=', 1)
              ->count();

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 1970-01-01
      • 2018-01-26
      • 2021-07-23
      • 2017-05-19
      • 2017-11-11
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多