{"id":230,"date":"2018-12-06T23:43:06","date_gmt":"2018-12-06T15:43:06","guid":{"rendered":"https:\/\/103.249.111.182\/?p=230"},"modified":"2024-08-17T22:53:40","modified_gmt":"2024-08-17T14:53:40","slug":"%e7%ac%94%e8%ae%b0laravel%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/7gugu.com\/index.php\/2018\/12\/06\/%e7%ac%94%e8%ae%b0laravel%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c\/","title":{"rendered":"[\u7b14\u8bb0][Laravel]\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"<h1>\u51c6\u5907<\/h1>\n<p>\u65b0\u5efaApp\\Http\\Controller\\StudentController.php<\/p>\n<pre>&lt;?php\nnamespace App\\Http\\Controller;\nuse Illuminate\\Support\\Facades\\DB;\/\/\u4f7f\u7528DB\u7c7b\n\nclass StudentController extends Controller{\n\/\/\u521b\u5efa\u540d\u4e3aStudentController\u7684\u63a7\u5236\u5668\u5e76\u7ee7\u627fController\u57fa\u7c7b\npublic function query(){\n\/\/php\n}\n}<\/pre>\n<p>\u5207\u6362\u81f3routes\\web.php\u65b0\u5efa\u4e00\u4e2a\u8def\u7531<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nRoute::any('query','StudentController@query');\n<\/pre>\n<p>\u521b\u5efa<\/p>\n<hr \/>\n<h1>\u8fde\u63a5\u6570\u636e\u5e93<\/h1>\n<h4>\u914d\u7f6e.env\u6587\u4ef6<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB_CONNECTION=mysql&#x5B;\u6570\u636e\u5e93\u7684\u8fde\u63a5\u6a21\u5f0f]\nDB_HOST=\u6570\u636e\u5e93\u7684IP\u5730\u5740\nDB_PORT=\u6570\u636e\u5e93\u7aef\u53e3\nDB_DATABASE=\u6570\u636e\u5e93\u5e93\u540d\nDB_USERNAME=\u6570\u636e\u5e93\u7528\u6237\u540d\nDB_PASSWORD=\u6570\u636e\u5e93\u5bc6\u7801\n<\/pre>\n<h4>\u914d\u7f6econfig\/database.php<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n'mysql' =&gt; &#x5B;\n'driver' =&gt; 'mysql', \/\/\u6570\u636e\u5e93\u7684\u7c7b\u578b\n'host' =&gt; env('DB_HOST', 'localhost'), \/\/\u6570\u636e\u5e93\u7684\u4f4d\u7f6e\n'port' =&gt; env('DB_PORT', '3306'), \/\/\u7aef\u53e3\u53f7\n'database' =&gt; env('DB_DATABASE', ''), \/\/\u6570\u636e\u5e93\u540d\n'username' =&gt; env('DB_USERNAME', ''), \/\/\u7528\u6237\u540d\n'password' =&gt; env('DB_PASSWORD', ''), \/\/\u5bc6\u7801\n'charset' =&gt; 'utf8', \/\/\u5b57\u7b26\u96c6\n'collation' =&gt; 'utf8_unicode_ci', \/\/\u6392\u5e8f\u65b9\u5f0f\n'prefix' =&gt; '', \/\/\u524d\u7f00\n'strict' =&gt; true, \/\/Strict\u6a21\u5f0f\n'engine' =&gt; null, \/\/\u5f15\u64ce\n],\n<\/pre>\n<p><strong>\u81f3\u6b64laravel\u5373\u53ef\u8fde\u63a5\u4e0a\u6570\u636e\u5e93\u4e86<\/strong><\/p>\n<hr \/>\n<h1>\u4f7f\u7528DB facades\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c<\/h1>\n<p>SQL\u8bed\u53e5\u4e2d\u7684?\u662f\u5360\u4f4d\u7b26,\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e8c\u53d8\u91cf\u4f20\u5165\u6570\u7ec4\u6765\u4f20\u9012\u6570\u503c<\/p>\n<h4>\u67e5\u8be2\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::select(\"select * from student\");\/\/\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528select\u8bed\u53e5\nDB::select(\"select * from student where age = ? and name = ?\",&#x5B;18,'7gugu'])\/\/\u7b49\u540c\u4e8e:select * from student where age = 18 and name = '7gugu'\n<\/pre>\n<h4>\u63d2\u5165\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::insert(\"insert into student(name,age,sex) values('hzq','19','0')\");\/\/\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528insert\u8bed\u53e5\nDB::insert(\"insert into student(name,age,sex) values(?,?,?)\",&#x5B;'hzq','19','0']);\/\/\u7b49\u540c\u4e8e:insert into student(name,age,sex) values('hzq','19','0'),\u8fd9\u6837\u5b50\u65b9\u4fbf\u4f20\u503c\n<\/pre>\n<h4>\u66f4\u65b0\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::update(\"update student set age = 1 where name =7gugu\");\/\/\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528update\u8bed\u53e5\nDB::update(\"update student set age = ? where name =?\",&#x5B;1,'7gugu'])\/\/\u7b49\u540c\u4e8e:update student set age = 1 where name ='7gugu'\n<\/pre>\n<h4>\u5220\u9664\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::delete('delete from student where id=1002');\/\/\u76f4\u63a5\u4f7f\u7528delete\u8bed\u53e5\nDB::delete('delete from student where id=?',&#x5B;'1002']);\/\/\u7b49\u540c\u4e8e:delete from student where id=1002\n<\/pre>\n<hr \/>\n<h1>\u4f7f\u7528\u67e5\u8be2\u6784\u9020\u5668<\/h1>\n<h4>\u6570\u636e\u8868\u6307\u5411\u5668<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::table(\"\u6570\u636e\u8868\u540d\")-&gt;\u64cd\u4f5c\/\u7b5b\u9009\u5668;\nDB::table('student')-&gt;\u64cd\u4f5c\/\u7b5b\u9009\u5668;\n<\/pre>\n<h4>\u7b5b\u9009\u5668<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::table('student')-&gt;where()-&gt;\u64cd\u4f5c;\nwhere('id',1001)\/\/\u5373\u53ef\u7b5b\u9009id\u662f1001\u7684\u6570\u636e\nwhere('id','=',1001)\/\/\u5373\u53ef\u7b5b\u9009id\u662f1001\u7684\u6570\u636e\nwhere('id = ? and name = ?',&#x5B;1001,'7gugu'])\/\/\u5373\u53ef\u9650\u5b9a\u591a\u4e2a\u6761\u4ef6\n<\/pre>\n<h4>\u6392\u5e8f<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/*\n* @param string \u5b57\u6bb5\u540d\u79f0\n* @param string \u6392\u5e8f\u65b9\u5f0f(ASC:\u6b63\u5e8f|DESC:\u5012\u5e8f)\n*\/\nDB::table('student')-&gt;orderBy('\u5b57\u6bb5\u540d','asc|desc')-&gt;\u64cd\u4f5c;\n<\/pre>\n<h4>\u5b57\u6bb5\u503c\u81ea\u589e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/*\n* \u6570\u503c\u81ea\u589e\n* @param string \u5b57\u6bb5\u540d\u79f0\n* @param int \u5355\u6b21\u81ea\u589e\u6570\u91cf\n*\/\nDB::table('student')-&gt;where('id','1001')-&gt;increment('age',1);\n<\/pre>\n<h4>\u5b57\u6bb5\u503c\u81ea\u51cf<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/*\n* \u6570\u503c\u81ea\u51cf\n* @param string \u5b57\u6bb5\u540d\u79f0\n* @param int \u5355\u6b21\u81ea\u51cf\u6570\u91cf\n*\/\nDB::table('student')-&gt;where('id','1001')-&gt;decrement('age',1);\n<\/pre>\n<h4>\u63d2\u5165\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::table('student')-&gt;insert(&#x5B;'name'=&gt;'7gugu','age'=&gt;'18']);\/\/\u63d2\u5165\u4e00\u6761\u6570\u636e\nDB::table('student')-&gt;insert(&#x5B;\n&#x5B;'name'=&gt;'test','age'=&gt;'20'],\n&#x5B;'name'=&gt;'guangzhou','age'=&gt;'30']\n]);\/\/\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u627f\u8f7d\u591a\u6761\u6570\u636e,\u5373\u53ef\u63d2\u5165\u591a\u6761\u6570\u636e\n<\/pre>\n<h4>\u66f4\u65b0\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::table('student')-&gt;where('id',1004)-&gt;update(&#x5B;'age'=&gt;30]);\/\/\u4f7f\u7528update\u51fd\u6570\u5373\u53ef\u66f4\u65b0\u6307\u5b9a\u5b57\u6bb5\u7684\u503c\n\/\/laravel\u597d\u50cf\u5e76\u4e0d\u53ef\u4ee5\u4e00\u6b21\u6027\u66f4\u65b0\u591a\u4e2a\u5b57\u6bb5\n<\/pre>\n<h4>\u5220\u9664\u6570\u636e<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::table('student')-&gt;where('id','1002')-&gt;delete();\n<\/pre>\n<h4>get\u51fd\u6570<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/*\n*\u7528\u4e8e\u83b7\u53d6\u9650\u5b9a\u6761\u4ef6\u540e\u7684\u6570\u636e,\u53ef\u4ee5\u65e0\u9650\u5b9a\u6761\u4ef6\u76f4\u63a5\u83b7\u53d6\n*\/\nDB::table('student')-&gt;get();\n<\/pre>\n<h4>first\u51fd\u6570<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/*\n* \u7528\u4e8e\u83b7\u53d6\u7b26\u5408\u9650\u5b9a\u6761\u4ef6\u7684\u7b2c\u4e00\u884c\u7684\u6570\u636e\n* \u65e0\u9700\u4f20\u53c2\n*\/\nDB::table('student')-&gt;first();\n<\/pre>\n<h4>pluck\u51fd\u6570<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/*\n* \u83b7\u53d6\u6307\u5b9a\u540d\u79f0\u7684\u5b57\u6bb5\u6570\u636e\n*\/\nDB::table('student')-&gt;pluck('name','age');\n<\/pre>\n<p>\u6548\u679c\u5982\u56fe\u6240\u793a:<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-236\" src=\"https:\/\/103.249.111.182\/wp-content\/uploads\/2018\/12\/QQ\u622a\u56fe20181209234249-1024x304.jpg\" sizes=\"auto, (max-width: 525px) 85vw, 525px\" srcset=\"https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u622a\u56fe20181209234249-1024x304.jpg 1024w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u622a\u56fe20181209234249-300x89.jpg 300w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u622a\u56fe20181209234249-768x228.jpg 768w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u622a\u56fe20181209234249-1200x356.jpg 1200w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u622a\u56fe20181209234249.jpg 1500w\" alt=\"\" width=\"525\" height=\"156\" \/><\/p>\n<h4>\u805a\u5408\u51fd\u6570<\/h4>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/\/count()\u7edf\u8ba1\u8868\u7684\u6570\u636e\u603b\u6570\nDB::table('student')-&gt;count();\n\/\/max()\u6bd4\u8f83\u67d0\u4e2a\u5b57\u6bb5\u7684\u6700\u5927\u503c\nDB::table('student')-&gt;max('age');\n\/\/min()\u6bd4\u8f83\u67d0\u4e2a\u5b57\u6bb5\u7684\u6700\u5c0f\u503c\nDB::table('student')-&gt;min('age');\n\/\/avg()\u8ba1\u7b97\u67d0\u4e2a\u5b57\u6bb5\u7684\u5e73\u5747\u503c\nDB::table('student')-&gt;avg('age');\n\/\/sum()\u8ba1\u7b97\u67d0\u4e00\u4e2a\u5b57\u6bb5\u7684\u6240\u6709\u6570\u503c\nDB::table('student')-&gt;avg('age');\n<\/pre>\n<h4>truncate\u51fd\u6570<\/h4>\n<p><strong>\u6e05\u7a7a\u8868\u4e2d\u6570\u636e<span style=\"color: #ff9900;\">[\u5371\u9669]<\/span><\/strong><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/\/truncate()\u6e05\u7a7a\u8868\u4e2d\u6570\u636e&#x5B;\u5371\u9669]\nDB::table('student')-&gt;truncate();\n<\/pre>\n<h4>chunk\u51fd\u6570<\/h4>\n<p>\u7528\u4e8e\u5206\u5757\u67e5\u627e\u6570\u636e(\u7528\u4e8e\u5927\u91cf\u6570\u636e\u67e5\u627e\u65f6\u4f7f\u7528)<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::table('users')-&gt;chunk(2, function($users)\n{\ndd($users);\/\/\u6bcf\u6b21\u8fd0\u884c\u4ec5\u4ec5\u67e5\u8be2\u4e24\u6761\u6570\u636e,\u76f4\u81f3\u67e5\u8be2\u5b8c\u6bd5\n});\n<\/pre>\n<p>\u7ed3\u679c\u5982\u56fe\u6240\u793a:<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-239 size-large\" src=\"https:\/\/103.249.111.182\/wp-content\/uploads\/2018\/12\/QQ\u56fe\u724720181211191605-514x1024.png\" sizes=\"auto, (max-width: 514px) 85vw, 514px\" srcset=\"https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u56fe\u724720181211191605-514x1024.png 514w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u56fe\u724720181211191605-151x300.png 151w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u56fe\u724720181211191605-768x1530.png 768w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u56fe\u724720181211191605-771x1536.png 771w, https:\/\/7gugu.com\/wp-content\/uploads\/2018\/12\/QQ\u56fe\u724720181211191605.png 856w\" alt=\"\" width=\"514\" height=\"1024\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51c6\u5907 \u65b0\u5efaApp\\Http\\Controller\\StudentController.php &lt;?php &hellip; <a href=\"https:\/\/7gugu.com\/index.php\/2018\/12\/06\/%e7%ac%94%e8%ae%b0laravel%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c[\u7b14\u8bb0][Laravel]\u6570\u636e\u5e93\u64cd\u4f5c\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-230","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/comments?post=230"}],"version-history":[{"count":1,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"predecessor-version":[{"id":3617,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/230\/revisions\/3617"}],"wp:attachment":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/tags?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}