{"id":23439,"date":"2024-11-21T10:59:03","date_gmt":"2024-11-21T02:59:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23439\/"},"modified":"2024-11-21T10:59:03","modified_gmt":"2024-11-21T02:59:03","slug":"laravel%e5%bc%80%e5%8f%91%ef%bc%9a%e5%a6%82%e4%bd%95%e4%b8%ba%e5%a4%9a%e4%b8%aa%e6%a8%a1%e5%9e%8b%e5%ae%9a%e4%b9%89polymorphic-relationships%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23439\/","title":{"rendered":"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4e3a\u591a\u4e2a\u6a21\u578b\u5b9a\u4e49Polymorphic Relationships\uff1f"},"content":{"rendered":"<p>\u7b14\u8005\u901a\u8fc7\u5b9e\u9645\u9879\u76ee\u7ecf\u9a8c\u53d1\u73b0\uff0c\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u7740\u76f8\u4f3c\u7684\u6570\u636e\u5173\u8054\u9700\u6c42\uff0c\u800c\u6b64\u65f6\u4f7f\u7528polymorphic relationships\u80fd\u591f\u6781\u5927\u5730\u65b9\u4fbf\u6570\u636e\u7684\u7ba1\u7406\u4e0e\u7ef4\u62a4\u3002\u90a3\u4e48\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4e3a\u591a\u4e2a\u6a21\u578b\u5b9a\u4e49polymorphic relationships\uff0c\u4ee5\u6846\u67b6\u4e3a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662fPolymorphic Relationships\uff1f<\/p>\n<p>\u5728\u4ecb\u7ecd\u5b9a\u4e49Polymorphic Relationships\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u4e86\u89e3\u5b83\u7684\u6982\u5ff5\u3002Polymorphic Relationships\u662fLaravel\u6846\u67b6\u4e2d\u7684\u4e00\u79cd\u5173\u7cfb\u7c7b\u578b\uff0c\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u6a21\u578b\u4e0e\u591a\u4e2a\u5176\u4ed6\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8fd9\u6837\uff0c\u5c31\u80fd\u591f\u4f7f\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u6570\u636e\u5173\u8054\u66f4\u52a0\u7075\u6d3b\u3001\u9ad8\u6548\u3002<\/p>\n<p>\u4e8c\u3001Polymorphic Relationships\u7684\u5b9a\u4e49<\/p>\n<p>\u5728Laravel\u6846\u67b6\u4e2d\uff0cPolymorphic Relationships\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49morph\u65b9\u6cd5\u548cmorphTo\u65b9\u6cd5\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u53ef\u5173\u8054\u7684\u6a21\u578b<\/li>\n<\/ol>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ee5\u201c\u8bc4\u8bba\u201d\u548c\u201c\u70b9\u8d5e\u201d\u4e24\u4e2a\u6a21\u578b\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\uff0c\u8fd9\u4e24\u4e2a\u6a21\u578b\u90fd\u9700\u8981\u5173\u8054\u201c\u7528\u6237\u201d\u548c\u201c\u6587\u7ae0\u201d\u3002\u56e0\u6b64\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u53ef\u5173\u8054\u7684\u6a21\u578b\u3002\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u5b9a\u4f4d\u5230\u6587\u4ef6\u5939\u6240\u5728\u4f4d\u7f6e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>php artisan make:model Action<\/p>\n<p>\u8fd9\u6837\uff0c\u5c31\u4f1a\u5728app\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aAction\u7684\u6a21\u578b\u6587\u4ef6\u3002<\/p>\n<ol>\n<li>\u4e3a\u6a21\u578b\u5b9a\u4e49Polymorphic Relationships<\/li>\n<\/ol>\n<p>\u6211\u4eec\u9700\u8981\u5728Action\u6a21\u578b\u4e2d\u521b\u5efamorph\u65b9\u6cd5\u548cmorphTo\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<p>\u2460 morph\u65b9\u6cd5\uff1a<\/p>\n<pre>public function morph()\n{\n    return $this-&amp;gt;morphTo();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u6307\u5b9a\u9700\u8981\u5173\u8054\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528morphTo\u65b9\u6cd5\u8fdb\u884c\u5173\u8054\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5173\u8054\u7528\u6237\u548c\u6587\u7ae0\u4e24\u4e2a\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u6309\u4ee5\u4e0b\u65b9\u5f0f\u5b9a\u4e49\uff1a<\/p>\n<pre>public function actionable()\n{\n    return $this-&amp;gt;morphTo();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u2461 morphTo\u65b9\u6cd5\uff1a<\/p>\n<pre>public function morphTo()\n{\n    return $this-&amp;gt;morphTo('actionable');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u7528\u4e8e\u5173\u8054\u7279\u5b9a\u7684\u6a21\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6a21\u578b\u5173\u8054\u5230\u7528\u6237\u548c\u6587\u7ae0\u4e24\u4e2a\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u6309\u4ee5\u4e0b\u65b9\u5f0f\u5b9a\u4e49\uff1a<\/p>\n<pre>public function actions()\n{\n    return $this-&amp;gt;morphMany(Action::class, 'actionable');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\uff0c\u5c31\u5b8c\u6210\u4e86Action\u6a21\u578b\u7684\u5b9a\u4e49\u3002<\/p>\n<ol>\n<li>\u4e3a\u5176\u4ed6\u6a21\u578b\u521b\u5efaPolymorphic Relationships<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u8ba9\u5176\u4ed6\u6a21\u578b\u80fd\u591f\u4e0eAction\u6a21\u578b\u8fdb\u884c\u5173\u8054\uff0c\u9700\u8981\u5728\u5176\u4ed6\u6a21\u578b\u4e2d\u589e\u52a0morphMany\u65b9\u6cd5\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u589e\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>use IlluminateDatabaseEloquentRelationsMorphMany;\n\n\/**\n * \u83b7\u53d6\u8be5\u6a21\u578b\u7684\u6240\u6709\u201c\u8bc4\u8bba\u201d\u5bf9\u8c61.\n *\/\npublic function comments(): MorphMany\n{\n    return $this-&amp;gt;morphMany(Action::class, 'actionable');\n}\n\n\/**\n * \u83b7\u53d6\u8be5\u6a21\u578b\u7684\u6240\u6709\u201c\u70b9\u8d5e\u201d\u5bf9\u8c61.\n *\/\npublic function likes(): MorphMany\n{\n    return $this-&amp;gt;morphMany(Action::class, 'actionable');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\uff0c\u5176\u4ed6\u6a21\u578b\u5c31\u4e0eAction\u6a21\u578b\u5efa\u7acb\u4e86\u5173\u8054\uff0c\u5e76\u80fd\u591f\u6109\u5feb\u5730\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>Polymorphic Relationships\u662fLaravel\u6846\u67b6\u4e2d\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u5173\u8054\u65b9\u5f0f\uff0c\u80fd\u591f\u5927\u5927\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\u5ea6\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4e3a\u591a\u4e2a\u6a21\u578b\u5b9a\u4e49Polymorphic Relationships\uff0c\u5e0c\u671b\u80fd\u4e3a\u8bfb\u8005\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e2d\u63d0\u4f9b\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u5f00\u53d1\uff1a\u5982\u4f55\u4e3a\u591a\u4e2a\u6a21\u578b\u5b9a\u4e49Polymorphic Relationships\uff1f\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>\u7b14\u8005\u901a\u8fc7\u5b9e\u9645\u9879\u76ee\u7ecf\u9a8c\u53d1\u73b0\uff0c\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u7740\u76f8\u4f3c\u7684\u6570\u636e\u5173\u8054\u9700\u6c42\uff0c\u800c\u6b64\u65f6\u4f7f\u7528polymorphic relationships\u80fd\u591f\u6781\u5927\u5730\u65b9\u4fbf\u6570\u636e\u7684\u7ba1\u7406\u4e0e\u7ef4\u62a4\u3002\u90a3\u4e48\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4e3a\u591a\u4e2a\u6a21\u578b\u5b9a\u4e49polymorphic relationships\uff0c\u4ee5\u6846\u67b6\u4e3a\u4f8b\u3002 \u4e00\u3001\u4ec0\u4e48\u662fPolymorphic Relationships\uff1f \u5728\u4ecb\u7ecd\u5b9a\u4e49Polymorphic Relationships\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u4e86\u89e3\u5b83\u7684\u6982\u5ff5\u3002Polymorphic Relationships\u662fLaravel\u6846\u67b6\u4e2d\u7684\u4e00\u79cd\u5173\u7cfb\u7c7b\u578b\uff0c\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u6a21\u578b\u4e0e\u591a\u4e2a\u5176\u4ed6\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8fd9\u6837\uff0c\u5c31\u80fd\u591f\u4f7f\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u6570\u636e\u5173\u8054\u66f4\u52a0\u7075\u6d3b\u3001\u9ad8\u6548\u3002 \u4e8c\u3001Polymorphic Relationships\u7684\u5b9a\u4e49 \u5728Laravel\u6846\u67b6\u4e2d\uff0cPolymorphic Relationships\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49morph\u65b9\u6cd5\u548cmorphTo\u65b9\u6cd5\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u521b\u5efa\u4e00\u4e2a\u53ef\u5173\u8054\u7684\u6a21\u578b \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ee5\u201c\u8bc4\u8bba\u201d\u548c\u201c\u70b9\u8d5e\u201d\u4e24\u4e2a\u6a21\u578b\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\uff0c\u8fd9\u4e24\u4e2a\u6a21\u578b\u90fd\u9700\u8981\u5173\u8054\u201c\u7528\u6237\u201d\u548c\u201c\u6587\u7ae0\u201d\u3002\u56e0\u6b64\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u53ef\u5173\u8054\u7684\u6a21\u578b\u3002\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u5b9a\u4f4d\u5230\u6587\u4ef6\u5939\u6240\u5728\u4f4d\u7f6e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a php artisan make:model Action \u8fd9\u6837\uff0c\u5c31\u4f1a\u5728app\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aAction\u7684\u6a21\u578b\u6587\u4ef6\u3002 \u4e3a\u6a21\u578b\u5b9a\u4e49Polymorphic Relationships \u6211\u4eec\u9700\u8981\u5728Action\u6a21\u578b\u4e2d\u521b\u5efamorph\u65b9\u6cd5\u548cmorphTo\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u2460 morph\u65b9\u6cd5\uff1a public function morph() { return $this-&amp;gt;morphTo(); } \u767b\u5f55\u540e\u590d\u5236 \u6b64\u65b9\u6cd5\u7528\u4e8e\u6307\u5b9a\u9700\u8981\u5173\u8054\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528morphTo\u65b9\u6cd5\u8fdb\u884c\u5173\u8054\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5173\u8054\u7528\u6237\u548c\u6587\u7ae0\u4e24\u4e2a\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u6309\u4ee5\u4e0b\u65b9\u5f0f\u5b9a\u4e49\uff1a public function actionable() { return $this-&amp;gt;morphTo(); } \u767b\u5f55\u540e\u590d\u5236 \u2461 morphTo\u65b9\u6cd5\uff1a public function morphTo() { return $this-&amp;gt;morphTo(&#8216;actionable&#8217;); } \u767b\u5f55\u540e\u590d\u5236 \u6b64\u65b9\u6cd5\u7528\u4e8e\u5173\u8054\u7279\u5b9a\u7684\u6a21\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6a21\u578b\u5173\u8054\u5230\u7528\u6237\u548c\u6587\u7ae0\u4e24\u4e2a\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u6309\u4ee5\u4e0b\u65b9\u5f0f\u5b9a\u4e49\uff1a public function actions() [&hellip;]<\/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-23439","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23439","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=23439"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23439\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}