DilipkumarC
1 month ago

How to validate date in laravel model

Posted 1 month ago by DilipkumarC

I'm trying to not allow the user to save data on a specific date. Now my code will lead to user can't save data even if the user selected a specific date. How do I allow the user to save data when the date is not on the specific date?

Code :

static::save(function($model)
    {
        //User can't save data between this date
        $from = date('2018-01-01');
        $to = date('2018-11-19');

        //Disable save function between specific
        if (User::whereBetween('date', [$from, $to])->get()&&
           (!property_exists($model, 'always_writable') || !$model->always_writable)) {
            $model->setErrors(trans('validation.read_only'));
            return false;
        }
        return $model->validate();
    });

Please sign in or create an account to participate in this conversation.