【发布时间】:2023-03-04 13:38:01
【问题描述】:
我正在尝试检查我的数据库中两列的差异是否低于给定的年数 - 意思是 DATETIME - BIRTHDATE
我试过了;
$result->where(date_diff((strtotime('datetime')-strtotime('student.birth'), '<', $request->search);
但是,我收到一条错误消息,告诉我;
return type of strtotime is a boolean
现在我的想法已经不多了。任何帮助,将不胜感激。
编辑:澄清: 值.php:
public function student()
{
return $this->belongsTo(\App\Student::class);
}
和Student.php
public function values()
{
return $this->hasMany(\App\Value::class);
}
在我保存的学生表中:
$table->DateTime('birth');
在值表中是:
$table->DateTime('datetime');
$table->unsignedBigInteger('student_id');
$table->foreign('student_id')->references('id')->on('students');
第二次编辑:
感谢@motia,我编辑了我的代码:
$age = $search->age;
这会从请求中读取年龄,工作得很好..但是......:
$result = Value::query()
->whereHas('student', function($q) use($age) {
$q->whereRaw(
'TIMESTAMPDIFF(YEAR, students.birth, values.datetime) < ?', [$age]
);
})
->get();
抛出一个错误说明:
SQLSTATE[HY000]: 一般错误: 1 no such function: TIMESTAMPDIFF (SQL: select * from "values" where exists (select * from "students" where "values"."student_id" = "students"."id " 和 TIMESTAMPDIFF(YEAR, students.birth, datetime)
(19 是我的年龄示例值)
【问题讨论】:
-
您好,您能告诉我您的模型之间的关系以及如何在它们之间建立连接吗?我将帮助您使代码尽可能干净。
-
@motia 您好,谢谢!我从模型中添加了关系信息,还有什么有用的吗?