Toshi
Toshi

Reputation: 1303

Non-static method Illuminate\Support\Collection::where() should not be called statically

I try to get the data array as following:

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

I have this error:

Non-static method Illuminate\Support\Collection::where() should not be called statically

Could you tell me what the problem is?

Upvotes: 3

Views: 11085

Answers (3)

andrew-caulfield
andrew-caulfield

Reputation: 2070

I'd probably define an eloquent relationship in my user model.

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

and then call it like

$user = User::findOrFail($id);

$coupons = $user->coupons;

Upvotes: 2

ThatMSG
ThatMSG

Reputation: 1506

You might want to change it into something like this:

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

..or combined:

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

If you are using Laravel 5.1 or slightly below you might want to consider using pluck instead of lists: https://laravel.com/docs/5.1/collections#method-pluck

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

Upvotes: 2

Pietro
Pietro

Reputation: 988

Try to send an array of where clauses

from official docs

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

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

Upvotes: 2

Related Questions