{"id":48786,"date":"2024-12-02T11:59:59","date_gmt":"2024-12-02T03:59:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48786\/"},"modified":"2024-12-02T11:59:59","modified_gmt":"2024-12-02T03:59:59","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-thinkphp-orm-%e7%9a%84-withattr-%e6%89%b9%e9%87%8f%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48786\/","title":{"rendered":"Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\u529f\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\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\">\u300aLaravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\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\/20241031\/173035228167231499da68f.jpg\" class=\"aligncenter\" title=\"Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\u529f\u80fd\uff1f\u63d2\u56fe\" alt=\"Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\u529f\u80fd\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>laravel \u67e5\u8be2\u6784\u9020\u5668\u4e2d\u7684\u6279\u91cf\u6570\u636e\u96c6\u5408\u5904\u7406<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u5982\u4f55\u50cf thinkphp orm \u4e2d\u7684 withattr \u529f\u80fd\u4e00\u6837\uff0c\u5728 laravel \u67e5\u8be2\u6784\u9020\u5668\u4e2d\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>laravel \u67e5\u8be2\u6784\u9020\u5668\u7684\u786e\u6ca1\u6709\u7c7b\u4f3c\u4e8e thinkphp orm \u7684 withattr \u529f\u80fd\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\u6279\u91cf\u6570\u636e\u5904\u7406\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u884c\u5b9e\u73b0<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u6216 helper \u51fd\u6570\u6765\u5904\u7406\u6279\u91cf\u6570\u636e\u96c6\u5408\u3002<\/li>\n<li>\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre>namespace App\\Services;\n\nclass CollectionAttributesProcess\n{\n    public static function make(Collection $collection, array $attributes = [], $auto_times = true)\n    {\n        \/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\n        $collection = $collection-&gt;toArray();\n\n        foreach ($collection as &amp;$data) {\n            \/\/ \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\u5e76\u8fd4\u56de\n            $data = json_decode(json_encode($data), true);\n\n            \/\/ \u904d\u5386\u5c5e\u6027\u5217\u8868\n            foreach ($auto_times ? self::time_default_values($attributes) : $attributes as $field =&gt; $func) {\n                \/\/ \u5904\u7406\u6bcf\u4e2a\u5c5e\u6027\n                $data[$field] = $func($data[$field] ?? null, $data);\n            }\n        }\n\n        return $collection;\n    }\n\n    private static function time_default_values(array $attributes)\n    {\n        \/\/ \u6307\u5b9a\u65f6\u95f4\u5b57\u6bb5\u7684\u9ed8\u8ba4\u503c\n        foreach ([self::DEFAULT_CREATED_AT_FIELD, self::DEFAULT_UPDATED_AT_FIELD] as $_type) {\n            if (!isset($attributes[$_type])) {\n                $attributes[$_type] = self::$_type();\n            }\n        }\n\n        return $attributes;\n    }\n\n    private static function created_at()\n    {\n        \/\/ \u8fd4\u56de\u521b\u5efa\u65f6\u95f4\u5b57\u6bb5\u7684\u5904\u7406\u51fd\u6570\n        return function($created_at) {\n            return date(self::DEFAULT_TIME_TYPE, $created_at);\n        };\n    }\n\n    private static function updated_at()\n    {\n        \/\/ \u8fd4\u56de\u66f4\u65b0\u65f6\u95f4\u5b57\u6bb5\u7684\u5904\u7406\u51fd\u6570\n        return self::created_at();\n    }\n}<\/pre>\n<ol>\n<li>\n<p><strong>\u4fee\u6539\u5185\u7f6e\u7684 toarray \u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li>\u53ef\u4ee5\u4fee\u6539 toarray \u65b9\u6cd5\u4ee5\u6dfb\u52a0\u6279\u91cf\u6570\u636e\u5904\u7406\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u4eca\u5929\u5173\u4e8e\u300aLaravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\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>Laravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\u529f\u80fd\uff1f \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300aLaravel \u67e5\u8be2\u6784\u9020\u5668\u5982\u4f55\u5b9e\u73b0 ThinkPHP ORM \u7684 withAttr \u6279\u91cf\u6570\u636e\u5904\u7406\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 laravel \u67e5\u8be2\u6784\u9020\u5668\u4e2d\u7684\u6279\u91cf\u6570\u636e\u96c6\u5408\u5904\u7406 \u95ee\u9898\uff1a\u5982\u4f55\u50cf thinkphp orm \u4e2d\u7684 withattr \u529f\u80fd\u4e00\u6837\uff0c\u5728 laravel \u67e5\u8be2\u6784\u9020\u5668\u4e2d\u6279\u91cf\u5904\u7406\u6570\u636e\u96c6\u5408\uff1f \u7b54\u6848\uff1a laravel \u67e5\u8be2\u6784\u9020\u5668\u7684\u786e\u6ca1\u6709\u7c7b\u4f3c\u4e8e thinkphp orm \u7684 withattr \u529f\u80fd\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\u6279\u91cf\u6570\u636e\u5904\u7406\uff1a \u81ea\u884c\u5b9e\u73b0 \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u6216 helper \u51fd\u6570\u6765\u5904\u7406\u6279\u91cf\u6570\u636e\u96c6\u5408\u3002 \u4f8b\u5982\uff1a namespace App\\Services; class CollectionAttributesProcess { public static function make(Collection $collection, array $attributes = [], $auto_times = true) { [&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-48786","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48786","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=48786"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48786\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}