简体   繁体   English

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

[英]Laravel return a value if it is not unique

I am checking if the value is unique or not in the database using validation and here is the code: 我正在使用验证检查该值在数据库中是否唯一,这是代码:

 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
            ));
}

This works perfect but it show me a message telling me if the website input is duplicated or not. 效果很好,但它向我显示一条消息,告诉我网站输入是否重复。

What I am trying to do is to return a value from the table leads which contain the website column. 我想做的是从包含网站列的表线索中返回一个值。

I found this code: 我发现以下代码:

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

        }

How I could modify it to return a value from the same duplicated row in the leads table? 我如何修改它以从Leads表中的相同重复行返回值?

Maybe, something like this work for you 也许像这样的事情对你有用

            $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();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM