【问题标题】:Check daily if users have commented on posts每天检查用户是否对帖子发表了评论
【发布时间】:2020-07-17 20:55:47
【问题描述】:

我有这些模型: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,
                ]);
            }
        }
   }

我该怎么写?

【问题讨论】:

  • 恐怕没有任何一种神奇的方法可以做到这一点。您需要使用 Carbon 来确定日期是否是周末,并且您可以使用 eloquent distinct 查询来获取唯一日期(因此如果它们在同一天飞行两次,它就不会加倍)stackoverflow.com/a/32871174/972235
  • 我改变了我的问题我怎样才能只计算prize_count
  • 到目前为止您尝试过什么?你被困在哪里了?
  • 请帮帮我。

标签: php laravel date datediff


【解决方案1】:

我终于解决了。谢谢你不回答也不帮忙

$notCheck_comments =    \App\Models\Comment::where('check_days_comment',0)
        ->whereIn('post_id',[2,3,4,5,6])
        ->groupBy(['user_id','comment_date'])->orderBy('user_id')->orderBy('comment_date')
        ->get(['user_id','comment_date']);

        foreach ($notCheck_comments as $notCheck_comment) {
            $comment  = \App\Models\Comment::where('check_days_comment',0)->whereDate('comment_date',$notCheck_comment->comment_date)->where('user_id',$notCheck_comment->user_id)->first();

            $check_day->create([
                    'post_id' =>$comment->post_id,
                    'user_id' =>$comment->user_id, 
                     'comment_id' =>$comment->comment_id, 
                     'comment_date' =>$comment->comment_date, 
                     'check_days_comment' =>1,
            ]);
         $comment->update([

         ]);

        }

【讨论】:

    【解决方案2】:

    您可以使用每天在内核中运行的控制台命令来编写此代码,如果 flight_date 是周末,则使用 Carbon 类检查/更新奖品表

    $date = Carbon::create($flight_date);
    $isWeekend = $date->isWeekend() //Returns true or false
    

    【讨论】:

    • 我改变了我的问题 我怎样才能只计算 Prize_count ?
    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多