After looking at your requirement, you have to make custom validation rule that will return true if no date range don't collide and false otherwise.
In order to implement such thing, you have to make custom validation rule Range with following artisan command.
php artisan make:rule Range
Now, you will see Range.php at App\Rules\ folder.
Then make your code like following.
App\Rules\Range.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Range implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$intersect = true;
for($i=0;$i<count($value); $i++){
for($j=$i+1;$j<count($value); $j++){
if($value[$i]['start']<=$value[$j]['end'] && $value[$i]['end']>=$value[$j]['start'])
{
$intersect = false;
}
}
}
return $intersect;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The dates intersect each other.';
}
}
Now you can use the range rule in your validation like this,
Usage
Case I
If you are validating in controller,
$this->validate($request,[
. . .
'data'=>[new Range],
. . .
]);
Case II
If you have made Request class then
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
. . .
'data' => [new Range],
. . .
];
}
Here, data is the parameter in which date ranges are sent.
I hope you will understand. If any further explanation required feel free to ask.