Reputation: 932
I have a Laravel 5.1 app, and am having trouble with a 'where' with a numerical comparison. Specifically I am trying to do:
{{\App\Items::all()->where('paid_price','>',0)->count()}}
The SQL 'type' of paid_price is 'decimal(8,2)'. There are several Item rows where the paid_price is in fact greater than zero, but the code above just yields 0. Stuff like the below which don't rely on numerical comparisons works just fine - can you give me any tips on why the > doesn't work? Thanks a lot
{{\App\Items::all()->where('other_column','some_value')->count()}}
The code for my Items class is below:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Items extends Model {
protected $fillable =['gateway','paid_price','payment_date','auth_date','charge_date','refunded_date'];
protected $dates = ['payment_date','auth_date','charge_date','refunded_date'];
public function setUserIdAttribute($value)
{
$this->attributes['user_id'] = $value ?: null;
}
}
Upvotes: 3
Views: 2738
Reputation: 2483
Try to debug the SQL using the method toSql()
so you can see the SQL query and you can execute it from a client or a frontend like MySQL Workbench:
{{ \App\Items::all()->where('paid_price','>',0)->count()->toSql() }}
Upvotes: 0
Reputation: 3538
The comparison in your where
clause should work correctly. Just remove the all()
.
\App\Items::where('paid_price','>',0)->count()
If you specify all()
, it will first return an array of Items
objects, so when you called where()
function, it will lead to wrong query on the array returned
Upvotes: 3