我想通过从url提取域名来检查唯一的url。
https://stackoverflow.com和https://stackoverflow.com/something?foo=bar应该显示错误。
我有如下表格要求,
public function rules()
{
return [
'name' => 'required',
'url' => 'required|unique:sites,url',
'favicon' => 'nullable|image',
];
}我试过更改表单请求值,但它不起作用,
// The getDomainName() returns the stackoverflow.com for https://stackoverflow.com
$this->request->set('url',
Site::getDomainName($this->request->get('url'))
);我也试着跟着,
$url = $this->request->get('url');
$domain = 'https://' . Site::getDomainName($url);
$uniqueRule = Rule::unique('sites', 'url')
->where(function ($query) use ($domain) {
return $query->where('url', $domain) ? false : true;
});任何帮助都将不胜感激,谢谢!
发布于 2022-04-25 05:20:05
您需要解析url以提取带有协议的域名,然后可以合并到表单请求中。你必须在“规则启动”方法中这样做。
$result = parse_url(request()->url);
request()->merge(['url'=>$result['scheme']."://".$result['host']]);发布于 2022-04-25 06:29:35
用prepareForValidation()求解
protected function prepareForValidation()
{
$this->merge([
'url' => 'https://' . Site::getDomainName($this->request->get('url'))
]);
}https://stackoverflow.com/questions/71994701
复制相似问题