我有这些型号:User , Post , Commant , Check_days
评论模型包含:post_id , user_id , comment_date , check_days_comment=(defalt = 0)
---------------------------------------------------------------------------------------
id ---- user_id ---- post_id ---- comment_date --- check_days_comment --- created_at
---------------------------------------------------------------------------------------
1 1 ---- 2 --- 2020-04-01 ---- 0 --- 2020-04-08
2 1 ---- 2 --- 2020-04-01 ---- 0 --- 2020-04-08
3 1 ---- 4 --- 2020-04-02 ---- 0 --- 2020-04-08
4 1 ---- 5 --- 2020-04-02 ---- 0 --- 2020-04-08
5 1 ---- 6 --- 2020-04-03 ---- 0 --- 2020-04-08
6 1 ---- 7 --- 2020-04-03 ---- 0 --- 2020-04-08
7 1 ---- 8 --- 2020-04-03 ---- 0 --- 2020-04-08
--------------------------------------------------------------------------------------
我想通过任务计划每天创建此表Check_days模型包含:user_id,post_id,comment_id,comment_date,user_send_comment
-------------------------------------------------------------------------------------------------------
user_id ---- post_id ---- comment_id ---- comment_date --- user_send_comment --- created_at
1 ---- 2 ---- 1 --- 2020-04-01 ---- true --- 2020-04-08
1 ---- 4 ---- 3 --- 2020-04-02 ---- false --- 2020-04-08
1 ---- 8 ---- 7 --- 2020-04-03 ---- false --- 2020-04-08
-------------------------------------------------------------------------------------------------------
我已经尝试过了:
$notCheck_users = \App\Models\Comment::where('check_days_comment',0)->get();
$all_users = $notCheck_users->pluck('user_id','id')->toArray();
$all_comment_dates = $notCheck_users->pluck('comment_date','id')->toArray();
$comment_dates = array_unique($all_comment_dates);
$users = array_unique($all_users);
$check_day = new Check_days;
foreach($users as $user) {
foreach ($comment_dates as $date) {
$comment = $notCheck_users->where('user_id',$user)->whereDate('comment_date',$date)->first();
if(!is_null($comment)) {
$check_day->create([
'user_id' => $comment->user_id,
'post_id' => $comment->post_id,
'comment_id' => $comment->id,
'comment_date' => $comment->comment_date,
'user_send_comment' => true,
]);
}
}
}
我该怎么写?
您可以使用控制台命令编写此命令,该命令每天在内核中运行,如果flight_date是周末,则使用Carbon类检查/更新奖赏表
$date = Carbon::create($flight_date);
$isWeekend = $date->isWeekend() //Returns true or false