When you use unique validation rule with "where" clause and a"null" value cause an error in laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php in getExtraConditions
because the 'null' value is deleted from array $segments

Rules to validate.
return [
'name' => Rule::unique('lodgings')->where('type_id', $this->type),
'type' => 'required',
];
When not pass type_id attribute cause a error.
But this, work.
return [
'name' => Rule::unique('lodgings')->where('type_id', $this->type ?? 'null'),
'type' => 'required',
];
I don't know which is the better solution...
If the value is null, do not add the condition where
//framework\src\Illuminate\Validation\Rules\DatabaseRule.php
/**
* Set a "where" constraint on the query.
*
* @param string|\Closure $column
* @param array|string $value
* @return $this
*/
public function where($column, $value)
{
if (is_array($value)) {
return $this->whereIn($column, $value);
}
if ($column instanceof Closure) {
return $this->using($column);
}
if (!is_null($value)) {
$this->wheres[] = compact('column', 'value');
}
return $this;
}
or if null add null as a value.
//framework\src\Illuminate\Validation\Rules\DatabaseRule.php
/**
* Format the where clauses.
*
* @return string
*/
protected function formatWheres()
{
return collect($this->wheres)->map(function ($where) {
return $where['column'].','. ($where['value'] ?? 'null');
})->implode(',');
}
I don't understand your first solution. Why would you ignore a null constraint?
We could call whereNull():
public function where($column, $value)
{
if (is_array($value)) {
return $this->whereIn($column, $value);
}
if ($column instanceof Closure) {
return $this->using($column);
}
if (is_null($value)) {
return $this->whereNull($column);
}
return $this;
}
I think that is better use null (null) to ignore condition and 'null' (string) to filter by null.
This will be ignore.
$type = null;
Rule::unique('lodgings')->where('type_id', $type),
And this will be compare with null.
Rule::unique('lodgings')->whereNull('type_id'),
I think @staudenmeir's suggestion is the correct solution. This probably needs to be sent to master to avoid breaking people's validation.
This will be fixed in the next major release.
Most helpful comment
I don't understand your first solution. Why would you ignore a
nullconstraint?We could call
whereNull():