{"id":22624,"date":"2024-11-21T12:16:45","date_gmt":"2024-11-21T04:16:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22624\/"},"modified":"2024-11-21T12:16:45","modified_gmt":"2024-11-21T04:16:45","slug":"laravel%e4%b8%89%e5%bc%a0%e8%a1%a8%e4%b8%80%e5%af%b9%e5%a4%9a%e6%80%8e%e4%b9%88%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22624\/","title":{"rendered":"laravel\u4e09\u5f20\u8868\u4e00\u5bf9\u591a\u600e\u4e48\u67e5"},"content":{"rendered":"<p>\u5728laravel\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u5173\u7cfb\u578b\u6570\u636e\u5e93\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\uff0c\u800c\u4e00\u5bf9\u591a\u5173\u7cfb\u662f\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u3002\u4e00\u5bf9\u591a\u5173\u7cfb\u6307\u7684\u662f\u4e00\u4e2a\u6a21\u578b\u62e5\u6709\u591a\u4e2a\u5173\u8054\u6a21\u578b\u7684\u5173\u7cfb\uff0c\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u4e00\u4e9b\u5e38\u89c1\u7684\u4e1a\u52a1\u573a\u666f\uff0c\u6bd4\u5982\u4e00\u4e2a\u7528\u6237\u5bf9\u5e94\u591a\u4e2a\u8ba2\u5355\u6216\u8005\u4e00\u4e2a\u8bfe\u7a0b\u5bf9\u5e94\u591a\u4e2a\u5b66\u751f\u7b49\u3002<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent ORM\u6765\u5b9e\u73b0\u4e00\u5bf9\u591a\u5173\u7cfb\u67e5\u8be2\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u4e2d\u7684Eloquent\u6a21\u578b\u67e5\u8be2\u4e09\u5f20\u8868\u4e00\u5bf9\u591a\u5173\u7cfb\u3002<\/p>\n<ol>\n<li>\u8bbe\u8ba1\u4e09\u5f20\u8868<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e09\u5f20\u8868\uff0c\u5206\u522b\u662f\u4e3b\u8868\u3001\u5b50\u8868\u548c\u5b50\u8868\u5916\u952e\u5173\u8054\u8868\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u8868\u7684\u8bbe\u8ba1\uff1a<\/p>\n<p>\u4e3b\u8868\uff1ausers<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>email<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>Tom<\/td>\n<td>tom@laravel.com<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Jerry<\/td>\n<td>jerry@laravel.com<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Bob<\/td>\n<td>bob@laravel.com<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b50\u8868\uff1aorders<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>order_no<\/th>\n<th>user_id<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1001<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>1002<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>1003<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1004<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b50\u8868\u5916\u952e\u5173\u8054\u8868\uff1aorder_items<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>order_id<\/th>\n<th>product_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>apple<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>1<\/td>\n<td>banana<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2<\/td>\n<td>pear<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>4<\/td>\n<td>orange<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>4<\/td>\n<td>grape<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol>\n<li>\u521b\u5efaLaravel\u6a21\u578b<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e09\u4e2a\u6a21\u578b\uff0c\u5206\u522b\u5bf9\u5e94\u4e09\u5f20\u8868\u3002\u5728\u521b\u5efa\u6a21\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<p>\u4e3b\u8868\u6a21\u578b\uff1aUser<\/p>\n<pre>class User extends Model\n{\n    public function orders()\n    {\n        return $this-&amp;gt;hasMany(Order::class);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b50\u8868\u6a21\u578b\uff1aOrder<\/p>\n<pre>class Order extends Model\n{\n    public function user()\n    {\n        return $this-&amp;gt;belongsTo(User::class);\n    }\n\n    public function items()\n    {\n        return $this-&amp;gt;hasMany(OrderItem::class);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b50\u8868\u5916\u952e\u5173\u8054\u8868\u6a21\u578b\uff1aOrderItem<\/p>\n<pre>class OrderItem extends Model\n{\n    public function order()\n    {\n        return $this-&amp;gt;belongsTo(Order::class);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u67e5\u8be2\u4e00\u5bf9\u591a\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent ORM\u6765\u5b9e\u73b0\u4e00\u5bf9\u591a\u5173\u7cfb\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u67e5\u8be2\u4e00\u4f4d\u7528\u6237\u7684\u6240\u6709\u8ba2\u5355\u53ca\u5176\u4e0b\u7684\u6240\u6709\u8ba2\u5355\u9879\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>$user = User::find(1);\n\nforeach ($user-&amp;gt;orders as $order) {\n    echo $order-&amp;gt;order_no;\n\n    foreach ($order-&amp;gt;items as $item) {\n        echo $item-&amp;gt;product_name;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u8f93\u51fa\u7528\u6237Tom\u7684\u6240\u6709\u8ba2\u5355\u53ca\u5176\u4e0b\u7684\u6240\u6709\u8ba2\u5355\u9879\u3002<\/p>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Eloquent\u7684\u5173\u8054\u9884\u8f7d\u5165\u65b9\u6cd5\u6765\u67e5\u8be2\u4e00\u4f4d\u7528\u6237\u53ca\u5176\u8ba2\u5355\u53ca\u5176\u4e0b\u6240\u6709\u8ba2\u5355\u9879\uff1a<\/p>\n<pre>$user = User::with('orders.items')-&amp;gt;find(1);\n\nforeach ($user-&amp;gt;orders as $order) {\n    echo $order-&amp;gt;order_no;\n\n    foreach ($order-&amp;gt;items as $item) {\n        echo $item-&amp;gt;product_name;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u8f93\u51fa\u7528\u6237Tom\u7684\u6240\u6709\u8ba2\u5355\u53ca\u5176\u4e0b\u7684\u6240\u6709\u8ba2\u5355\u9879\uff0c\u4e0e\u4e0a\u9762\u7684\u65b9\u5f0f\u76f8\u6bd4\uff0c\u8be5\u65b9\u5f0f\u6709\u66f4\u597d\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent ORM\u975e\u5e38\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e00\u5bf9\u591a\u5173\u7cfb\u67e5\u8be2\u3002\u53ea\u9700\u5728\u6a21\u578b\u4e2d\u5b9a\u4e49\u597d\u5173\u8054\u5173\u7cfb\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u5230\u6307\u5b9a\u6a21\u578b\u7684\u6240\u6709\u5173\u8054\u6570\u636e\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5173\u8054\u9884\u8f7d\u5165\u65b9\u6cd5\u6765\u4f18\u5316\u67e5\u8be2\u6027\u80fd\uff0c\u8ba9\u6211\u4eec\u7684\u5e94\u7528\u66f4\u52a0\u9ad8\u6548\u7a33\u5b9a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u4e09\u5f20\u8868\u4e00\u5bf9\u591a\u600e\u4e48\u67e5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728laravel\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u5173\u7cfb\u578b\u6570\u636e\u5e93\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\uff0c\u800c\u4e00\u5bf9\u591a\u5173\u7cfb\u662f\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u3002\u4e00\u5bf9\u591a\u5173\u7cfb\u6307\u7684\u662f\u4e00\u4e2a\u6a21\u578b\u62e5\u6709\u591a\u4e2a\u5173\u8054\u6a21\u578b\u7684\u5173\u7cfb\uff0c\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u4e00\u4e9b\u5e38\u89c1\u7684\u4e1a\u52a1\u573a\u666f\uff0c\u6bd4\u5982\u4e00\u4e2a\u7528\u6237\u5bf9\u5e94\u591a\u4e2a\u8ba2\u5355\u6216\u8005\u4e00\u4e2a\u8bfe\u7a0b\u5bf9\u5e94\u591a\u4e2a\u5b66\u751f\u7b49\u3002 \u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent ORM\u6765\u5b9e\u73b0\u4e00\u5bf9\u591a\u5173\u7cfb\u67e5\u8be2\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u4e2d\u7684Eloquent\u6a21\u578b\u67e5\u8be2\u4e09\u5f20\u8868\u4e00\u5bf9\u591a\u5173\u7cfb\u3002 \u8bbe\u8ba1\u4e09\u5f20\u8868 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e09\u5f20\u8868\uff0c\u5206\u522b\u662f\u4e3b\u8868\u3001\u5b50\u8868\u548c\u5b50\u8868\u5916\u952e\u5173\u8054\u8868\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u8868\u7684\u8bbe\u8ba1\uff1a \u4e3b\u8868\uff1ausers id name email 1 Tom tom@laravel.com 2 Jerry jerry@laravel.com 3 Bob bob@laravel.com \u5b50\u8868\uff1aorders id order_no user_id 1 1001 1 2 1002 2 3 1003 3 4 1004 1 \u5b50\u8868\u5916\u952e\u5173\u8054\u8868\uff1aorder_items id order_id product_name 1 1 apple 2 1 banana 3 2 pear 4 4 orange 5 4 grape \u521b\u5efaLaravel\u6a21\u578b \u5728Laravel\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e09\u4e2a\u6a21\u578b\uff0c\u5206\u522b\u5bf9\u5e94\u4e09\u5f20\u8868\u3002\u5728\u521b\u5efa\u6a21\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\u3002 [&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-22624","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22624","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=22624"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22624\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}