{"id":22803,"date":"2024-11-21T14:28:03","date_gmt":"2024-11-21T06:28:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22803\/"},"modified":"2024-11-21T14:28:03","modified_gmt":"2024-11-21T06:28:03","slug":"thinkphp%e5%a4%9a%e9%80%89%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22803\/","title":{"rendered":"thinkphp\u591a\u9009\u5220\u9664"},"content":{"rendered":"<p>thinkphp6\u662f\u4e00\u6b3e\u975e\u5e38\u4f18\u79c0\u7684php\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u57fa\u4e8emvc\u67b6\u6784\uff0c\u5f00\u53d1\u6548\u7387\u9ad8\u3001\u6613\u4e0a\u624b\u3001\u5b89\u5168\u53ef\u9760\uff0c\u540c\u65f6\u6781\u5927\u7b80\u5316\u4e86\u4ee3\u7801\u7f16\u5199\u7684\u6d41\u7a0b\u3002\u4eca\u5929\uff0c\u6211\u4eec\u6765\u8c08\u4e00\u4e0b\u4f7f\u7528thinkphp6\u5b9e\u73b0\u591a\u9009\u5220\u9664\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4e00\u3001\u524d\u8a00<\/p>\n<p>\u591a\u9009\u5220\u9664\u662f\u73b0\u4ee3Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5e38\u89c1\u529f\u80fd\u4e4b\u4e00\u3002\u5b83\u5141\u8bb8\u7528\u6237\u5728\u6570\u636e\u8868\u683c\u6216\u5217\u8868\u4e2d\u9009\u62e9\u591a\u4e2a\u9879\u76ee\uff0c\u7136\u540e\u53ef\u4ee5\u5220\u9664\u5b83\u4eec\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528thinkphp6\u7684\u63a7\u5236\u5668\u548c\u6a21\u578b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<p>\u4e8c\u3001\u5efa\u7acb\u6570\u636e\u5e93<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728MySQL\u6570\u636e\u5e93\u4e2d\u5efa\u7acb\u4e00\u4e2a\u793a\u4f8b\u8868\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u6216phpMyAdmin\u6765\u5b8c\u6210\u8fd9\u4e2a\u6b65\u9aa4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>CREATE TABLE example_table (<br \/> id int(11) NOT NULL AUTO_INCREMENT,<br \/> name varchar(255) NOT NULL,<br \/> email varchar(255) NOT NULL,<br \/> PRIMARY KEY (id)<br \/>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c example_table\u201d\u7684\u65b0\u8868\uff0c\u5176\u4e2d\u5305\u542bid\uff0cname\u548cemail\u5b57\u6bb5\u3002<\/p>\n<p>\u4e09\u3001\u751f\u6210\u6a21\u578b\u548c\u63a7\u5236\u5668<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u4e0a\u9762\u521b\u5efa\u7684\u8868\u7ed3\u6784\u6765\u751f\u6210\u4e00\u4e2a\u6a21\u578b\u548c\u4e00\u4e2a\u63a7\u5236\u5668\u3002<\/p>\n<p>\u4f7f\u7528thinkphp6\u7684Artisan\u547d\u4ee4\u884c\u5de5\u5177\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u8fd9\u4e9b\u6587\u4ef6\uff1a<\/p>\n<p>php think make:model ExampleModel &#8211;migration<\/p>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6a21\u578b\u6587\u4ef6\uff08 app \/Model\/ExampleModel.php \uff09\u548c\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\uff08 database \/migrations \/yyyymmddhhmmss_create_example_model.php \uff09\u3002<\/p>\n<p>php think make:controller ExampleController<\/p>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3aExampleController.php\u7684\u65b0\u6587\u4ef6\uff08 app \/Controller\/ExampleController.php \uff09\u3002<\/p>\n<p>\u56db\u3001\u7f16\u5199\u4ee3\u7801<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u51c6\u5907\u597d\u7f16\u5199\u63a7\u5236\u5668\u4ee3\u7801\u6765\u5904\u7406\u591a\u9009\u5220\u9664\u4e86\u3002\u5728\u63a7\u5236\u5668\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e24\u4e2a\u4e3b\u8981\u7684action\u51fd\u6570\uff1aindex\uff08\u7528\u4e8e\u663e\u793a\u6240\u6709\u6570\u636e\uff09\u548cdelete\uff08\u7528\u4e8e\u5904\u7406\u5220\u9664\u8bf7\u6c42\uff09\u3002<\/p>\n<p>\u5728app \/Controller \/ExampleController.php\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>acadeView;\nuse thinkRequest;\nuse appmodelExampleModel;\n\nclass ExampleController\n{\n    public function index()\n    {\n        $list = ExampleModel::all();\n\n        return View::fetch('index', [\n            'list' =&amp;gt; $list,\n        ]);\n    }\n\n    public function delete(Request $request)\n    {\n        $ids = $request-&amp;gt;param('ids');\n\n        foreach ($ids as $id) {\n            ExampleModel::destroy($id);\n        }\n\n        return ['status' =&amp;gt; 'success', 'message' =&amp;gt; '\u5220\u9664\u6210\u529f'];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86\u7c7b\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570index\u548cdelete\u3002\u51fd\u6570index\u7528\u4e8e\u4eceExampleModel\u4e2d\u83b7\u53d6\u6240\u6709\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u5230\u6a21\u677f\u4e2d\u3002\u51fd\u6570delete\u4eceHTTP\u8bf7\u6c42\u4e2d\u83b7\u53d6\u8981\u5220\u9664\u7684ID\u5217\u8868\uff0c\u5e76\u4f7f\u7528ExampleModel::destroy\u65b9\u6cd5\u5220\u9664\u8fd9\u4e9b\u9879\u76ee\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6a21\u677f\u6dfb\u52a0\u4e00\u4e2a\u591a\u9009\u6846\u4ee5\u53ca\u5220\u9664\u6309\u94ae\u3002\u6253\u5f00app \/View \/example \/index.html\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>\n\n\n    &lt;meta charset=\"UTF-8\"&gt;&lt;title&gt;Example&lt;\/title&gt;&lt;h1&gt;Example&lt;\/h1&gt;\n    <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>ID<\/th>\n<th>Name<\/th>\n<th>Email<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><\/td>\n<td> id ?&gt;<\/td>\n<td> name ?&gt;<\/td>\n<td> email ?&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \u5220\u9664 &lt;script&gt; var btnDelete = document.querySelector(&#8216;#btn-delete&#8217;); btnDelete.addEventListener(&#8216;click&#8217;, function () { var ids = []; var checkboxes = document.querySelectorAll(&#8216;input[type=checkbox][name^=ids]:checked&#8217;); for (var i = 0; i &lt; checkboxes.length; i++) { ids.push(checkboxes[i].value); } if (ids.length &gt; 0) { if (confirm(&#8216;\u786e\u5b9a\u5220\u9664\uff1f&#8217;)) { var xhr = new XMLHttpRequest(); xhr.open(&#8216;POST&#8217;, &#8216;\/example\/delete&#8217;); xhr.setRequestHeader(&#8216;Content-Type&#8217;, &#8216;application\/json;charset=UTF-8&#8217;); xhr.onload = function () { var result = JSON.parse(xhr.responseText); if (result.status == &#8216;success&#8217;) { alert(result.message); location.reload(); } else { console.error(result); alert(&#8216;\u5220\u9664\u5931\u8d25&#8217;); } }; xhr.onerror = function () { console.error(xhr); alert(&#8216;\u7f51\u7edc\u5f02\u5e38\uff0c\u8bf7\u91cd\u8bd5&#8217;); }; xhr.send(JSON.stringify({ids: ids})); } } else { alert(&#8216;\u8bf7\u9009\u62e9\u8981\u5220\u9664\u7684\u9879\u76ee&#8217;); } }); var checkAll = document.querySelector(&#8216;#check-all&#8217;); checkAll.addEventListener(&#8216;click&#8217;, function () { var checkboxes = document.querySelectorAll(&#8216;input[type=checkbox][name^=ids]&#8217;); for (var i = 0; i &lt; checkboxes.length; i++) { checkboxes[i].checked = checkAll.checked; } }); &lt;\/script&gt; <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86JavaScript\u6765\u83b7\u53d6\u6240\u6709\u9009\u4e2d\u7684\u591a\u9009\u6846\uff0c\u5e76\u5c06\u8fd9\u4e9bID\u4f20\u9012\u7ed9\u670d\u52a1\u5668\u7684\/delete\u8def\u7531\u3002<\/p>\n<p>\u4e94\u3001\u6d4b\u8bd5<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5f00\u63a7\u5236\u53f0\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06Web\u670d\u52a1\u5668\u542f\u52a8\u8d77\u6765\u3002<\/p>\n<p>php think run<\/p>\n<p>\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165localhost:8000\/example\uff0c\u5373\u53ef\u6253\u5f00example\u7684\u9996\u9875\uff0c\u70b9\u51fb\u591a\u9009\u6846\u4ee5\u9009\u62e9\u8981\u5220\u9664\u7684\u9879\u3002\u6700\u540e\u70b9\u51fb\u5220\u9664\u6309\u94ae\u5373\u53ef\u5220\u9664\u9009\u62e9\u7684\u9879\u3002<\/p>\n<p>\u516d\u3001\u7ed3\u8bba<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528thinkphp6\u7684\u63a7\u5236\u5668\uff0c\u6a21\u578b\u548c\u89c6\u56fe\u6765\u5b9e\u73b0\u4e86\u591a\u9009\u5220\u9664\u7684\u529f\u80fd\u3002\u8fd9\u4e2a\u529f\u80fd\u53ef\u4ee5\u5728Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53d1\u6325\u91cd\u8981\u4f5c\u7528\uff0c\u4f18\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u591a\u9009\u5220\u9664\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>thinkphp6\u662f\u4e00\u6b3e\u975e\u5e38\u4f18\u79c0\u7684php\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u57fa\u4e8emvc\u67b6\u6784\uff0c\u5f00\u53d1\u6548\u7387\u9ad8\u3001\u6613\u4e0a\u624b\u3001\u5b89\u5168\u53ef\u9760\uff0c\u540c\u65f6\u6781\u5927\u7b80\u5316\u4e86\u4ee3\u7801\u7f16\u5199\u7684\u6d41\u7a0b\u3002\u4eca\u5929\uff0c\u6211\u4eec\u6765\u8c08\u4e00\u4e0b\u4f7f\u7528thinkphp6\u5b9e\u73b0\u591a\u9009\u5220\u9664\u7684\u65b9\u6cd5\u3002 \u4e00\u3001\u524d\u8a00 \u591a\u9009\u5220\u9664\u662f\u73b0\u4ee3Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5e38\u89c1\u529f\u80fd\u4e4b\u4e00\u3002\u5b83\u5141\u8bb8\u7528\u6237\u5728\u6570\u636e\u8868\u683c\u6216\u5217\u8868\u4e2d\u9009\u62e9\u591a\u4e2a\u9879\u76ee\uff0c\u7136\u540e\u53ef\u4ee5\u5220\u9664\u5b83\u4eec\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528thinkphp6\u7684\u63a7\u5236\u5668\u548c\u6a21\u578b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002 \u4e8c\u3001\u5efa\u7acb\u6570\u636e\u5e93 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728MySQL\u6570\u636e\u5e93\u4e2d\u5efa\u7acb\u4e00\u4e2a\u793a\u4f8b\u8868\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u6216phpMyAdmin\u6765\u5b8c\u6210\u8fd9\u4e2a\u6b65\u9aa4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b CREATE TABLE example_table ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, email varchar(255) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c example_table\u201d\u7684\u65b0\u8868\uff0c\u5176\u4e2d\u5305\u542bid\uff0cname\u548cemail\u5b57\u6bb5\u3002 \u4e09\u3001\u751f\u6210\u6a21\u578b\u548c\u63a7\u5236\u5668 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u4e0a\u9762\u521b\u5efa\u7684\u8868\u7ed3\u6784\u6765\u751f\u6210\u4e00\u4e2a\u6a21\u578b\u548c\u4e00\u4e2a\u63a7\u5236\u5668\u3002 \u4f7f\u7528thinkphp6\u7684Artisan\u547d\u4ee4\u884c\u5de5\u5177\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u8fd9\u4e9b\u6587\u4ef6\uff1a php think make:model ExampleModel &#8211;migration \u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6a21\u578b\u6587\u4ef6\uff08 app \/Model\/ExampleModel.php \uff09\u548c\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\uff08 database \/migrations \/yyyymmddhhmmss_create_example_model.php \uff09\u3002 php think make:controller ExampleController \u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3aExampleController.php\u7684\u65b0\u6587\u4ef6\uff08 app \/Controller\/ExampleController.php \uff09\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-22803","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22803","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=22803"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22803\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}