验证ip和hostname laravel

时间:2018-05-06 08:55:53

标签: php laravel

如何验证ip和hostname的唯一性? 在数据库中我有列ip,并存储了所有的ips和主机名。我如何检查数据库中是否存在ip?

我知道,对于验证,我需要使用:

$ip = request()->ip;
if(gethostbyname($ip) == $database->column-ip || $ip == $database->column-ip)
  return false;
}

但是如何验证laravel?我知道我可以用独特的规则来做这个,但gethostname呢?

['data.ip' => 'required|unique:ips_table,ip']

有想法如何做到这一点?

2 个答案:

答案 0 :(得分:1)

您应该make your own validation rule,例如require 'sidekiq/web' Rails.application.routes.draw do resources :jobs resource :subscription resources :meetups resources :posts do resources :comments end resources :meetups do resources :discussions end devise_for :users root to: 'welcome#index' get 'posts', to: 'posts#index' get '/jobs', to: 'jobs#index' get '/team', to: 'team#index' get '/meetups', to: 'meetups#index' end

答案 1 :(得分:0)

如果我正确理解您的问题,您是否希望每个主机名的IP唯一?如果是这种情况,您可以在您的唯一验证规则中添加一个子句(假设您的host_name_column位于ips_table中):

['data.ip' => 'required|unique:ips_table,ip,NULL,id,host_name_column,'.$host_name_value]

有关详细信息,请参阅https://laravel.com/docs/5.1/validation#rule-unique