简体   繁体   English

模型中的Laravel日期验证

[英]Laravel date validation in model

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

quick maffs with unix timestamps. 带有unix时间戳的快速事务。

 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 the always_writable conditions. 编辑:添加了always_writable条件。 edit2: made your always_writable checks actually do something sensible. edit2:使您的always_writable检查实际上是明智的。

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

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. 这基本上使系统中所有用户的日期列在$ from和$ 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? 这意味着:当前登录用户的日期列在$ from和$ to之间?

Alternative: 选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM