{"id":23400,"date":"2024-11-21T11:41:25","date_gmt":"2024-11-21T03:41:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23400\/"},"modified":"2024-11-21T11:41:25","modified_gmt":"2024-11-21T03:41:25","slug":"laravel%e5%bc%80%e5%8f%91%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8laravel-eloquent%e5%ae%9e%e7%8e%b0%e5%a4%9a%e6%80%81%e5%85%b3%e8%81%94%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23400\/","title":{"rendered":"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Eloquent\u5b9e\u73b0\u591a\u6001\u5173\u8054\uff1f"},"content":{"rendered":"<p>\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528laravel eloquent\u5b9e\u73b0\u591a\u6001\u5173\u8054\uff1f<\/p>\n<p>\u591a\u6001\u5173\u8054\u662f Laravel Eloquent \u7684\u4e00\u9879\u91cd\u8981\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u4f7f\u4e00\u4e2a\u6a21\u578b\u548c\u591a\u4e2a\u4e0d\u540c\u7684\u6a21\u578b\u5efa\u7acb\u5173\u8054\u5173\u7cfb\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u76f8\u5bf9\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u5c24\u5176\u5728\u6570\u636e\u5e93\u8bbe\u8ba1\u4e0a\u975e\u5e38\u65b9\u4fbf\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528 Laravel Eloquent \u5b9e\u73b0\u591a\u6001\u5173\u8054\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u591a\u6001\u5173\u8054\uff1f<\/p>\n<p>\u591a\u6001\u5173\u8054\u662f\u6307\u4e00\u4e2a\u6a21\u578b\u548c\u591a\u4e2a\u4e0d\u540c\u7684\u6a21\u578b\u5efa\u7acb\u5173\u8054\u5173\u7cfb\uff0c\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u5bf9\u901a\u7528\u7c7b\u522b\u7684\u5f15\u7528\u3002\u5b83\u80fd\u4e3a\u8bb8\u591a\u5e94\u7528\u5e26\u6765\u4fbf\u5229\uff0c\u5982\uff1a<\/p>\n<ol>\n<li>\u56fe\u7247\u3001\u97f3\u9891\u548c\u89c6\u9891\u6a21\u578b\u90fd\u53ef\u4ee5\u4e0e\u8bc4\u8bba\u6a21\u578b\u5efa\u7acb\u591a\u6001\u5173\u8054\uff0c\u4f7f\u8bc4\u8bba\u53ef\u4ee5\u5e94\u7528\u4e8e\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>\u7528\u6237\u53ef\u4ee5\u4e0e\u8bc4\u8bba\u6a21\u578b\u5efa\u7acb\u591a\u6001\u5173\u8054\uff0c\u5e76\u88ab\u5e94\u7528\u4e8e\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6587\u7ae0\u3001\u56fe\u7247\u3001\u89c6\u9891\u7b49\u3002<\/li>\n<li>\u8ba2\u5355\u6a21\u578b\u53ef\u4ee5\u4e0e\u6536\u8d27\u5730\u5740\u6a21\u578b\u5efa\u7acb\u591a\u6001\u5173\u8054\uff0c\u4f7f\u8ba2\u5355\u53ef\u4ee5\u914d\u9001\u5230\u591a\u79cd\u5730\u5740\u7c7b\u578b\uff0c\u5982\u5bb6\u5ead\u3001\u516c\u53f8\u3001\u7f51\u70b9\u7b49\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u5b9e\u73b0\u591a\u6001\u5173\u8054\u7684\u65b9\u6cd5<\/p>\n<p>\u4e0b\u9762\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528 Laravel Eloquent \u5b9e\u73b0\u591a\u6001\u5173\u8054\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u7684\u662f\u6570\u636e\u8868\u7684\u8bbe\u8ba1\u3002\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u6a21\u578b\u4e4b\u95f4\u7684\u591a\u6001\u5173\u8054\u5173\u7cfb\u3002\u6b64\u8868\u5e94\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<ol>\n<li>id: \u8868\u4e3b\u952e ID\uff1b<\/li>\n<li>target_type: \u76ee\u6807\u6a21\u578b\u7684\u7c7b\u578b\u540d\u79f0\uff1b<\/li>\n<li>target_id: \u76ee\u6807\u6a21\u578b\u7684 ID\uff1b<\/li>\n<li>source_type: \u6e90\u6a21\u578b\u7684\u7c7b\u578b\u540d\u79f0\uff1b<\/li>\n<li>source_id: \u6e90\u6a21\u578b\u7684 ID\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php use IlluminateDatabaseMigrationsMigration;\nuse IlluminateDatabaseSchemaBlueprint;\nuse IlluminateSupportFacadesSchema;\n\nclass CreateCommentsTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::create('comments', function (Blueprint $table) {\n            $table-&gt;id();\n            $table-&amp;gt;morphs('commentable');\n            $table-&amp;gt;text('content');\n            $table-&amp;gt;timestamps();\n        });\n\n        Schema::create('votes', function (Blueprint $table) {\n            $table-&amp;gt;id();\n            $table-&amp;gt;unsignedBigInteger('user_id');\n            $table-&amp;gt;unsignedBigInteger('voteable_id');\n            $table-&amp;gt;string('voteable_type');\n            $table-&amp;gt;enum('type', ['up', 'down']);\n            $table-&amp;gt;timestamps();\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        Schema::dropIfExists('comments');\n        Schema::dropIfExists('votes');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4ee5\u4e0a\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u65b0\u7684\u8868\uff1acomments\u548cvotes\u3002<\/p>\n<p>comments \u8868\u5305\u542b\u8bc4\u8bba\u6a21\u578b\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u53e6\u5916\u4f7f\u7528morphs()\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e86\u591a\u6001\u5173\u8054\u7684\u6307\u5411\u3002votes \u8868\u4e5f\u7c7b\u4f3c\uff0c\u4f7f\u7528voteable_id\u548cvoteable_type\u5b57\u6bb5\u6765\u5b9e\u73b0\u591a\u6001\u5173\u8054\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728 Eloquent \u6a21\u578b\u4e2d\u5b9a\u4e49\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<p>Comment \u6a21\u578b\uff1a<\/p>\n<pre>&lt;?php namespace AppModels;\n\nuse IlluminateDatabaseEloquentFactoriesHasFactory;\nuse IlluminateDatabaseEloquentModel;\n\nclass Comment extends Model\n{\n    use HasFactory;\n\n    public function commentable()\n    {\n        return $this-&gt;morphTo();\n    }\n\n    public function votes()\n    {\n        return $this-&amp;gt;morphMany(Vote::class, 'voteable');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Vote \u6a21\u578b\uff1a<\/p>\n<pre>&lt;?php namespace AppModels;\n\nuse IlluminateDatabaseEloquentFactoriesHasFactory;\nuse IlluminateDatabaseEloquentModel;\n\nclass Vote extends Model\n{\n    use HasFactory;\n\n    public function voteable()\n    {\n        return $this-&gt;morphTo();\n    }\n\n    public function user()\n    {\n        return $this-&amp;gt;belongsTo(User::class);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4e3a Comment \u6a21\u578b\u548c Vote \u6a21\u578b\u5206\u522b\u5b9a\u4e49\u591a\u6001\u5173\u8054\u5173\u7cfb\u3002\u5728 Comment \u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528morphTo()\u65b9\u6cd5\u5b9a\u4e49\u4e86\u6307\u5411\u7684\u591a\u6001\u5173\u8054\u5173\u7cfb\uff0c\u800c\u5728 Vote \u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528morphMany()\u65b9\u6cd5\u6765\u5b9a\u4e49\u4e86\u5bf9\u8bc4\u8bba\u7684\u591a\u6001\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u591a\u6001\u5173\u8054<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528\u591a\u6001\u5173\u8054\u3002<\/p>\n<p>\u521b\u5efa\u8bc4\u8bba\uff1a<\/p>\n<pre>$article = Article::find(1);\n\n$comment = $article-&amp;gt;comments()-&amp;gt;create([\n    'content' =&amp;gt; 'This is a comment',\n]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u83b7\u53d6\u8bc4\u8bba\u7684\u6295\u7968\uff1a<\/p>\n<pre>$votes = $comment-&amp;gt;votes;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u83b7\u53d6\u6587\u7ae0\u7684\u8bc4\u8bba\uff1a<\/p>\n<pre>$comments = $article-&amp;gt;comments;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6295\u7968\uff1a<\/p>\n<pre>$comment-&amp;gt;votes()-&amp;gt;create([\n    'user_id' =&amp;gt; 1,\n    'type' =&amp;gt; 'up',\n]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u4e86\u591a\u6001\u5173\u8054\u5173\u7cfb\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728 Laravel Eloquent \u6587\u6863\u4e2d\u627e\u5230\u66f4\u591a\u5173\u4e8e\u6b64\u7279\u6027\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u591a\u6001\u5173\u8054\u662f Laravel Eloquent \u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u4f7f\u4e00\u4e2a\u6a21\u578b\u548c\u591a\u4e2a\u4e0d\u540c\u7684\u6a21\u578b\u5efa\u7acb\u5173\u8054\u5173\u7cfb\u3002\u5728\u6570\u636e\u5e93\u8bbe\u8ba1\u548c\u5e94\u7528\u5f00\u53d1\u4e2d\u975e\u5e38\u6709\u7528\u3002\u5728\u4f7f\u7528 Laravel Eloquent \u5b9e\u73b0\u591a\u6001\u5173\u8054\u65f6\uff0c\u9700\u8981\u8bbe\u8ba1\u5173\u8054\u5173\u7cfb\u7684\u4e2d\u95f4\u8868\uff0c\u5e76\u5728 Eloquent \u6a21\u578b\u4e2d\u5b9a\u4e49\u5173\u8054\u5173\u7cfb\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528morphTo() \u548c morphMany() \u65b9\u6cd5\u6765\u5b9e\u73b0\u591a\u6001\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Eloquent\u5b9e\u73b0\u591a\u6001\u5173\u8054\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>\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528laravel eloquent\u5b9e\u73b0\u591a\u6001\u5173\u8054\uff1f \u591a\u6001\u5173\u8054\u662f Laravel Eloquent \u7684\u4e00\u9879\u91cd\u8981\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u4f7f\u4e00\u4e2a\u6a21\u578b\u548c\u591a\u4e2a\u4e0d\u540c\u7684\u6a21\u578b\u5efa\u7acb\u5173\u8054\u5173\u7cfb\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u76f8\u5bf9\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u5c24\u5176\u5728\u6570\u636e\u5e93\u8bbe\u8ba1\u4e0a\u975e\u5e38\u65b9\u4fbf\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528 Laravel Eloquent \u5b9e\u73b0\u591a\u6001\u5173\u8054\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u591a\u6001\u5173\u8054\uff1f \u591a\u6001\u5173\u8054\u662f\u6307\u4e00\u4e2a\u6a21\u578b\u548c\u591a\u4e2a\u4e0d\u540c\u7684\u6a21\u578b\u5efa\u7acb\u5173\u8054\u5173\u7cfb\uff0c\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u5bf9\u901a\u7528\u7c7b\u522b\u7684\u5f15\u7528\u3002\u5b83\u80fd\u4e3a\u8bb8\u591a\u5e94\u7528\u5e26\u6765\u4fbf\u5229\uff0c\u5982\uff1a \u56fe\u7247\u3001\u97f3\u9891\u548c\u89c6\u9891\u6a21\u578b\u90fd\u53ef\u4ee5\u4e0e\u8bc4\u8bba\u6a21\u578b\u5efa\u7acb\u591a\u6001\u5173\u8054\uff0c\u4f7f\u8bc4\u8bba\u53ef\u4ee5\u5e94\u7528\u4e8e\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002 \u7528\u6237\u53ef\u4ee5\u4e0e\u8bc4\u8bba\u6a21\u578b\u5efa\u7acb\u591a\u6001\u5173\u8054\uff0c\u5e76\u88ab\u5e94\u7528\u4e8e\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6587\u7ae0\u3001\u56fe\u7247\u3001\u89c6\u9891\u7b49\u3002 \u8ba2\u5355\u6a21\u578b\u53ef\u4ee5\u4e0e\u6536\u8d27\u5730\u5740\u6a21\u578b\u5efa\u7acb\u591a\u6001\u5173\u8054\uff0c\u4f7f\u8ba2\u5355\u53ef\u4ee5\u914d\u9001\u5230\u591a\u79cd\u5730\u5740\u7c7b\u578b\uff0c\u5982\u5bb6\u5ead\u3001\u516c\u53f8\u3001\u7f51\u70b9\u7b49\u3002 \u4e8c\u3001\u5b9e\u73b0\u591a\u6001\u5173\u8054\u7684\u65b9\u6cd5 \u4e0b\u9762\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f7f\u7528 Laravel Eloquent \u5b9e\u73b0\u591a\u6001\u5173\u8054\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u7684\u662f\u6570\u636e\u8868\u7684\u8bbe\u8ba1\u3002\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u6a21\u578b\u4e4b\u95f4\u7684\u591a\u6001\u5173\u8054\u5173\u7cfb\u3002\u6b64\u8868\u5e94\u5305\u542b\u4ee5\u4e0b\u5217\uff1a id: \u8868\u4e3b\u952e ID\uff1b target_type: \u76ee\u6807\u6a21\u578b\u7684\u7c7b\u578b\u540d\u79f0\uff1b target_id: \u76ee\u6807\u6a21\u578b\u7684 ID\uff1b source_type: \u6e90\u6a21\u578b\u7684\u7c7b\u578b\u540d\u79f0\uff1b source_id: \u6e90\u6a21\u578b\u7684 ID\u3002 \u4e0b\u9762\u662f\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\u793a\u4f8b\uff1a &lt;?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateCommentsTable extends Migration { \/** * Run the migrations. * * @return void [&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-23400","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23400","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=23400"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23400\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}