{"id":294,"date":"2017-08-06T12:44:32","date_gmt":"2017-08-06T04:44:32","guid":{"rendered":"http:\/\/www.cxybj.com\/?p=294"},"modified":"2017-08-06T13:18:18","modified_gmt":"2017-08-06T05:18:18","slug":"thinkphp5-%e6%a8%a1%e5%9e%8b%e6%96%b9%e6%b3%95%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/www.cxybj.com\/?p=294","title":{"rendered":"thinkphp5 \u6570\u636e\u5e93\u64cd\u4f5c\u6a21\u578b\u65b9\u6cd5\u96c6\u5408"},"content":{"rendered":"<p>get\u6216\u8005find\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u5f53\u524d\u6a21\u578b\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u578b\u7684\u65b9\u6cd5\u3002<\/p>\n<p>save\u65b9\u6cd5\u65b0\u589e\u6570\u636e\u8fd4\u56de\u7684\u662f\u5199\u5165\u7684\u8bb0\u5f55\u6570\u3002<\/p>\n<p>saveAll\u65b9\u6cd5\u65b0\u589e\u6570\u636e\u8fd4\u56de\u7684\u662f\u5305\u542b\u65b0\u589e\u6a21\u578b\uff08\u5e26\u81ea\u589eID\uff09\u7684\u6570\u636e\u96c6\uff08\u6570\u7ec4\uff09\u3002<\/p>\n<p>saveAll\u65b9\u6cd5\u65b0\u589e\u6570\u636e\u9ed8\u8ba4\u4f1a\u81ea\u52a8\u8bc6\u522b\u6570\u636e\u662f\u9700\u8981\u65b0\u589e\u8fd8\u662f\u66f4\u65b0\u64cd\u4f5c\uff0c\u5f53\u6570\u636e\u4e2d\u5b58\u5728\u4e3b\u952e\u7684\u65f6\u5019\u4f1a\u8ba4\u4e3a\u662f\u66f4\u65b0\u64cd\u4f5c\uff0c\u5982\u679c\u4f60\u9700\u8981\u5e26\u4e3b\u952e\u6570\u636e\u6279\u91cf\u65b0\u589e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre>$user = new User;\r\n$list = [\r\n    ['id'=&gt;1, 'name'=&gt;'thinkphp', 'email'=&gt;'thinkphp@qq.com'],\r\n    ['id'=&gt;2, 'name'=&gt;'onethink', 'email'=&gt;'onethink@qq.com'],\r\n];\r\n$user-&gt;saveAll($list, false);<\/pre>\n<p>\u8fd8\u53ef\u4ee5\u76f4\u63a5\u9759\u6001\u8c03\u7528create\u65b9\u6cd5\u521b\u5efa\u5e76\u5199\u5165\uff1a<\/p>\n<pre>$user = User::create([\r\n    'name'  =&gt;  'thinkphp',\r\n    'email' =&gt;  'thinkphp@qq.com'\r\n]);\r\necho $user-&gt;name;\r\necho $user-&gt;email;\r\necho $user-&gt;id; \/\/ \u83b7\u53d6\u81ea\u589eID\r\n\u548csave\u65b9\u6cd5\u4e0d\u540c\u7684\u662f\uff0ccreate\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u5f53\u524d\u6a21\u578b\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002\r\nallowField(true)<code><span class=\"hljs-comment\">\u8fc7\u6ee4post\u6570\u7ec4\u4e2d\u7684\u975e\u6570\u636e\u8868\u5b57\u6bb5\u6570\u636e\u3002\r\n<\/span><\/code>destroy\u6839\u636e\u4e3b\u952e\u5220\u9664\u3002 <code>name\u83b7\u53d6\u5355\u4e2a\u6570\u636e\u3002\u5982\u679c\u4f60\u662f\u5728\u6a21\u578b\u5185\u90e8\uff0c\u8bf7\u4e0d\u8981\u4f7f\u7528$this-&gt;name\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u6570\u636e\uff0c\u8bf7\u4f7f\u7528$this-&gt;getAttr('name') \u66ff\u4ee3\u3002\r\n\r\n<\/code><code><span class=\"hljs-comment\">all\u6839\u636e\u4e3b\u952e\u83b7\u53d6\u591a\u4e2a\u6570\u636e:\r\n<\/span><\/code><\/pre>\n<pre><code><span class=\"hljs-comment\">\/\/ \u6839\u636e\u4e3b\u952e\u83b7\u53d6\u591a\u4e2a\u6570\u636e<\/span>\r\n$list = User::all(<span class=\"hljs-string\">'1,2,3'<\/span>);\r\n<span class=\"hljs-comment\">\/\/ \u6216\u8005\u4f7f\u7528\u6570\u7ec4<\/span>\r\n$list = User::all([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>]);\r\n<span class=\"hljs-keyword\">foreach<\/span>($list <span class=\"hljs-keyword\">as<\/span> $key=&gt;$user){\r\n    <span class=\"hljs-keyword\">echo<\/span> $user-&gt;name;\r\n}\r\n<span class=\"hljs-comment\">\/\/ \u4f7f\u7528\u6570\u7ec4\u67e5\u8be2<\/span>\r\n$list = User::all([<span class=\"hljs-string\">'status'<\/span>=&gt;<span class=\"hljs-number\">1<\/span>]);\r\n<span class=\"hljs-comment\">\/\/ \u4f7f\u7528\u95ed\u5305\u67e5\u8be2<\/span>\r\n$list = User::all(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">($query)<\/span><\/span>{\r\n    $query-&gt;where(<span class=\"hljs-string\">'status'<\/span>, <span class=\"hljs-number\">1<\/span>)-&gt;limit(<span class=\"hljs-number\">3<\/span>)-&gt;order(<span class=\"hljs-string\">'id'<\/span>, <span class=\"hljs-string\">'asc'<\/span>);\r\n});\r\n<span class=\"hljs-keyword\">foreach<\/span>($list <span class=\"hljs-keyword\">as<\/span> $key=&gt;$user){\r\n    <span class=\"hljs-keyword\">echo<\/span> $user-&gt;name;\r\n}\r\n<\/code>\u83b7\u53d6\u67d0\u4e2a\u5b57\u6bb5\u6216\u8005\u67d0\u4e2a\u5217\u7684\u503c \/\/ \r\n\u83b7\u53d6\u67d0\u4e2a\u7528\u6237\u7684\u79ef\u5206 User::where('id',10)-&gt;value('score'); \r\n\u83b7\u53d6\u67d0\u4e2a\u5217\u7684\u6240\u6709\u503c User::where('status',1)-&gt;column('name'); \r\n\u4ee5id\u4e3a\u7d22\u5f15 User::where('status',1)-&gt;column('name','id'); \r\n\u6ce8\u610f<code>value<\/code>\u548c<code>column<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684\u4e0d\u518d\u662f\u4e00\u4e2a\u6a21\u578b\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u800c\u662f\u5355\u7eaf\u7684\u503c\u6216\u8005\u67d0\u4e2a\u5217\u7684\u6570\u7ec4\u3002<\/pre>\n<pre><code><\/code><\/pre>\n<pre><code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>get\u6216\u8005find\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u5f53\u524d\u6a21\u578b\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21 &hellip; <a href=\"https:\/\/www.cxybj.com\/?p=294\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,133],"tags":[],"_links":{"self":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/294"}],"collection":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":12,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}