php - 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();
});
}
知道为什么它失败了。
解决方案
fullDate
列类型应该是时间戳,
你应该在模型中添加这个Date
:
protected $dates = [
'fullDate',
];
推荐阅读
- apache - .htaccess
运行 Mamp Pro 和 apache 2.4+ 时声明无效命令 - azure - Azure KeyVault 托管存储帐户
- python - 如何在 Python 安装中重用模块?
- python - 损失函数的正确方式
- c# - iTextSharp 库读取 unciode pdf 文件
- c# - winforms如何使用url启动ie到activex控件
- azure - 如何通过 python sdk 从 Azure 中 VM 的公共 IP 地址获取实例 ID/名称
- javascript - PHP 登录脚本错误地显示 HTML 代码
- javascript - 在 ES5 中创建自定义元素 v1,而不是 ES6
- c++ - 如何通过解析缓冲区并将数据放入正确的结构来处理传入的数据包连接?