{"id":49082,"date":"2024-12-02T12:24:27","date_gmt":"2024-12-02T04:24:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49082\/"},"modified":"2024-12-02T12:24:27","modified_gmt":"2024-12-02T04:24:27","slug":"laravel-%e6%9f%a5%e8%af%a2%e6%9e%84%e9%80%a0%e5%99%a8%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%b1%bb%e4%bc%bc-think-orm-%e7%9a%84-withattr-%e6%89%b9%e9%87%8f%e5%a4%84%e7%90%86%e6%95%b0%e6%8d%ae%e9%9b%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49082\/","title":{"rendered":"Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aLaravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\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~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731074795672e1aeb76c69.jpg\" class=\"aligncenter\" title=\"Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f\u63d2\u56fe\" alt=\"Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>laravel \u67e5\u8be2\u6784\u9020\u5668\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408<\/strong><\/p>\n<p>\u7c7b\u4f3c\u4e8e think-orm \u7684 withattr \u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u67e5\u8be2\u6784\u9020\u5668\u4e2d\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>laravel \u4e2d\u5e76\u4e0d\u76f4\u63a5\u63d0\u4f9b\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5185\u7f6e\u7684 toarray \u65b9\u6cd5\u6216\u81ea\u5df1\u5b9e\u73b0\u6765\u8fbe\u5230\u76ee\u7684\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49 toarray \u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 service \u6216 helper \u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49 toarray \u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace app\\services;\n\nclass dataloader\n{\n    public static function convertstatus($value)\n    {\n        \/\/ \u8fd4\u56de\u8f6c\u6362\u540e\u7684\u503c\n    }\n\n    public static function toarray($query, $attributes = [])\n    {\n        $data = $query-&gt;get()-&gt;toarray();\n\n        foreach ($data as $key =&gt; &amp;$value) {\n            foreach ($attributes as $field =&gt; $callback) {\n                $value[$field] = $callback($value[$field]);\n            }\n        }\n\n        return $data;\n    }\n}<\/pre>\n<p><strong>\u81ea\u5df1\u5b9e\u73b0<\/strong><\/p>\n<pre>foreach ($orders as &amp;$order) {\n    $order['status'] = ['\u5f85\u4ed8\u6b3e', '\u5f85\u53d1\u8d27'][$order['status']];\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aLaravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aLaravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c Think-ORM \u7684 withAttr \u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u529f\u80fd\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\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~ laravel \u67e5\u8be2\u6784\u9020\u5668\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408 \u7c7b\u4f3c\u4e8e think-orm \u7684 withattr \u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u67e5\u8be2\u6784\u9020\u5668\u4e2d\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\u3002 \u89e3\u51b3\u65b9\u6848 laravel \u4e2d\u5e76\u4e0d\u76f4\u63a5\u63d0\u4f9b\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5185\u7f6e\u7684 toarray \u65b9\u6cd5\u6216\u81ea\u5df1\u5b9e\u73b0\u6765\u8fbe\u5230\u76ee\u7684\u3002 \u81ea\u5b9a\u4e49 toarray \u65b9\u6cd5 \u53ef\u4ee5\u4f7f\u7528 service \u6216 helper \u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49 toarray \u65b9\u6cd5\u3002\u4f8b\u5982\uff1a namespace app\\services; class dataloader { public static function convertstatus($value) { \/\/ \u8fd4\u56de\u8f6c\u6362\u540e\u7684\u503c } public static function toarray($query, $attributes = []) { [&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-49082","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49082","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=49082"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49082\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}