雄辩的laravel 5.8如何匹配数组中的某个值?

在验证中,我没有将产品字段名称设置为唯一,而是希望以某种方式使其具有唯一性,例如具有相同user_id的产品,我希望单个用户具有唯一的产品名称。某些代码也将产品字段名称的值与数据库中的产品名称数组进行了匹配。


    $products = Product::find(auth()->user()->id)->get();


    foreach ($products as $product) {

        $pro_name = $product->name;

    }


    $value = Input::get('name');


    if ($value == anyOf($pro_name) {

        return false;

    }


不负相思意
浏览 185回答 2
2回答

慕码人8056858

也可以使用标准Laravel验证来定义此验证:    $this->validate(        $request,        [            'name' => \Illuminate\Validation\Rule::unique('products', 'name')                ->ignore($id) // id of product being updated. If it is a new product you can remove this, or pass null                ->where(function ($query) {                    return $query->where('user_id', \Auth::user()->id);                })        ]    );
打开App,查看更多内容
随时随地看视频慕课网APP