Laravel 如何一次验证两个日期

Laravel 如何一次验证两个日期,laravel,octobercms,Laravel,Octobercms,在后端创建表单中,我需要一次验证两个字段(日期),因为我将检查这两个日期之间是否有值 我被困在这里: 在模型中,我写道: public $rules = [ 'date_in' => 'checkDate', 'date_out' => 'checkDate', ]; 在插件文件中,我写道: public function boot() { Validator::extend('checkDate', function($attrib

在后端创建表单中,我需要一次验证两个字段(日期),因为我将检查这两个日期之间是否有值

我被困在这里:

在模型中,我写道:

public $rules = [
    'date_in' => 'checkDate',
    'date_out' => 'checkDate',
];
在插件文件中,我写道:

    public function boot()
    {
        Validator::extend('checkDate', function($attribute, $value, $parameters) {
                // I need here two dates
                // a rough condition example: $someValue BETWEEN $date1 AND $date2
                if (condition) return false;
        });

        Validator::replacer('checkDate', function ($message, $attribute, $rule, $parameters) {
            return "My error message";
        });
    }

天哪,几个小时的搜索和思考,我终于解决了

不需要Plugin.php中的
boot()
函数。为此,我只使用了
beforeSave
函数。在模型中:

use Db;
use \October\Rain\Exception\ValidationException;
class myModel extends Model
{
    public function beforeSave()
    {
        // I can access dates like that!
        $start = $this->date_in;
        $end = $this->date_out;

        $check = Db::table('mytable')
            ->whereBetween('myColumn', [$start, $end])
            ->get();

        if ($check->count() > 0)
            throw new ValidationException(['date_in' => 'My error message.']);
    }
}