【问题标题】:Laravel Model Query - can use "Models:all()" but "where/whereRaw" return no dataLaravel 模型查询 - 可以使用 "Models:all()" 但 "where/whereRaw" 不返回任何数据
【发布时间】: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 子句没有意义。

标签: php laravel orm model


【解决方案1】:

根据你需要的这条SQL:

SELECT * FROM sadaqah_history WHERE user_id = 25 AND sadaqah_date BETWEEN '2019-06-01' AND '2019-06-30'

如果 $startDate$endDate 确实有 YYYY-MM-DD 格式,你应该使用这个:

$models = SadaqahHistory::where('user_id', $user->id)
      ->whereRaw("sadaqah_date BETWEEN '$startDate' AND '$endDate'")
      ->orderBy('sadaqah_date', 'desc')
      ->get();

【讨论】:

  • 嗨,谢谢:D,现在可以了。但我看到你删除了->actived(),如果我添加它,它不会显示结果。那么,它的目的是什么?
  • actived() 不是原生的 Eloquent 命令。我猜你是从某个地方复制了你的示例,其中 Model 中有这样的函数:func actived() { return $this->where('active',true);} 或类似的东西。
猜你喜欢
  • 2011-09-22
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
相关资源
最近更新 更多