php - Laravel date validation in model

49

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();
    });
787

Answer

Solution:

You need to check against the count of the query:

//if count is greater than 0

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 (count(User::whereBetween('date', [$from, $to])->get()) > 0 &&
           (!property_exists($model, 'always_writable') || !$model->always_writable)) {
            $model->setErrors(trans('validation.read_only'));
            return false;
        }
        return $model->validate();
    });
260

Answer

Solution:

quick maffs with unix timestamps.

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

            //Disable save function between specific
            if ( !(property_exists($model, 'always_writable') 
                && $model->always_writable) 
                && (time() >= $form && time() <=$to)){
                $model->setErrors(trans('validation.read_only'));
                return false;
            }
            return $model->validate();
        });

edit: added thealways_writable conditions. edit2: made your always_writable checks actually do something sensible.

779

Answer

Solution:

Not really sure what you meant to achieve with the query:

User::whereBetween('date', [$from, $to])->get()

This basically gets all users in the system with a date column between $from and $to. Presumably this will always be true. Shouldn't that be:

Auth::user()->whereBetween('date', [$from, $to])->count()

Which means: is the current logged in user's date column between $from and $to?

Alternative:

$userDate = Auth::user()->date;
if (($userDate >= $from && $userDate <= $to) && ... ) {
  ...
} 

People are also looking for solutions to the problem: javascript - Ajax Data Display fetched from Database

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.