{"id":317,"date":"2020-05-31T20:49:39","date_gmt":"2020-05-31T12:49:39","guid":{"rendered":"https:\/\/wp.dj47.top\/?p=317"},"modified":"2021-10-20T16:16:17","modified_gmt":"2021-10-20T08:16:17","slug":"thinkphp5-with%e7%9a%84%e5%b8%b8%e7%94%a8%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=317","title":{"rendered":"thinkphp5 with\u7684\u5e38\u7528\u5199\u6cd5"},"content":{"rendered":"<h2 id=\"\u4e3a\u4f55\u4f7f\u7528with\" style=\"box-sizing: border-box; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-weight: 500; line-height: 32px; color: #4f4f4f; margin: 8px 0px 16px; font-size: 24px; outline: 0px; padding: 0px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u4e3a\u4f55\u4f7f\u7528with<\/h2>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"box-sizing: border-box; color: #999999; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures; background-color: #eef0f4;\">\u5173\u8054\u67e5\u8be2\u7684\u9884\u67e5\u8be2\u8f7d\u5165\u529f\u80fd\uff0c\u4e3b\u8981\u89e3\u51b3\u4e86N+1\u6b21\u67e5\u8be2\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u67e5\u8be2\u5982\u679c\u67093\u4e2a\u8bb0\u5f55\uff0c\u4f1a\u6267\u884c4\u6b21\u67e5\u8be2\uff1a<\/span><\/p>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"box-sizing: border-box; color: #999999; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures; background-color: #eef0f4;\"><span style=\"box-sizing: border-box; color: #4d4d4d; background-color: #ffffff;\">\u4e3e\u4e2a\u6817\u5b50\uff1a<\/span><\/span><\/p>\n<pre class=\"language-php\"><code>$list = User::all([1,2,3]);\r\nforeach($list as $user){    \r\n    \/\/ \u83b7\u53d6\u7528\u6237\u5173\u8054\u7684 profile \u6a21\u578b\u6570\u636e\r\n    dump($user-&gt;profile);\r\n}<\/code><\/pre>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"background-color: #eef0f4; color: #999999; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\">\u5982\u679c\u4f7f\u7528\u5173\u8054\u9884\u67e5\u8be2\u529f\u80fd\uff0c\u5bf9\u4e8e\u4e00\u5bf9\u4e00\u5173\u8054\u6765\u8bf4\uff0c\u53ea\u6709\u4e00\u6b21\u67e5\u8be2\uff0c\u5bf9\u4e8e\u4e00\u5bf9\u591a\u5173\u8054\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u53d8\u62102\u6b21\u67e5\u8be2\uff0c\u6709\u6548\u63d0\u9ad8\u6027\u80fd\u3002<\/span><\/p>\n<pre class=\"language-php\"><code>$list = User::with('profile')-&gt;select([1,2,3]);\r\nforeach($list as $user){    \r\n    \/\/ \u83b7\u53d6\u7528\u6237\u5173\u8054\u7684 profile \u6a21\u578b\u6570\u636e\r\n    dump($user-&gt;profile);\r\n}<\/code><\/pre>\n<h3 id=\"\u5355\u4e2a\u9884\u8f7d\u5165\" style=\"box-sizing: border-box; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-weight: 500; line-height: 30px; color: #4f4f4f; margin: 8px 0px 16px; font-size: 22px; outline: 0px; padding: 0px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u5355\u4e2a\u9884\u8f7d\u5165<\/h3>\n<pre class=\"language-php\"><code>$list = User::with('profile')-&gt;select([1,2,3]);<\/code><\/pre>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"box-sizing: border-box; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\">\u8fd9\u91cc\u8f7d\u5165\u7684profile \u6700\u540e\u67e5\u8be2\u5b57\u6bb5\u7531\u4f60\u914d\u7f6e\u5173\u8054\u5173\u7cfb\u65f6\u5019\u8bbe\u7f6e\u7684filed\u6765\u5b9a\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u914d\u7f6e\u5173\u8054\u5173\u7cfb\u65f6\u4e0d\u4f7f\u7528filed\u6765\u6307\u5b9a\uff0c\u5728with\u65f6\u6307\u5b9a\u7684\u8bdd\uff0c\u9700\u8981\u4f7f\u7528\u5230\u95ed\u5305\u51fd\u6570\u7684\u65b9\u5f0f\u6307\u5b9a<\/span><br style=\"box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; overflow-wrap: break-word; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\" \/><span style=\"box-sizing: border-box; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\">\u4e3e\u4e2a\u6817\u5b50\uff1a<\/span><\/p>\n<pre class=\"language-php\"><code>$list = User::with('profile' =&gt;  function($query){\r\n            $query-&gt;withField('id, name, status');\r\n        })-&gt;select([1,2,3]);<\/code><\/pre>\n<h3 id=\"\u591a\u4e2a\u5173\u8054\u6a21\u578b\u9884\u8f7d\u5165\" style=\"box-sizing: border-box; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-weight: 500; line-height: 30px; color: #4f4f4f; margin: 8px 0px 16px; font-size: 22px; outline: 0px; padding: 0px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u591a\u4e2a\u5173\u8054\u6a21\u578b\u9884\u8f7d\u5165<\/h3>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"box-sizing: border-box; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\">\u5728\u67e5\u8be2\u4e2d\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u9884\u8f7d\u5165\u591a\u4e2a\u5173\u8054\u6a21\u578b\u7684\u60c5\u51b5\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/span><\/p>\n<pre class=\"language-php\"><code>$list = User::with(['profile', 'info', 'order'])-&gt;select([1,2,3]);<\/code><\/pre>\n<h3 id=\"\u591a\u5c42\u5173\u8054\u5173\u7cfb\u8f7d\u5165\" style=\"box-sizing: border-box; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-weight: 500; line-height: 30px; color: #4f4f4f; margin: 8px 0px 16px; font-size: 22px; outline: 0px; padding: 0px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u591a\u5c42\u5173\u8054\u5173\u7cfb\u8f7d\u5165<\/h3>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"box-sizing: border-box; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\">\u5728\u9884\u8f7d\u5165\u4e2d\uff0c\u6700\u590d\u6742\u7684\u5c5e\u4e8e\u591a\u5c42\u9884\u8f7d\u5165\u4e86\uff0c\u4f8b\u5982\uff1auser\u7684order\u7684product\u7684brand<\/span><br style=\"box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; overflow-wrap: break-word; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\" \/><span style=\"box-sizing: border-box; color: #4d4d4d; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures;\">\u8fd9\u4e2a\u5982\u679c\u9700\u8981\u5728user\u67e5\u8be2\u65f6\u9884\u8f7d\u5165\uff0c\u5199\u6cd5\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"language-php\"><code>$list = User::with(['profile', 'info', 'order' =&gt; ['product' =&gt; ['brand]]])-&gt;select([1,2,3]);<\/code><\/pre>\n<p style=\"box-sizing: border-box; margin: 0px 0px 16px; font-size: 14px; outline: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; color: #4d4d4d; line-height: 26px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u5176\u5b9e\u4e5f\u7b80\u5355\uff0c\u5c31\u662f\u4f7f\u7528\u6570\u7ec4\u5d4c\u5957\u65b9\u5f0f\u53bb\u5d4c\u5957\u5c31\u597d\u4e86\u3002<\/p>\n<h3 id=\"\u6700\u590d\u6742\u5199\u6cd5\" style=\"box-sizing: border-box; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-weight: 500; line-height: 30px; color: #4f4f4f; margin: 8px 0px 16px; font-size: 22px; outline: 0px; padding: 0px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u6700\u590d\u6742\u5199\u6cd5<\/h3>\n<p style=\"box-sizing: border-box; margin: 0px 0px 16px; font-size: 14px; outline: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; color: #4d4d4d; line-height: 26px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; background-color: #ffffff;\">\u5c31\u662f\u5728with \u5173\u8054\u6a21\u578b\u65f6\uff0c\u4f7f\u7528\u4e86\u95ed\u5305\u67e5\u8be2\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u8be5\u6a21\u578b\u4e0b\u7684\u5176\u4ed6\u5173\u8054\u6a21\u578b\uff0c\u8fd9\u79cd\u5199\u6cd5\u6700\u590d\u6742\uff0c\u76f4\u63a5\u4e0a\u6817\u5b50\uff1a<\/p>\n<pre class=\"language-php\"><code>$list = User::with('profile' =&gt;  function($query){\r\n            $query-&gt;withField('id, name, status')-&gt;with('test');\r\n        })-&gt;select([1,2,3]);<\/code><\/pre>\n<p style=\"box-sizing: border-box; margin: 0px 0px 10px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\"><span style=\"box-sizing: border-box; color: #999999; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-variant-ligatures: common-ligatures; background-color: #eef0f4;\">\u6ce8\u610f\u95ed\u5305\u91cc\u9762\u7684with\uff0c\u56e0\u4e3a\u8fd9\u4e2aquery \u662f\u6574\u4e2aprofile \u5bf9\u8c61\uff0c\u6240\u4ee5\u95ed\u5305\u91cc\u9762\u5c31\u8ddf\u5355\u72ec\u67e5\u8be2profile \u4e00\u6837\uff0c\u8be5withField\u5c31 withField\uff0c\u8be5with \u5c31with\uff0c\u8fd8\u53ef\u4ee5\u5176\u4ed6where \u7b49\u64cd\u4f5c\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4f55\u4f7f\u7528with \u5173\u8054\u67e5\u8be2\u7684\u9884\u67e5\u8be2\u8f7d\u5165\u529f\u80fd\uff0c\u4e3b\u8981\u89e3\u51b3\u4e86N+1\u6b21\u67e5\u8be2\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u67e5\u8be2\u5982\u679c\u67093\u4e2a\u8bb0\u5f55\uff0c\u4f1a\u6267\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[44],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-thinkphp","tag-tp5"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}