{"id":48904,"date":"2024-12-02T10:40:39","date_gmt":"2024-12-02T02:40:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48904\/"},"modified":"2024-12-02T10:40:39","modified_gmt":"2024-12-02T02:40:39","slug":"%e4%ba%86%e8%a7%a3-laravel-%e7%9a%84-morphto-%e5%85%b3%e7%b3%bb%ef%bc%9a%e5%ae%9e%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48904\/","title":{"rendered":"\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241103\/17305986806726d7182faac.jpg\" class=\"aligncenter\" title=\"\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357\u63d2\u56fe\" alt=\"\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>\u76ee\u5f55<\/strong><\/p>\n<ol>\n<li>\u7b80\u4ecb<\/li>\n<li>\u4ec0\u4e48\u662f\u591a\u6001\u6027\uff1f<\/li>\n<li>morphto \u7684\u5b9e\u9645\u5e94\u7528<\/li>\n<li>\u6570\u636e\u5e93\u8bbe\u7f6e<\/li>\n<li>\u68c0\u7d22\u6570\u636e<\/li>\n<li>\u4e3a\u4ec0\u4e48\u4f7f\u7528 morphto\uff1f<\/li>\n<li>\u7ed3\u8bba<\/li>\n<\/ol>\n<h3> \u4ecb\u7ecd <\/h3>\n<p>\u5f53\u6df1\u5165 laravel \u7684\u4e16\u754c\u65f6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u96c4\u8fa9\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u65e2\u5f3a\u5927\u53c8\u4f18\u96c5\u3002 morphto \u5173\u7cfb\u5c31\u662f\u6b64\u7c7b\u9ad8\u7ea7\u529f\u80fd\u4e4b\u4e00\u3002\u8be5\u5de5\u5177\u662f laravel \u591a\u6001\u5173\u7cfb\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u8ba9\u4e00\u4e2a\u6a21\u578b\u5728\u5355\u4e2a\u5173\u8054\u4e0a\u5c5e\u4e8e\u591a\u4e2a\u5176\u4ed6\u6a21\u578b\u3002\u542c\u8d77\u6765\u5f88\u6df7\u4e71\uff1f\u8ba9\u6211\u4eec\u7528\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50\u548c\u200b\u200b\u89e3\u91ca\u6765\u5206\u89e3\u5b83\u3002<\/p>\n<h3> \u4ec0\u4e48\u662f\u591a\u6001\u6027\uff1f <\/h3>\n<p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u591a\u6001\u6027\u5141\u8bb8\u65b9\u6cd5\u6839\u636e\u5b83\u4eec\u6240\u4f5c\u7528\u7684\u5bf9\u8c61\u505a\u4e0d\u540c\u7684\u4e8b\u60c5\u3002\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u8bc4\u8bba\u7cfb\u7edf\u3002\u8bc4\u8bba\u53ef\u4ee5\u5c5e\u4e8e\u535a\u5ba2\u6587\u7ae0\u3001\u89c6\u9891\uff0c\u751a\u81f3\u7528\u6237\u4e2a\u4eba\u8d44\u6599\u3002\u5728\u8fd9\u91cc\uff0c\u591a\u6001\u5173\u7cfb\u8ba9\u5355\u4e2a\u8bc4\u8bba\u8868\u5b58\u50a8\u6240\u6709\u8fd9\u4e9b\u5173\u7cfb\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7684\u201c\u53ef\u8bc4\u8bba\u201d\u5b9e\u4f53\u521b\u5efa\u5355\u72ec\u7684\u8868\u6216\u5217\u3002<\/p>\n<h3> morphto \u7684\u5b9e\u9645\u5e94\u7528 <\/h3>\n<p>\u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u73b0\u5b9e\u4e16\u754c\u7684\u4f8b\u5b50\u3002\u8003\u8651\u4e00\u4e2a\u5305\u542b\u7528\u6237\u53ef\u4ee5\u8bc4\u8bba\u7684\u5e16\u5b50\u548c\u89c6\u9891\u7684\u7f51\u7ad9\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7684\u5185\u5bb9\u63d0\u4f9b\u6a21\u578b\uff1a<\/p>\n<pre>\n\n\/\/ post model\nclass post extends model\n{\n    public function comments()\n    {\n        return $this-&gt;morphmany(comment::class, 'commentable');\n    }\n}\n\n\/\/ video model\nclass video extends model\n{\n    public function comments()\n    {\n        return $this-&gt;morphmany(comment::class, 'commentable');\n    }\n}\n\n\n<\/pre>\n<p>\u6bcf\u4e2a\u5e16\u5b50\u548c\u89c6\u9891\u90fd\u53ef\u4ee5\u6709\u5f88\u591a\u8bc4\u8bba\uff0c\u4f46\u8bf7\u6ce8\u610f\u4e24\u8005\u5982\u4f55\u4f7f\u7528\u5177\u6709\u76f8\u540c\u53ef\u8bc4\u8bba\u5173\u7cfb\u6807\u8bc6\u7b26\u7684 morphmany\u3002\u8fd9\u5c31\u662f\u9b54\u6cd5\u5f00\u59cb\u7684\u5730\u65b9\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5bf9\u4e8e\u8bc4\u8bba\u6a21\u578b\uff1a<\/p>\n<pre>\n\n\/\/ comment model\nclass comment extends model\n{\n    public function commentable()\n    {\n        return $this-&gt;morphto();\n    }\n}\n\n\n<\/pre>\n<p>comment\u6a21\u578b\u4e2d\u7684\u53ef\u8bc4\u8bba\u65b9\u6cd5\u662fmorphto\u5173\u7cfb\u3002\u5b83\u4e0d\u9700\u8981\u663e\u5f0f\u6307\u5b9a\u76f8\u5173\u7c7b\u578b\uff0c\u56e0\u4e3a laravel \u4f1a\u6839\u636e\u6ce8\u91ca\u8868\u4e2d\u7684\u4e24\u4e2a\u7279\u6b8a\u5217\u52a8\u6001\u5904\u7406\u6b64\u95ee\u9898\uff1acommentable_type \u548c commentable_id\u3002<\/p>\n<h3> \u6570\u636e\u5e93\u8bbe\u7f6e <\/h3>\n<p>\u5bf9\u4e8e\u6570\u636e\u5e93\uff0c\u8bc4\u8bba\u8868\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\n\nschema::create('comments', function (blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;text('body');\n    $table-&gt;unsignedbiginteger('commentable_id');\n    $table-&gt;string('commentable_type');\n    $table-&gt;timestamps();\n});\n\n\n<\/pre>\n<p>\u8fd9\u91cccommentable_id\u5b58\u50a8\u4e86\u76f8\u5173\u6a21\u578b\u7684id\uff08\u65e0\u8bba\u662fpost\u8fd8\u662fvideo\uff09\uff0ccommentable_type\u5b58\u50a8\u4e86\u76f8\u5173\u6a21\u578b\u7684\u7c7b\u540d\uff0c\u8868\u793a\u8be5\u8bc4\u8bba\u4e0e\u4ec0\u4e48\u7c7b\u578b\u7684\u6a21\u578b\u76f8\u5173\u3002<\/p>\n<h3> \u68c0\u7d22\u6570\u636e <\/h3>\n<p>\u8981\u83b7\u53d6\u5e16\u5b50\u7684\u8bc4\u8bba\u53ca\u5176\u7236\u5b9e\u4f53\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<pre>\n\n$post = Post::find(1);\nforeach ($post-&gt;comments as $comment) {\n    echo $comment-&gt;body; \/\/ The comment text\n    \/\/ Accessing the parent model instance:\n    echo $comment-&gt;commentable-&gt;title; \/\/ Assuming 'title' is a field in 'Post'\n}\n\n\n<\/pre>\n<p>commentable \u65b9\u6cd5\u8fd4\u56de\u8bc4\u8bba\u6240\u5c5e\u6a21\u578b\u7684\u5b9e\u4f8b\uff0c\u65e0\u8bba\u662f\u5e16\u5b50\u8fd8\u662f\u89c6\u9891\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48\u4f7f\u7528 morphto\uff1f <\/h3>\n<p>\u4e3a\u4ec0\u4e48\u5e94\u8be5\u4f7f\u7528 <strong>laravel morphto \u5173\u7cfb<\/strong>\uff1f\u6709\u51e0\u4e2a\u5173\u952e\u4f18\u52bf\uff1a<\/p>\n<ol>\n<li> <strong>\u7b80\u5355\u6027<\/strong>\uff1a\u7ba1\u7406\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u65e0\u9700\u4e3a\u6bcf\u79cd\u5173\u7cfb\u7c7b\u578b\u521b\u5efa\u989d\u5916\u7684\u8868\u6216\u5217\u3002<\/li>\n<li> <strong>\u53ef\u6269\u5c55\u6027<\/strong>\uff1a\u968f\u7740\u5e94\u7528\u7a0b\u5e8f\u7684\u589e\u957f\uff0c\u591a\u6001\u5173\u7cfb\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u6e05\u6670\u3001\u66f4\u53ef\u6269\u5c55\u7684\u65b9\u5f0f\u6765\u5904\u7406\u4e0d\u540c\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u8054\u3002<\/li>\n<li> <strong>\u6548\u7387<\/strong>\uff1a\u907f\u514d\u6570\u636e\u5e93\u7ed3\u6784\u4e2d\u7684\u5197\u4f59\uff0c\u4f7f\u5e94\u7528\u7a0b\u5e8f\u7684\u53d1\u5c55\u66f4\u5bb9\u6613\u7ef4\u62a4\u548c\u66f4\u65b0\u3002<\/li>\n<li> <strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u5c06\u591a\u6001\u5173\u7cfb\u5e94\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4ece\u8bc4\u8bba\u548c\u6807\u7b7e\u5230\u6536\u85cf\u5939\u548c\u56fe\u50cf\uff0c\u4e3a\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5185\u5bb9\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ol>\n<h3> \u7ed3\u8bba <\/h3>\n<p>laravel \u7684 morphto \u5173\u7cfb\u8bc1\u660e\u4e86\u8be5\u6846\u67b6\u81f4\u529b\u4e8e\u4f18\u96c5\u7684\u6570\u636e\u5e93\u5904\u7406\u3002\u901a\u8fc7\u6709\u6548\u5730\u7406\u89e3\u548c\u4f7f\u7528\u591a\u6001\u5173\u7cfb\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7f16\u5199\u66f4\u5c11\u7684\u4ee3\u7801\u3001\u964d\u4f4e\u6570\u636e\u5e93\u590d\u6742\u6027\u5e76\u6539\u8fdb\u5e94\u7528\u7a0b\u5e8f\u7ef4\u62a4\u3002\u6df1\u5165\u7814\u7a76\u5e76\u5c1d\u8bd5\u5b83\uff0c\u60a8\u5f88\u5feb\u5c31\u4f1a\u6b23\u8d4f\u5230\u5b83\u4e3a\u60a8\u7684 laravel \u9879\u76ee\u5e26\u6765\u7684\u5f3a\u5927\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002\u5feb\u4e50\u7f16\u7801\uff01<\/p>\n<hr>\n<h3> \u5173\u4e8e laravel morphto \u5173\u7cfb\u7684\u5e38\u89c1\u95ee\u9898\u89e3\u7b54 <\/h3>\n<ol>\n<li>\n<p><strong>laravel \u4e2d\u7684 morphto \u548c belongsto \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<ul>\n<li> morphto \u5141\u8bb8\u4e00\u4e2a\u6a21\u578b\u5c5e\u4e8e\u591a\u4e2a\u6a21\u578b\uff0c\u800c belongsto \u5219\u5efa\u7acb\u4e00\u79cd\u7b80\u5355\u7684\u5173\u7cfb\uff0c\u5176\u4e2d\u4e00\u4e2a\u6a21\u578b\u5c5e\u4e8e\u5355\u4e2a\u6a21\u578b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6211\u53ef\u4ee5\u5bf9\u4e24\u4e2a\u4ee5\u4e0a\u7684\u6a21\u578b\u4f7f\u7528\u591a\u6001\u5173\u7cfb\u5417\uff1f<\/strong><\/p>\n<ul>\n<li>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u6001\u5173\u7cfb\u6765\u5173\u8054\u4e24\u4e2a\u4ee5\u4e0a\u7684\u591a\u4e2a\u6a21\u578b\uff0c\u8fd9\u4f7f\u5b83\u4eec\u6210\u4e3a\u7075\u6d3b\u5173\u8054\u7684\u7406\u60f3\u9009\u62e9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u591a\u6001\u5173\u7cfb\u4e2d\u7684\u5173\u952e\u5217\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<ul>\n<li>\u5173\u952e\u5217\u662fcommentable_id\u548ccommentable_type\uff0c\u5b58\u50a8\u76f8\u5173\u6a21\u578b\u7684id\u548c\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5bf9\u591a\u6001\u5173\u7cfb\u5e94\u7528\u7ea6\u675f\uff1f<\/strong><\/p>\n<ul>\n<li>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728 commentable_type \u6216 commentable_id \u4e0a\u6307\u5b9a\u6761\u4ef6\u6765\u5bf9\u591a\u6001\u5173\u7cfb\u5e94\u7528\u7ea6\u675f\u6216\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9664\u4e86\u6ce8\u91ca\u4e4b\u5916\uff0c\u591a\u6001\u5173\u7cfb\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u529f\u80fd\u5417\uff1f<\/strong><\/p>\n<ul>\n<li>\u7edd\u5bf9\uff01\u591a\u6001\u5173\u7cfb\u53ef\u7528\u4e8e laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6807\u7b7e\u3001\u56fe\u50cf\u3001\u70b9\u8d5e\u548c\u8bb8\u591a\u5176\u4ed6\u5173\u8054\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357 \u6536\u85cf \u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u90a3\u4e48\u672c\u6587\u300a\u4e86\u89e3 Laravel \u7684 MorphTo \u5173\u7cfb\uff1a\u5b9e\u7528\u6307\u5357\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u76ee\u5f55 \u7b80\u4ecb \u4ec0\u4e48\u662f\u591a\u6001\u6027\uff1f morphto \u7684\u5b9e\u9645\u5e94\u7528 \u6570\u636e\u5e93\u8bbe\u7f6e \u68c0\u7d22\u6570\u636e \u4e3a\u4ec0\u4e48\u4f7f\u7528 morphto\uff1f \u7ed3\u8bba \u4ecb\u7ecd \u5f53\u6df1\u5165 laravel \u7684\u4e16\u754c\u65f6\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u96c4\u8fa9\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u65e2\u5f3a\u5927\u53c8\u4f18\u96c5\u3002 morphto \u5173\u7cfb\u5c31\u662f\u6b64\u7c7b\u9ad8\u7ea7\u529f\u80fd\u4e4b\u4e00\u3002\u8be5\u5de5\u5177\u662f laravel \u591a\u6001\u5173\u7cfb\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u8ba9\u4e00\u4e2a\u6a21\u578b\u5728\u5355\u4e2a\u5173\u8054\u4e0a\u5c5e\u4e8e\u591a\u4e2a\u5176\u4ed6\u6a21\u578b\u3002\u542c\u8d77\u6765\u5f88\u6df7\u4e71\uff1f\u8ba9\u6211\u4eec\u7528\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50\u548c\u200b\u200b\u89e3\u91ca\u6765\u5206\u89e3\u5b83\u3002 \u4ec0\u4e48\u662f\u591a\u6001\u6027\uff1f \u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u591a\u6001\u6027\u5141\u8bb8\u65b9\u6cd5\u6839\u636e\u5b83\u4eec\u6240\u4f5c\u7528\u7684\u5bf9\u8c61\u505a\u4e0d\u540c\u7684\u4e8b\u60c5\u3002\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u8bc4\u8bba\u7cfb\u7edf\u3002\u8bc4\u8bba\u53ef\u4ee5\u5c5e\u4e8e\u535a\u5ba2\u6587\u7ae0\u3001\u89c6\u9891\uff0c\u751a\u81f3\u7528\u6237\u4e2a\u4eba\u8d44\u6599\u3002\u5728\u8fd9\u91cc\uff0c\u591a\u6001\u5173\u7cfb\u8ba9\u5355\u4e2a\u8bc4\u8bba\u8868\u5b58\u50a8\u6240\u6709\u8fd9\u4e9b\u5173\u7cfb\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7684\u201c\u53ef\u8bc4\u8bba\u201d\u5b9e\u4f53\u521b\u5efa\u5355\u72ec\u7684\u8868\u6216\u5217\u3002 morphto \u7684\u5b9e\u9645\u5e94\u7528 \u8ba9\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u73b0\u5b9e\u4e16\u754c\u7684\u4f8b\u5b50\u3002\u8003\u8651\u4e00\u4e2a\u5305\u542b\u7528\u6237\u53ef\u4ee5\u8bc4\u8bba\u7684\u5e16\u5b50\u548c\u89c6\u9891\u7684\u7f51\u7ad9\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7684\u5185\u5bb9\u63d0\u4f9b\u6a21\u578b\uff1a \/\/ post model class post extends model { public function comments() { return $this-&gt;morphmany(comment::class, &#8216;commentable&#8217;); } } \/\/ video model class [&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-48904","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48904","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=48904"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48904\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}