非静态方法 IlluminateSupportCollection::where() 不应静态调用


Non-static method IlluminateSupportCollection::where() should not be called statically

我尝试获取数据数组如下:

$coupons = $user->coupons::where('is_activated_flg', 1)->where('is_used_flg', 0)->lists('amount');

我有这个错误:

非静态方法 Illuminate''Support''Collection::where() 不应静态调用

你能告诉我问题出在哪里吗?

您可能希望将其更改为以下内容:

$couponQuery = $user->coupons()->where('is_activated_flg', 1)->where('is_used_flg', 0);
$couponCollection = $couponQuery->get();

..或组合:

$coupons = $user->coupons()->where('is_activated_flg', 1)->where('is_used_flg', 0)->get();

如果您使用的是 Laravel 5.1 或稍低的版本,您可能需要考虑使用 pluck 而不是列表:https://laravel.com/docs/5.1/collections#method-pluck

$plucked = $collection->pluck('name');
$plucked->all();

尝试发送 where 子句数组

来自官方文档

$users = DB::table('users')->where([
    ['status','1'],
    ['subscribed','<>','1'],
])->get();

https://laravel.com/docs/5.2/queries#selects

我可能会在我的用户模型中定义一个雄辩的关系。

  /**
  * User can have many coupons
  */
  public function coupons()
  {
    return $this->hasMany('App'Coupons')->where('is_activated_flg', 1)->where('is_used_flg', 0);
  }

然后像这样称呼它

$user = User::findOrFail($id);
$coupons = $user->coupons;