Laravel唯一字段验证

时间:2018-10-20 06:20:56

标签: laravel validation laravel-5.7

我有一个产品模型,其中有一个用于产品编号的文本输入字段。在我的Laravel应用程序中,我验证此字段对于该特定用户而言是唯一的。因此,两个用户可以具有相同的产品编号,但一个用户不能重复。到目前为止,验证规则在添加新产品时有效:

'product_no' => 'nullable|unique:products,product_no,NULL,id,user_id,' . auth()->user()->id

但是,当编辑同一产品时,验证失败。可能是因为它已经存在。我不确定如何在验证中排除现有ID。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据要求的示例

require(igraph)
require(ggplot2)
require(reshape2)

g <- make_star(5)
gAdjMatrix <- as.matrix(as_adj(g))

print(gAdjMatrix)

logMatrix <- (gAdjMatrix == 1)
logMatrix

mm <- logMatrix

mm %>% 
  melt() %>% 
  ggplot(aes(Var2, Var1)) + 
  geom_tile(aes(fill = value, 
                color = value)) + 
  coord_equal() + 
  scale_fill_manual(values = c("black", "white")) + 
  scale_color_manual(values = c("white", "black")) + 
  theme_bw() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank()) + 
  guides(fill = FALSE, color = FALSE) + 
  scale_x_discrete(expand = c(0,0)) + 
  scale_y_discrete(expand = c(0,0))

然后具有唯一性的那个就这样

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Request1 extends FormRequest
{
    private $rules;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function __construct()
    {
         parent::__construct();
         $this->rules = [
            'password' => [
                'nullable',
            ]
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->rules;
    }
}