{"id":22553,"date":"2024-11-21T11:42:00","date_gmt":"2024-11-21T03:42:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22553\/"},"modified":"2024-11-21T11:42:00","modified_gmt":"2024-11-21T03:42:00","slug":"laravel-%e6%80%8e%e4%b9%88%e8%bf%9e%e8%a1%a8%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22553\/","title":{"rendered":"laravel \u600e\u4e48\u8fde\u8868\u67e5\u8be2"},"content":{"rendered":"<p>laravel \u662f\u4e00\u4e2a\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u6784\u5efa web \u5e94\u7528\u7a0b\u5e8f\u548c api\u3002\u5728\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9700\u8981\u8fdb\u884c\u8868\u4e4b\u95f4\u7684\u5173\u8054\u67e5\u8be2\uff0c\u4ee5\u4fbf\u4e8e\u83b7\u53d6\u66f4\u591a\u7684\u6570\u636e\u4fe1\u606f\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 laravel \u8fdb\u884c\u8fde\u8868\u67e5\u8be2\u3002<\/p>\n<ol>\n<li>\u57fa\u7840\u6a21\u578b\u7c7b<\/li>\n<\/ol>\n<p>\u5728 Laravel \u4e2d\uff0c\u6bcf\u4e2a\u5173\u7cfb\u90fd\u662f\u901a\u8fc7\u76f8\u5173\u6a21\u578b\u4e4b\u95f4\u7684\u65b9\u6cd5\u5efa\u7acb\u7684\u3002\u6211\u4eec\u9700\u8981\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49\u5173\u7cfb\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49 belongsTo \u548c hasMany \u5173\u7cfb\u65b9\u6cd5\u3002<\/p>\n<pre>class User extends Model\n{\n    \/**\n     * Get the post that belongs to the user.\n     *\/\n    public function post()\n    {\n        return $this-&gt;belongsTo(Post::class);\n    }\n}\n\nclass Post extends Model\n{\n    \/**\n     * Get the comments for the blog post.\n     *\/\n    public function comments()\n    {\n        return $this-&gt;hasMany(Comment::class);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 User \u6a21\u578b\u4e2d\uff0cbelongsTo \u65b9\u6cd5\u8868\u793a User \u6a21\u578b\u62e5\u6709\u4e00\u4e2a Post \u6a21\u578b\uff0c\u800c\u5728 Post \u6a21\u578b\u4e2d\uff0chasMany \u65b9\u6cd5\u8868\u793a Post \u6a21\u578b\u6709\u591a\u4e2a Comment \u6a21\u578b\u3002<\/p>\n<ol>\n<li>\u5173\u7cfb\u67e5\u8be2<\/li>\n<\/ol>\n<p>\u5728 Laravel \u4e2d\uff0c\u67e5\u8be2\u6784\u5efa\u5668\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u8fdb\u884c\u5173\u8054\u67e5\u8be2\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 with \u65b9\u6cd5\u83b7\u53d6\u5173\u8054\u6a21\u578b\u7684\u6570\u636e\u3002<\/p>\n<pre>$users = User::with('post')-&gt;get();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u83b7\u53d6\u6240\u6709 User \u6a21\u578b\uff0c\u5e76\u4f7f\u7528 with \u65b9\u6cd5\u9884\u52a0\u8f7d\u76f8\u5173\u7684 Post \u6a21\u578b\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u7528\u6237\u548c\u5e16\u5b50\u4e4b\u95f4\u5efa\u7acb\u5173\u7cfb\u4e86\u3002<\/p>\n<p>\u540c\u6837\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728 post \u548c comment \u4e4b\u95f4\u8fdb\u884c\u5173\u7cfb\u67e5\u8be2\u3002<\/p>\n<pre>$posts = Post::with('comments')-&gt;get();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u83b7\u53d6\u6240\u6709 Post \u6a21\u578b\uff0c\u5e76\u4f7f\u7528 with \u65b9\u6cd5\u9884\u52a0\u8f7d\u76f8\u5173\u7684 Comment \u6a21\u578b\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u8fdb\u4e00\u6b65\u8fc7\u6ee4\u67e5\u8be2\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u65b9\u6cd5\u4e2d\u4f20\u5165\u95ed\u5305\u51fd\u6570\u3002\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u6240\u6709\u5df2\u53d1\u5e03\u7684\u8bc4\u8bba\u3002<\/p>\n<pre>$comments = Comment::with(['post' =&gt; function ($query) {\n    $query-&gt;where('published', true);\n}])-&gt;get();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u83b7\u53d6\u6240\u6709 Comment \u6a21\u578b\uff0c\u5e76\u4f7f\u7528 with \u65b9\u6cd5\u9884\u52a0\u8f7d\u76f8\u5173\u7684 Post \u6a21\u578b\u3002\u5728 with \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\u3002\u6b64\u65f6\uff0c\u6570\u7ec4\u7684\u952e\u8868\u793a\u5173\u7cfb\u540d\u79f0\uff0c\u800c\u6570\u7ec4\u7684\u503c\u8868\u793a\u5f53\u524d\u5173\u7cfb\u7684\u67e5\u8be2\u95ed\u5305\u51fd\u6570\u3002<\/p>\n<ol>\n<li>\u81ea\u5b9a\u4e49\u5173\u7cfb\u67e5\u8be2<\/li>\n<\/ol>\n<p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u81ea\u5b9a\u4e49\u67e5\u8be2\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u7528\u6237\u7684\u89d2\u8272\u8fdb\u884c\u67e5\u8be2\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5173\u7cfb\u65b9\u6cd5\u3002<\/p>\n<pre>class User extends Model\n{\n    \/**\n     * Get the posts for the user by role.\n     *\/\n    public function postsByRole($role)\n    {\n        return $this-&gt;hasManyThrough(\n            'AppPost', \n            'AppCategory', \n            'user_id', \n            'category_id'\n        )-&gt;where('role', '=', $role);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 User \u6a21\u578b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a postsByRole \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528 hasManyThrough \u65b9\u6cd5\u5efa\u7acb Posts \u6a21\u578b\u548c Categories \u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5176\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a Posts \u6a21\u578b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a Categories \u6a21\u578b\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u8868\u793a\u53ef\u4ee5\u4ece\u4e2d\u83b7\u53d6 Posts \u6a21\u578b\u7684 User \u6a21\u578b\u7684\u5916\u952e\u540d\uff0c\u7b2c\u56db\u4e2a\u53c2\u6570\u8868\u793a\u53ef\u4ee5\u4ece\u4e2d\u83b7\u53d6 Categories \u6a21\u578b\u7684 Posts \u6a21\u578b\u7684\u5916\u952e\u540d\u3002<\/p>\n<ol>\n<li>\u591a\u5bf9\u591a\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u5728 Laravel \u4e2d\uff0c\u591a\u5bf9\u591a\u5173\u7cfb\u662f\u901a\u8fc7\u4e2d\u95f4\u8868\u5efa\u7acb\u7684\u3002\u5728\u6a21\u578b\u7c7b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49 belongsToMany \u5173\u7cfb\u65b9\u6cd5\u6765\u521b\u5efa\u591a\u5bf9\u591a\u5173\u7cfb\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5728 User \u6a21\u578b\u548c Role \u6a21\u578b\u4e4b\u95f4\u5efa\u7acb\u591a\u5bf9\u591a\u5173\u7cfb\u3002<\/p>\n<pre>class User extends Model\n{\n    \/**\n     * The roles that belong to the user.\n     *\/\n    public function roles()\n    {\n        return $this-&gt;belongsToMany(Role::class);\n    }\n}\n\nclass Role extends Model\n{\n    \/**\n     * The users that belong to the role.\n     *\/\n    public function users()\n    {\n        return $this-&gt;belongsToMany(User::class);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 User \u6a21\u578b\u4e2d\uff0cbelongsToMany \u65b9\u6cd5\u8868\u793a User \u6a21\u578b\u548c Role \u6a21\u578b\u4e4b\u95f4\u5efa\u7acb\u4e86\u591a\u5bf9\u591a\u5173\u7cfb\u3002\u540c\u6837\u5730\uff0c\u5728 Role \u6a21\u578b\u4e2d\uff0cbelongsToMany \u65b9\u6cd5\u8868\u793a Role \u6a21\u578b\u548c User \u6a21\u578b\u4e4b\u95f4\u5efa\u7acb\u4e86\u591a\u5bf9\u591a\u5173\u7cfb\u3002<\/p>\n<p>\u5173\u4e8e\u591a\u5bf9\u591a\u5173\u7cfb\u7684\u67e5\u8be2\uff0cLaravel \u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1awithCount\u3001has\u3001whereHas \u7b49\u3002<\/p>\n<ol>\n<li>\u7ed3\u8bba<\/li>\n<\/ol>\n<p>\u672c\u6587\u91cd\u70b9\u4ecb\u7ecd\u4e86\u5728 Laravel \u4e2d\u5982\u4f55\u8fdb\u884c\u8868\u4e4b\u95f4\u7684\u5173\u8054\u67e5\u8be2\uff0c\u5305\u62ec\u57fa\u7840\u6a21\u578b\u7c7b\u3001\u5173\u7cfb\u67e5\u8be2\u3001\u81ea\u5b9a\u4e49\u5173\u7cfb\u67e5\u8be2\u4ee5\u53ca\u591a\u5bf9\u591a\u5173\u7cfb\u67e5\u8be2\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u8bfb\u8005\u53ef\u4ee5\u638c\u63e1 Laravel \u8fde\u8868\u67e5\u8be2\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel \u600e\u4e48\u8fde\u8868\u67e5\u8be2\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>laravel \u662f\u4e00\u4e2a\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u6784\u5efa web \u5e94\u7528\u7a0b\u5e8f\u548c api\u3002\u5728\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9700\u8981\u8fdb\u884c\u8868\u4e4b\u95f4\u7684\u5173\u8054\u67e5\u8be2\uff0c\u4ee5\u4fbf\u4e8e\u83b7\u53d6\u66f4\u591a\u7684\u6570\u636e\u4fe1\u606f\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 laravel \u8fdb\u884c\u8fde\u8868\u67e5\u8be2\u3002 \u57fa\u7840\u6a21\u578b\u7c7b \u5728 Laravel \u4e2d\uff0c\u6bcf\u4e2a\u5173\u7cfb\u90fd\u662f\u901a\u8fc7\u76f8\u5173\u6a21\u578b\u4e4b\u95f4\u7684\u65b9\u6cd5\u5efa\u7acb\u7684\u3002\u6211\u4eec\u9700\u8981\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49\u5173\u7cfb\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49 belongsTo \u548c hasMany \u5173\u7cfb\u65b9\u6cd5\u3002 class User extends Model { \/** * Get the post that belongs to the user. *\/ public function post() { return $this-&gt;belongsTo(Post::class); } } class Post extends Model { \/** * Get the comments for the blog post. [&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-22553","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22553","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=22553"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22553\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}