{"id":48633,"date":"2024-12-02T16:23:16","date_gmt":"2024-12-02T08:23:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48633\/"},"modified":"2024-12-02T16:23:16","modified_gmt":"2024-12-02T08:23:16","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-laravel-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e7%b1%bb%e4%bc%bc-thinkphp-withattr-%e7%9a%84%e6%89%b9%e9%87%8f%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e5%8a%9f%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48633\/","title":{"rendered":"\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/1730176072672064483ca13.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 laravel \u4e2d\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408<\/strong><\/p>\n<p>\u5728 php \u6846\u67b6 laravel \u4e2d\uff0c\u67e5\u8be2\u6784\u9020\u5668\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\u96c6\u5408\u3002\u4f46\u5b83\u7f3a\u4e4f\u7c7b\u4f3c\u4e8e thinkphp \u4e2d withattr \u65b9\u6cd5\u7684\u529f\u80fd\uff0c\u65e0\u6cd5\u5bf9\u6570\u636e\u96c6\u5408\u7684\u7279\u5b9a\u5b57\u6bb5\u8fdb\u884c\u6279\u91cf\u8f6c\u6362\u3002<\/p>\n<p><strong>\u81ea\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u81ea\u884c\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u53ef\u5c06 status \u5b57\u6bb5\u4ece\u6570\u503c\u8f6c\u6362\u4e3a\u53ef\u8bfb\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>&lt;?php\n\nnamespace app\\helpers;\n\nuse illuminate\\support\\collection;\n\nclass datacollectionhelper\n{\n    public static function withattr(collection $collection, array $attributes)\n    {\n        foreach ($collection as $item) {\n            foreach ($attributes as $field =&gt; $callback) {\n                $item-&gt;{$field} = $callback($item-&gt;{$field}, $item);\n            }\n        }\n\n        return $collection;\n    }\n}<\/pre>\n<p>\u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u50cf\u8fd9\u6837\u5bf9\u8ba2\u5355\u6570\u636e\u96c6\u5408\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<pre>$orders = datacollectionhelper::withattr($orders, [\n    'status' =&gt; function($status) {\n        return ['\u5f85\u4ed8\u6b3e', '\u5f85\u53d1\u8d27'][$status];\n    }\n]);<\/pre>\n<p><strong>\u4fee\u6539\u5185\u7f6e\u65b9\u6cd5<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4fee\u6539 laravel \u5185\u7f6e\u7684 toarray \u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6a21\u578b\u6216\u670d\u52a1\u63d0\u4f9b\u8005\uff0c\u5e76\u8986\u5199 toarray \u65b9\u6cd5\uff0c\u5728\u5176\u4e2d\u6dfb\u52a0\u6240\u9700\u7684\u8f6c\u6362\u3002<\/p>\n<pre>&lt;?php\n\nnamespace app\\services;\n\nuse illuminate\\database\\eloquent\\model;\nuse illuminate\\database\\eloquent\\collection;\n\nclass dataprocessservice extends model\n{\n    protected static function boot()\n    {\n        parent::boot();\n\n        \/\/ \u8986\u5199 toarray \u65b9\u6cd5\n        static::observe(new dataprocessobserver);\n    }\n}<\/pre>\n<p><strong>\u89c2\u5bdf\u8005<\/strong><\/p>\n<pre>&lt;?php\n\nnamespace App\\Services;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Support\\Collection;\n\nclass DataProcessObserver\n{\n    public function updating(Model $model)\n    {\n        if ($model instanceof Collection) {\n            $model-&gt;transform(function ($item) {\n                $item-&gt;status = ['\u5f85\u4ed8\u6b3e', '\u5f85\u53d1\u8d27'][$item-&gt;status];\n                return $item;\n            });\n        }\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5728 Laravel \u4e2d\u5b9e\u73b0\u7c7b\u4f3c ThinkPHP withAttr \u7684\u6279\u91cf\u6570\u636e\u8f6c\u6362\u529f\u80fd\uff1f\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 \u5728 laravel \u4e2d\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408 \u5728 php \u6846\u67b6 laravel \u4e2d\uff0c\u67e5\u8be2\u6784\u9020\u5668\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\u96c6\u5408\u3002\u4f46\u5b83\u7f3a\u4e4f\u7c7b\u4f3c\u4e8e thinkphp \u4e2d withattr \u65b9\u6cd5\u7684\u529f\u80fd\uff0c\u65e0\u6cd5\u5bf9\u6570\u636e\u96c6\u5408\u7684\u7279\u5b9a\u5b57\u6bb5\u8fdb\u884c\u6279\u91cf\u8f6c\u6362\u3002 \u81ea\u5b9e\u73b0\u65b9\u6cd5 \u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u81ea\u884c\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u53ef\u5c06 status \u5b57\u6bb5\u4ece\u6570\u503c\u8f6c\u6362\u4e3a\u53ef\u8bfb\u5b57\u7b26\u4e32\uff1a &lt;?php namespace app\\helpers; use illuminate\\support\\collection; class datacollectionhelper { public static function withattr(collection $collection, array $attributes) { foreach ($collection as $item) { foreach ($attributes as $field =&gt; $callback) [&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-48633","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48633","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=48633"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48633\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}