我要达到的目的是要从表中删除未满17岁或未成年人的条目。而且我还要删除父表。
这就是我的模型
// Define the "age" property accessor.
public function getAgeAttribute()
{
return now()->diffInYears($this->birthdate);
}
这是在我的控制器中,
public function removeApplicanWhoAreNotAdults()
{
$date = Carbon::createFromDate(2001, 01, 01);
$todaysDate = Carbon::today();
$lastDay = $date->copy()->endOfYear();
$applicants = Applicant::whereBetween("birthdate", [$lastDay, $todaysDate])->get();
$applicants->each(function ($item, $key) {
});
}
控制器上的代码是否足以解决我要解决的问题,以及如何查询年龄在17岁以下的申请人,例如当我的数据库中的年龄为空时
答案 0 :(得分:0)
Solution 1:
$applicant = Applicant::get();
foreach ($applicant as $key => $value) {
$year = now()->diffInYears($value->birthdate);
if($year > 17){
Applicant::where('id', $value->id)->delete();
}
}
第二种方法是首先必须创建一个命令,这样才能创建适当的编程和好的方法。
按照此示例
https://itsolutionstuff.com/post/example-of-cron-job-in-laravel-5example.html
在handel()方法中放置以上代码
public function handle()
{
$applicant = Applicant::get();
foreach ($applicant as $key => $value) {
$year = now()->diffInYears($value->birthdate);
if($year > 17){
Applicant::where('id', $value->id)->delete();
}
}
}