{"id":548,"date":"2021-07-16T13:29:14","date_gmt":"2021-07-16T05:29:14","guid":{"rendered":"http:\/\/log.tanxiaodian.com\/?p=548"},"modified":"2021-07-16T13:29:14","modified_gmt":"2021-07-16T05:29:14","slug":"laravel%e7%9a%84%e6%95%b0%e6%8d%ae%e6%a8%a1%e5%9e%8b%e5%85%b3%e8%81%94%e6%97%b6%e7%9a%84%e6%b3%a8%e6%84%8f","status":"publish","type":"post","link":"https:\/\/log.tanxiaodian.com\/?p=548","title":{"rendered":"laravel\u7684\u6570\u636e\u6a21\u578b\u5173\u8054\u65f6\u7684\u6ce8\u610f"},"content":{"rendered":"\n<p>\u8bed\u6cd5\u683c\u5f0f:<\/p>\n\n\n\n<p>\u5728\u6a21\u578b\u4e2d:<\/p>\n\n\n\n<p>1\u5bf91    hasOne(&#8216;\u7c7b&#8217;,&#8217;\u5916\u952e&#8217;,&#8217;\u4e3b\u952e&#8217;)<\/p>\n\n\n\n<p>1\u5bf9\u591a hasMany(&#8216;\u7c7b&#8217;,&#8217;\u5916\u952e&#8217;,&#8217;\u4e3b\u952e&#8217;)<\/p>\n\n\n\n<p>\u53cd\u5411\u5173\u8054\u4f7f\u7528belongTo(&#8216;\u7c7b&#8217;,&#8217;\u5916\u952e&#8217;,&#8217;\u4e3b\u952e&#8217;)<\/p>\n\n\n\n<p>\u53cd\u5411\u5173\u8054\u5982\u5546\u54c1\u548c\u7c7b\u578b\u7684\u5173\u7cfb   \u624b\u673a\u53f7\u548c\u73a9\u5bb6\u7684\u5173\u7cfb\u7b49<\/p>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a \u5b9a\u4e49\u7684\u683c\u5f0f\u5982:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public function mytype()<br>{<br>    return $this-&gt;belongsTo('App\\Models\\goods\\GoodsType', 'type' , 'id');<br>}<\/pre>\n\n\n\n<p>\u65b9\u6cd5\u540d\u4e3a\u540e\u7eed\u7ed3\u679c\u96c6\u4e2d\u7684\u7c7b\u578b\u540d,\u9a7c\u5cf0\u547d\u540d\u65e0\u6548<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u5206\u9875paginate  \u5bf9\u8c61 \u9700\u8981\u5bf9attribute\u8fdb\u884c\u5904\u7406 \u5982:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$mData = self::<em>paginate<\/em>($limit);\nforeach ($mData as $item) {\n    $item->attributes['goodtype'] = $item->mytype->name;\n    $item->attributes['brand'] = $item->mybrand->name;\n    $item->attributes['card'] = $item->mycard->name;\n    $item->attributes['rarity'] = $item->myrarity->name;\n}\n\n<\/pre>\n\n\n\n<p><span class=\"has-inline-color has-accent-color\">\u65b9\u6cd5\u540d\u6700\u597d\u4e0d\u8981\u4e0e \u65b0\u7684\u5c5e\u6027\u540d\u76f8\u540c\uff0cname\u5b57\u6bb5\u7279\u4f8b<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bed\u6cd5\u683c\u5f0f: \u5728\u6a21\u578b\u4e2d: 1\u5bf91 hasOne(&#038;#&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/log.tanxiaodian.com\/?p=548\">Continue reading<span class=\"screen-reader-text\">laravel\u7684\u6570\u636e\u6a21\u578b\u5173\u8054\u65f6\u7684\u6ce8\u610f<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[64,68],"_links":{"self":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts\/548"}],"collection":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=548"}],"version-history":[{"count":1,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions\/549"}],"wp:attachment":[{"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/log.tanxiaodian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}