如果Laravel不是唯一的,则返回一个值

时间:2016-10-09 08:25:16

标签: php laravel laravel-5 laravel-5.2 laravel-5.1

我正在使用验证检查数据库中的值是否唯一,这里是代码:

 public function store(Request $request)
        {
            $select = 'select';

            //logged in user id
            $userid         = Auth::user() -> id;
            $usercountry    = Auth::user() -> country;

            $this -> validate($request, array(
                'number'        => 'required | unique:leads',
                'website'       => 'required | unique:leads',
                'lead_source'   => 'required | not_in:' . $select,
                'industry'      => 'required | not_in:' . $select,
                'action'        => 'required | not_in:' . $select
            ));
}

这很完美,但它会向我显示一条消息,告诉我网站输入是否重复。

我要做的是从包含网站列的表线索中返回一个值。

  

我找到了这段代码:

if ($validator->fails()) {
            $failedRules = $validator->failed();

        }

如何修改它以从潜在客户表格中的同一重复行返回值?

1 个答案:

答案 0 :(得分:0)

也许,这样的事情适合你

            $rules = [
                'number'        => 'required | unique:leads',
                'website'       => 'required | unique:leads',
                'lead_source'   => 'required | not_in:' . $select,
                'industry'      => 'required | not_in:' . $select,
                'action'        => 'required | not_in:' . $select
            ];

            $data = [
                'number.required' => 'number is required.',
                'number.unique' => 'number has already been taken',
                ....
            ];

            $validate = Validator::make($request->all(),$rules,$data);

            if($validate->fails())
            {
                return redirect()->back()->withErrors($validate->errors())->withInput();
            }