Search code examples
laravellaravel-5.3laravel-validation

Validation of starting dateTime and ending datetime


I want to validate in function with two columns: 1) entry_dateTime 2) close_dateTime

close_datetime will be more than entry_dateTime. How can I do validation?

For example, entry_dateTime 1/1/2018 09:15:14, then close_dateTime must be more than entry_dateTime, 2/1/2019 08:00:00

My function :

public function store(Request $request)
    {
      $input = $request->all();

        $tradeID= Auth::user()->trade()->create($input);

        $reasons=$request->input('reason');

                        //Loop for creating KEY as Value
                        $data = [];
                        foreach($reasons as $key => $value) {

                            $data[] = ['reason_id' => $value];

                        };         
       
 if($data > 0) {

            foreach ($data as $datum) {
                $tradeID->tradereason()->save (new TradeReason($datum));
            }

        }


Solution

  • You can use the after validation rule to say that the closing time should be after the entry time:

    public function store(Request $request)
    {
        $rules = [
            'entry_dateTime' => ['required', 'date'],
            'close_dateTime' => ['required', 'date', 'after:entry_dateTime'],
        ];
    
        $this->validate($request, $rules);
    
        $input = $request->all();
    
        ...
    }