Laravel模型查询-可以使用“ Models:all()”,但“ where / whereRaw”不返回任何数据

筑基

在Laravel PHP,

我尝试查询模型以获取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();

它将不返回任何数据/ null:

{
    "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,您需要:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

您可以使用WHERE逻辑通过Laravel中的数据透视表获取数据吗?

Laravel属于Total()关系,可以使用all()或where()方法,而不仅仅是find()

Laravel 使用 UUID 从模型中查询数据

我可以使用Django模型在数据库上执行复杂的查询吗?

您可以使用Laravel 5在数据库中创建条目而不填充Eloquent模型吗

有没有一种方法可以使用WHERE子句查询SQLite数据库,而该子句并不总是设置所有属性?

LINQ to XML查询Where()函数不返回任何内容

使用两个 Laravel 模型查询并返回结果

EF Core 3.1:Sequence中不包含任何元素,可以使用FromSqlRaw查询存储过程

我可以使用where equals子句和orderby查询Cloud Firestore集合吗?

我可以使用LINQ查询来计算用属性中的where过滤的总行数吗?

SQL查询-使用SELECT WHERE插入INTO不执行任何操作

Laravel - 无法使用“Where”查询过滤数据库

我可以使用查询的行结果来运行子查询并获取返回的数据吗?

使用OR WHERE时,Scala Doobie不返回任何内容

可以使用Laravel的Eloquent hasManyThrough()关系处理此查询,还是需要原始数据库查询?

使用祖先查询Google Cloud数据存储区不返回任何内容

django中有什么方法可以使用数据库而不创建models.py文件吗?

Laravel 5.7:数据库查询不返回任何值

OpenJPA连接查询不返回任何数据

FMDB SQL查询不返回任何数据

SPARQL查询不返回任何数据

MySQL查询不返回任何数据与转义\

如果查询不返回任何数据

为什么查询不返回任何数据?

WCM使用类可以在任何可以使用吊索模型的地方使用,哪个应该是首选,为什么?

MDX 查询不返回任何它应该返回的值 - 可能是由于 where 子句

我可以使用 JpaSpecificationExecutor 在同一个表中分组的子查询中创建 where 子句吗?

laravel雄辩地不返回任何数据