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

laravelPHP

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?

Best Answer

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();