Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

swapnilmanew's avatar

How to validate date in Laravel?

Hello Sir, I want to add validation to dates in laravel 8. Actually, I am generating a salary for workers but If the salary is already created and the admin tries to create the salary between already created dates then I want to show him the message Please select different dates or something else.

Suppose the salary between 1-10-2022 to 1-15-2022 is already created then I don't want to create another salary between these dates.

I tried to do myself but no idea had come into my mind.

 public function createForHamal(Request $request)
    {
 $result = SalaryMaster::whereBetween//
        }
0 likes
8 replies
Snapey's avatar

not all business logic can be expressed as validation rules.

swapnilmanew's avatar

@snapey i don't want user to create salary if the salary is already created between these two dates.

Randy_Johnson's avatar

Yes, so you don't need it as a validation rule. Validation is just to stop hackers from abusing input, as well as checking if the information coming in is correct type.

You would do something like such:

class SalaryController  extends Controller {

		public function store(Request $request)
		{
				// Run validation on $request here


				$employee = Employee::find($request->employee_id);
				
				if ($request->salary_date !>  $employee->salary->created_on->latest() && $request->salary_date + 30 !<  $employee->salary->end_on->latest())
				{
						$ns= new Salary;
						$ns->employee_id = $employee->id;
						$ns->amount = $request->amount;
						$ns->created_on = now();
						$ns->end_on = now() + 30;

						return "Success!";
				}
				else
				{
								return "Error!";
				}
		}

}

Okay, this won't actually run, but I hope it will help.

Snapey's avatar

if you want help with code you are going to need to show how you store this data

PovilasKorop's avatar

@swapnilmanew just reproduced it, for the sake of writing an article about it.

New validation rule:

php artisan make:rule UniqueSalaryRule --invokable

Then inside that class:

use App\Models\Salary;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\InvokableRule;

class UniqueSalaryRule implements DataAwareRule, InvokableRule
{
    protected $data = [];

    public function __invoke($attribute, $value, $fail)
    {
        if (Salary::where('user_id', $this->data['user_id'])
            ->where('date_from', '<=', $value)
            ->Where('date_to', '>=', $this->data['date_to'])
            ->exists()) {
            $fail('Salary for this period already exists');
        }
    }

    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }
}

And then use that rule in Form Request validation:

class StoreSalaryRequest extends FormRequest
{
    public function rules()
    {
        return [
            'user_id' => 'required',
            'date_from' => ['required', new UniqueSalaryRule()],
            'date_to' => 'required',
            'amount' => 'required',
        ];
    }
}

That worked for my Laravel 9, with new custom rule syntax, read the docs on how to do it with the older syntax.

Inside that custom rule, you need the data from other validation request, so read more about it here: https://laravel.com/docs/9.x/validation#using-rule-objects

Please or to participate in this conversation.