【发布时间】:2019-11-17 04:51:57
【问题描述】:
在 Laravel PHP 中,
我尝试向 Models 查询以获取 json 数据,这里有什么奇怪的,我只能使用像 Models:all() 这样的查询,但不能使用 where。
对于第一个查询
我可以使用以下查询:
$models = SadaqahHistory::all();
它会返回结果:
{
"status": 200,
"message": "Success, user=25, date from=2019-07-01, end date=2019-07-31",
"data": [
{
"id": 1,
"user_id": 372,
"month": 7,
"year": 2019,
"name": null,
"point_total": 198,
"total": 4580,
"sadaqah_date": "2019-07-05 00:00:00",
"status": 0,
"is_dzikir": 0,
"created_at": null,
"updated_at": null
},
{
"id": 2,
"user_id": 1034,
"month": 7,
"year": 2019,
"name": null,
"point_total": 2,
"total": 46,
"sadaqah_date": "2019-07-05 00:00:00",
"status": 0,
"is_dzikir": 0,
"created_at": null,
"updated_at": null
},
...
第二个查询
我不能使用下面的查询,因为会返回空/无数据:
$models = SadaqahHistory::where('user_id', $user->id)
->whereRaw("DATE_FORMAT(sadaqah_date, '%Y-%m-%d') >= '$startDate'
AND '$endDate'")
->actived()
->orderBy('sadaqah_date', 'desc')
->get();
它不会返回任何数据/空:
{
"status": 200,
"message": "Success, user=25, date from=2019-07-01, end date=2019-07-31",
"data": []
}
===编辑,添加SadaqahHistory Model:
namespace App;
class SadaqahHistory extends BaseModel
{
const STATUS_CONVERT_FROM_POINT = 1;
protected $table = 'sadaqah_history';
protected $fillable = [
'user_id',
'name',
'point_total',
'total',
'sadaqah_date',
'status',
'is_dzikir',
'foundation_donate_id',
'created_at',
'updated_at',
'created_by',
'updated_by',
];
protected $hidden = [
'foundation_donate_id',
'created_by',
'updated_by'
];
protected $casts = [
'status' => 'int',
'is_dzikir' => 'int',
'point_total' => 'int',
'total' => 'int',
'user_id' => 'int',
'foundation_donate_id' => 'int',
];
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
}
public function foundationDonate()
{
return $this->hasOne('\App\FoundationDonate', 'id', 'foundation_donate_id');
}
知道为什么,我的第二个模型查询不起作用?
【问题讨论】:
-
首先想到的是你的第二条SQL语句没有返回任何结果。你确定吗?
-
是的,第二条 SQL 语句没有返回任何结果...
-
所以问题就在那里,不在 laravel 中
-
@GiacomoMasseroniChiaro 有什么问题?
-
错误出在 SQL 中,不在 laravel 或 PHP 中。例如 whereRaw 子句没有意义。