0

I have these models : User , Post , Commant , Check_days

Comment model contains : 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
--------------------------------------------------------------------------------------   

i want to create this table daily by Task Scheduling Check_days model contains : 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
-------------------------------------------------------------------------------------------------------

i have tried this :

$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,
                ]);
            }
        }
   }

How can I write this?

AliAsghar
  • 49
  • 12
  • I’m afraid there’s no one line magical method for this. You’ll need to use Carbon to determine whether the date is a weekend and you can use the eloquent distinct query for unique dates (so it doesn’t double up if they fly twice on the same day) https://stackoverflow.com/a/32871174/972235 – Savlon Apr 05 '20 at 22:08
  • I changed my questions How can I calculate only the prize_count – AliAsghar Apr 06 '20 at 05:21
  • What have you tried so far? Where are you stuck? – Nico Haase Apr 06 '20 at 15:59
  • please help me . – AliAsghar Apr 08 '20 at 05:12

2 Answers2

1

You can write this with a Console Command that runs every day in your kernel and checks/updates the prize table with the Carbon class if the flight_date is a weekend

$date = Carbon::create($flight_date);
$isWeekend = $date->isWeekend() //Returns true or false
Eliecer
  • 81
  • 2
0

I finally solved it.Thank you for not answering and not helping

$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([

         ]);

        }
AliAsghar
  • 49
  • 12