首页 > 解决方案 > whereMonth 未能按月检索约会日期

问题描述

我的应用程序中有两个模型,Appointment 和 Date,一个约会有很多日期,一个日期属于一个约会,现在我想搜索在特定月份创建的日期的约会,我通过查看约会来做到这一点日期,其中这个日期 fullDate 字段等于过去的月份,我为此使用雄辩的 whereMonth。

遗憾的是,尽管是结果,但这些函数没有返回任何结果,我怀疑这可能是因为 Date 模型 fullDate 字段是一个字符串而不是时间戳,所以也许这就是它失败的原因,我尝试将 fullDate 转换为时间戳,但我得到了相同的结果。

fullDate 是这样的字符串:“26-3-2020”。

这是我的控制器方法:

public function list2(Request $request)
    {
        $month = $request->input('month');

        $appointments = Appointment::whereHas('dates', function ($query) use ($month){
            $query->whereMonth('fullDate', $month);
        })->with(['user'])->latest()->paginate($request->input('paginate'));


    }

日期模型迁移:

public function up()
    {
        Schema::create('dates', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('appointment_id')->index();
            $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade')->onUpdate('cascade');
            $table->string('fullDate');//34-3-2020
            $table->timestamps();
        });
    }

知道为什么它失败了。

标签: phplaravel

解决方案


fullDate列类型应该是时间戳,

你应该在模型中添加这个Date

protected $dates = [
    'fullDate',
];

推荐阅读