{"id":6711,"date":"2024-11-14T09:06:57","date_gmt":"2024-11-14T01:06:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6711\/"},"modified":"2024-11-14T09:06:57","modified_gmt":"2024-11-14T01:06:57","slug":"thinkphp6-with-%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e7%bb%93%e6%9e%9c%e5%a6%82%e4%bd%95%e6%89%81%e5%b9%b3%e5%8c%96%e4%b8%ba%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6711\/","title":{"rendered":"ThinkPHP6 with() \u5173\u8054\u67e5\u8be2\u7ed3\u679c\u5982\u4f55\u6241\u5e73\u5316\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172976091286989.jpg\" class=\"aligncenter\" title=\"ThinkPHP6 with() \u5173\u8054\u67e5\u8be2\u7ed3\u679c\u5982\u4f55\u6241\u5e73\u5316\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"ThinkPHP6 with() \u5173\u8054\u67e5\u8be2\u7ed3\u679c\u5982\u4f55\u6241\u5e73\u5316\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>6 \u6a21\u578b with() \u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 thinkphp6 \u4e2d\uff0c\u4f7f\u7528 with() \u65b9\u6cd5\u53ef\u4ee5\u5173\u8054\u591a\u4e2a\u8868\u8fdb\u884c\u67e5\u8be2\u3002\u4e0d\u8fc7\uff0c\u5f53\u67e5\u8be2\u7ed3\u679c\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u4e2a\u8868\u5b57\u6bb5\u522b\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u79cd\u901a\u8fc7\u6a21\u578b\u76f4\u63a5\u5f97\u5230\u4e00\u7ef4\u6570\u7ec4\u5e76\u89e3\u51b3\u522b\u540d\u51b2\u7a81\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a\u7ed1\u5b9a\u5c5e\u6027\u5230\u7236\u6a21\u578b<\/strong><\/p>\n<p>thinkphp6 \u63d0\u4f9b\u4e86\u5c06\u5173\u8054\u6a21\u578b\u5c5e\u6027\u7ed1\u5b9a\u5230\u7236\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u53ef\u4ee5\u89e3\u51b3\u522b\u540d\u51b2\u7a81\u95ee\u9898\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u5728 hasone\/hasmany\/belongsto \u65b9\u6cd5\u4e2d\u6307\u5b9a binding \u9009\u9879\uff0c\u5c06\u5173\u8054\u6a21\u578b\u7684\u5c5e\u6027\u7ed1\u5b9a\u5230\u7236\u6a21\u578b\u3002<\/li>\n<li>\u5728\u5173\u8054\u6a21\u578b\u4e2d\uff0c\u5c06\u9700\u8981\u7ed1\u5b9a\u7684\u5c5e\u6027\u6dfb\u52a0\u5230 attributenames \u4e2d\u3002<\/li>\n<li>\u5728\u67e5\u8be2\u4e2d\uff0c\u4f7f\u7528 with \u65b9\u6cd5\u5173\u8054\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u7ed1\u5b9a\u5c5e\u6027\u8fdb\u884c\u67e5\u8be2\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>public static function get_card_store_list()\n{\n    return self::with([\n        'profile' =&gt; function ($query) {\n            $query-&gt;binding('add_time');\n        }\n    ])\n        -&gt;select();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u67e5\u8be2\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>[\n    {\n        \"id\": 1,\n        \"times\": 10,\n        \"add_time\": 1626933885\n    }\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u7ed1\u5b9a\u5c5e\u6027\u5230\u7236\u6a21\u578b\uff0c\u53ef\u4ee5\u5c06\u5173\u8054\u6a21\u578b\u5c5e\u6027\u6241\u5e73\u5316\u5230\u7236\u6a21\u578b\u4e2d\uff0c\u4ece\u800c\u89e3\u51b3\u522b\u540d\u51b2\u7a81\u95ee\u9898\u5e76\u5f97\u5230\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6 with() \u5173\u8054\u67e5\u8be2\u7ed3\u679c\u5982\u4f55\u6241\u5e73\u5316\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>6 \u6a21\u578b with() \u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4 \u5728 thinkphp6 \u4e2d\uff0c\u4f7f\u7528 with() \u65b9\u6cd5\u53ef\u4ee5\u5173\u8054\u591a\u4e2a\u8868\u8fdb\u884c\u67e5\u8be2\u3002\u4e0d\u8fc7\uff0c\u5f53\u67e5\u8be2\u7ed3\u679c\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u4e2a\u8868\u5b57\u6bb5\u522b\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u79cd\u901a\u8fc7\u6a21\u578b\u76f4\u63a5\u5f97\u5230\u4e00\u7ef4\u6570\u7ec4\u5e76\u89e3\u51b3\u522b\u540d\u51b2\u7a81\u7684\u65b9\u6cd5\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u7ed1\u5b9a\u5c5e\u6027\u5230\u7236\u6a21\u578b thinkphp6 \u63d0\u4f9b\u4e86\u5c06\u5173\u8054\u6a21\u578b\u5c5e\u6027\u7ed1\u5b9a\u5230\u7236\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u53ef\u4ee5\u89e3\u51b3\u522b\u540d\u51b2\u7a81\u95ee\u9898\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728 hasone\/hasmany\/belongsto \u65b9\u6cd5\u4e2d\u6307\u5b9a binding \u9009\u9879\uff0c\u5c06\u5173\u8054\u6a21\u578b\u7684\u5c5e\u6027\u7ed1\u5b9a\u5230\u7236\u6a21\u578b\u3002 \u5728\u5173\u8054\u6a21\u578b\u4e2d\uff0c\u5c06\u9700\u8981\u7ed1\u5b9a\u7684\u5c5e\u6027\u6dfb\u52a0\u5230 attributenames \u4e2d\u3002 \u5728\u67e5\u8be2\u4e2d\uff0c\u4f7f\u7528 with \u65b9\u6cd5\u5173\u8054\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u7ed1\u5b9a\u5c5e\u6027\u8fdb\u884c\u67e5\u8be2\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a public static function get_card_store_list() { return self::with([ &#8216;profile&#8217; =&gt; function ($query) { $query-&gt;binding(&#8216;add_time&#8217;); } ]) -&gt;select(); } \u767b\u5f55\u540e\u590d\u5236 \u67e5\u8be2\u7ed3\u679c\uff1a [ { &#8220;id&#8221;: 1, &#8220;times&#8221;: 10, &#8220;add_time&#8221;: 1626933885 } ] \u767b\u5f55\u540e\u590d\u5236 [&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-6711","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6711","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=6711"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6711\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}