jeudi 3 septembre 2020

How to make function work only after the status change?

So I'm making an application where when the user requests a "Leave" they have their days granted and the difference of dates gets removed from the days granted which works well but it would be more functional for the difference to get removed from the field after such "Leave" has been approved(status changed to 1) This is the LeaveController:

public function store(Request $request)
    {
        $this->validate($request,[
            'from'=>'required',
            'to'=>'required',
            'description'=>'required',
            'type'=>'required'
            ]);
            $data=$request->all();
            $data['user_id']=auth()->user()->id;
            $data['message']='';
            $data['status']=0;
            $leave =Leave::create($data);

           /** update users table with leaveBalance() return value*/
            //get logged in user id
            $user_id=auth()->user()->id;
            //get the returned value from User model method
            $leave_balance = new User;            
            //get days granted from db;then updates
            DB::table('users')
            ->where('id', $user_id)
            ->update(['leave_days_granted' => $leave_balance->leaveBalance()]);

            //there's a bit more code here but that has to do with something else so I took it out
            }
        }
        return redirect()->back()->with('message','Leave Created');

    }

Leave Model

protected $dates = ['from', 'to'];

     public function numberOfDays()
     {

         $datesx =Leave::where('user_id', auth()->user()->id)
         ->latest()->first();

         $from =$datesx->from;
         $to =$datesx->to;

         $datetime1 = new DateTime($from);
         $datetime2 = new DateTime($to);

         $interval = $datetime1->diff($datetime2);
         $days = $interval->format('%a');
         return $days;
     }

User Model

public function leaveBalance()
    {
        $leave =new Leave;
        $daysUsed= $leave->numberOfDays();
        $days_granted = User::where('id', auth()->user()->id)
        ->latest()->first();

        $leave_days_granted = $days_granted->leave_days_granted;
        return $leave_days_granted - $daysUsed;
    }

    public function hasAvailableLeave()
    {
        return $this->leaveBalance() > 0;
    }

    public function leaves()
    {
        return $this->hasMany(\App\Leave::class);
    }

So this works well the difference number gets removed from the field "leave_days_granted" after a user requests a leave which is all good but it would be better if the days granted get removed after it has been accepted which would mean after the status changes to "1" in the status field on the leave table and there's already a controller for that so I was wondering if I can use that controller and make an if statement to make it work after the leave is accepted but it wasn't working like that so I'm a little stuck Leave Controller AcceptReject Function

public function acceptReject(Request $request,$id){

        $status = $request->status;
        $message = $request->message;
        $leave = Leave::find($id);

        $leave->update([
            'status' =>$status,
            'message'=>$message
        ]);
        $user = $leave->user_id;
        $usersObject = User::where('id', $user)->get();
        foreach($usersObject as $users){
            $users->notify(new LeaveStatus($leave));
        }
        return redirect()->route('leaves.index')->with('message','Reply Updated');

        }
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire