{"id":22510,"date":"2024-11-21T09:30:19","date_gmt":"2024-11-21T01:30:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22510\/"},"modified":"2024-11-21T09:30:19","modified_gmt":"2024-11-21T01:30:19","slug":"laravel%e6%80%8e%e4%b9%88%e9%aa%8c%e8%af%81%e6%98%af%e5%90%a6%e5%94%af%e4%b8%80","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22510\/","title":{"rendered":"laravel\u600e\u4e48\u9a8c\u8bc1\u662f\u5426\u552f\u4e00"},"content":{"rendered":"<p>\u5728laravel\u6846\u67b6\u4e2d\uff0c\u9a8c\u8bc1\u8f93\u5165\u6570\u636e\u7684\u552f\u4e00\u6027\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u6bd4\u5982\u9a8c\u8bc1\u7528\u6237\u7684\u90ae\u7bb1\u6216\u8005\u624b\u673a\u53f7\uff0c\u786e\u4fdd\u4e0d\u4f1a\u88ab\u91cd\u590d\u6ce8\u518c\u3002<\/p>\n<p>Laravel\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u9a8c\u8bc1\u89c4\u5219\u2014\u2014unique\u3002\u5728\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u53ea\u9700\u8981\u5728\u89c4\u5219\u4e2d\u6dfb\u52a0unique:table,column\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>'username' =&gt; 'required|unique:users,username|max:255',\n'email' =&gt; 'required|unique:users,email|max:255|email',<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0cunique\u89c4\u5219\u6307\u660e\u4e86\u552f\u4e00\u6027\u9a8c\u8bc1\u8868users\u548c\u5b57\u6bb5username\u6216email\uff0c\u8fd9\u5c06\u4fdd\u8bc1\u6ca1\u6709\u5176\u4ed6\u7528\u6237\u4f7f\u7528\u76f8\u540c\u7684\u7528\u6237\u540d\u6216\u90ae\u7bb1\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cunique\u89c4\u5219\u5e76\u4e0d\u80fd\u5b8c\u5168\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u6bd4\u5982\uff0c\u5f53\u6211\u4eec\u66f4\u65b0\u4e00\u6761\u6570\u636e\u65f6\uff0c\u539f\u6709\u7684\u552f\u4e00\u6027\u9a8c\u8bc1\u89c4\u5219\u53ef\u80fd\u4f1a\u62a5\u9519\u2014\u2014\u56e0\u4e3a\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u5b58\u5728\u4e86\u6211\u4eec\u6b63\u5728\u66f4\u65b0\u7684\u67d0\u4e2a\u503c\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cLaravel\u63d0\u4f9b\u4e86\u4e00\u4e2a\u989d\u5916\u7684\u53c2\u6570\uff1aexclude\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>'username' =&gt; 'required|unique:users,username,' . $userId . ',id|max:255',<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c$userId\u4ee3\u8868\u4e86\u5f53\u524d\u8981\u66f4\u65b0\u7684\u7528\u6237ID\uff0c\u6211\u4eec\u901a\u8fc7\u5728unique\u89c4\u5219\u4e2d\u4f7f\u7528exclude\u53c2\u6570\uff0c\u544a\u8bc9Laravel\u5728\u9a8c\u8bc1\u552f\u4e00\u6027\u65f6\uff0c\u6392\u9664\u5f53\u524d\u7528\u6237\u3002<\/p>\n<p>\u9664\u4e86exclude\u53c2\u6570\u4ee5\u5916\uff0cLaravel\u8fd8\u63d0\u4f9b\u4e86other_columns\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u660e\u5176\u4ed6\u5217\u7684\u6761\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>'username' =&gt; Rule::unique('users')\n    -&gt;where(function ($query) use ($other_column_value) {\n        return $query-&gt;where('other_column', $other_column_value);\n    })\n    -&gt;ignore($userId),<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Rule\u7c7b\u548cwhere\u65b9\u6cd5\u5b9a\u4e49\u4e86\u5176\u4ed6\u5217\u7684\u6761\u4ef6\uff0c\u786e\u4fdd\u552f\u4e00\u6027\u9a8c\u8bc1\u53ea\u4f1a\u53d7\u5230\u6307\u5b9a\u6761\u4ef6\u7684\u9650\u5236\u3002ignore\u65b9\u6cd5\u5219\u540c\u6837\u7528\u4e8e\u6392\u9664\u5f53\u524d\u7528\u6237\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b\uff0c\u5728Laravel\u4e2d\u9a8c\u8bc1\u552f\u4e00\u6027\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u5728unique\u89c4\u5219\u4e2d\u4f7f\u7528exclude\u53c2\u6570\uff0c\u6392\u9664\u5f53\u524d\u6570\u636e\u7684\u5f71\u54cd\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528Rule\u7c7b\u548cwhere\u65b9\u6cd5\u5b9a\u4e49\u5176\u4ed6\u5217\u7684\u6761\u4ef6\uff0c\u786e\u4fdd\u552f\u4e00\u6027\u9a8c\u8bc1\u4e0d\u53d7\u5230\u5176\u4ed6\u5217\u7684\u5f71\u54cd\u3002<\/li>\n<\/ol>\n<p>\u4f7f\u7528\u4ee5\u4e0a\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u9a8c\u8bc1\u6570\u636e\u7684\u552f\u4e00\u6027\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u600e\u4e48\u9a8c\u8bc1\u662f\u5426\u552f\u4e00\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728laravel\u6846\u67b6\u4e2d\uff0c\u9a8c\u8bc1\u8f93\u5165\u6570\u636e\u7684\u552f\u4e00\u6027\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u6bd4\u5982\u9a8c\u8bc1\u7528\u6237\u7684\u90ae\u7bb1\u6216\u8005\u624b\u673a\u53f7\uff0c\u786e\u4fdd\u4e0d\u4f1a\u88ab\u91cd\u590d\u6ce8\u518c\u3002 Laravel\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u9a8c\u8bc1\u89c4\u5219\u2014\u2014unique\u3002\u5728\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u53ea\u9700\u8981\u5728\u89c4\u5219\u4e2d\u6dfb\u52a0unique:table,column\u5373\u53ef\uff0c\u4f8b\u5982\uff1a &#8216;username&#8217; =&gt; &#8216;required|unique:users,username|max:255&#8242;, &#8217;email&#8217; =&gt; &#8216;required|unique:users,email|max:255|email&#8217;, \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0cunique\u89c4\u5219\u6307\u660e\u4e86\u552f\u4e00\u6027\u9a8c\u8bc1\u8868users\u548c\u5b57\u6bb5username\u6216email\uff0c\u8fd9\u5c06\u4fdd\u8bc1\u6ca1\u6709\u5176\u4ed6\u7528\u6237\u4f7f\u7528\u76f8\u540c\u7684\u7528\u6237\u540d\u6216\u90ae\u7bb1\u3002 \u4f46\u662f\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cunique\u89c4\u5219\u5e76\u4e0d\u80fd\u5b8c\u5168\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u6bd4\u5982\uff0c\u5f53\u6211\u4eec\u66f4\u65b0\u4e00\u6761\u6570\u636e\u65f6\uff0c\u539f\u6709\u7684\u552f\u4e00\u6027\u9a8c\u8bc1\u89c4\u5219\u53ef\u80fd\u4f1a\u62a5\u9519\u2014\u2014\u56e0\u4e3a\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u5b58\u5728\u4e86\u6211\u4eec\u6b63\u5728\u66f4\u65b0\u7684\u67d0\u4e2a\u503c\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cLaravel\u63d0\u4f9b\u4e86\u4e00\u4e2a\u989d\u5916\u7684\u53c2\u6570\uff1aexclude\u3002\u4f8b\u5982\uff1a &#8216;username&#8217; =&gt; &#8216;required|unique:users,username,&#8217; . $userId . &#8216;,id|max:255&#8217;, \u767b\u5f55\u540e\u590d\u5236 \u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c$userId\u4ee3\u8868\u4e86\u5f53\u524d\u8981\u66f4\u65b0\u7684\u7528\u6237ID\uff0c\u6211\u4eec\u901a\u8fc7\u5728unique\u89c4\u5219\u4e2d\u4f7f\u7528exclude\u53c2\u6570\uff0c\u544a\u8bc9Laravel\u5728\u9a8c\u8bc1\u552f\u4e00\u6027\u65f6\uff0c\u6392\u9664\u5f53\u524d\u7528\u6237\u3002 \u9664\u4e86exclude\u53c2\u6570\u4ee5\u5916\uff0cLaravel\u8fd8\u63d0\u4f9b\u4e86other_columns\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u660e\u5176\u4ed6\u5217\u7684\u6761\u4ef6\u3002\u4f8b\u5982\uff1a &#8216;username&#8217; =&gt; Rule::unique(&#8216;users&#8217;) -&gt;where(function ($query) use ($other_column_value) { return $query-&gt;where(&#8216;other_column&#8217;, $other_column_value); }) -&gt;ignore($userId), \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Rule\u7c7b\u548cwhere\u65b9\u6cd5\u5b9a\u4e49\u4e86\u5176\u4ed6\u5217\u7684\u6761\u4ef6\uff0c\u786e\u4fdd\u552f\u4e00\u6027\u9a8c\u8bc1\u53ea\u4f1a\u53d7\u5230\u6307\u5b9a\u6761\u4ef6\u7684\u9650\u5236\u3002ignore\u65b9\u6cd5\u5219\u540c\u6837\u7528\u4e8e\u6392\u9664\u5f53\u524d\u7528\u6237\u3002 \u603b\u7ed3\u4e00\u4e0b\uff0c\u5728Laravel\u4e2d\u9a8c\u8bc1\u552f\u4e00\u6027\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u95ee\u9898\uff1a \u5728unique\u89c4\u5219\u4e2d\u4f7f\u7528exclude\u53c2\u6570\uff0c\u6392\u9664\u5f53\u524d\u6570\u636e\u7684\u5f71\u54cd\u3002 \u53ef\u4ee5\u4f7f\u7528Rule\u7c7b\u548cwhere\u65b9\u6cd5\u5b9a\u4e49\u5176\u4ed6\u5217\u7684\u6761\u4ef6\uff0c\u786e\u4fdd\u552f\u4e00\u6027\u9a8c\u8bc1\u4e0d\u53d7\u5230\u5176\u4ed6\u5217\u7684\u5f71\u54cd\u3002 \u4f7f\u7528\u4ee5\u4e0a\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u9a8c\u8bc1\u6570\u636e\u7684\u552f\u4e00\u6027\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\u3002 \u4ee5\u4e0a\u5c31\u662flaravel\u600e\u4e48\u9a8c\u8bc1\u662f\u5426\u552f\u4e00\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22510","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22510"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22510\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}