{"id":24860,"date":"2024-11-21T12:42:15","date_gmt":"2024-11-21T04:42:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24860\/"},"modified":"2024-11-21T12:42:15","modified_gmt":"2024-11-21T04:42:15","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8laravel%e5%bc%80%e5%8f%91%e4%b8%80%e4%b8%aa%e5%9c%a8%e7%ba%bf%e6%8a%95%e7%a5%a8%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24860\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u6295\u7968\u7cfb\u7edf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169892112019067.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u6295\u7968\u7cfb\u7edf<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4e8b\u7269\u90fd\u53ef\u4ee5\u5728\u7ebf\u5b8c\u6210\uff0c\u5176\u4e2d\u5305\u62ec\u6295\u7968\u3002\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\u53ef\u4ee5\u65b9\u4fbf\u3001\u9ad8\u6548\u5730\u6536\u96c6\u5927\u91cf\u7528\u6237\u7684\u610f\u89c1\u548c\u53cd\u9988\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u6846\u67b6\u6765\u5f00\u53d1\u4e00\u4e2a\u57fa\u672c\u7684\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u73af\u5883\u642d\u5efa\u4e0eLaravel\u5b89\u88c5\uff1a<\/p>\n<ol>\n<li>\u786e\u4fdd\u60a8\u7684\u673a\u5668\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u548cComposer\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u5148\u5b89\u88c5\u3002<\/li>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u4f7f\u7528Composer\u5b89\u88c5Laravel\uff1a<br \/>composer global require \/installer<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Laravel\u9879\u76ee\uff1a<br \/>laravel new votingsystem<\/li>\n<li>\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff1a<br \/>cd votingsystem<\/li>\n<li>\u542f\u52a8Laravel\u5f00\u53d1\u670d\u52a1\u5668\uff1a<br \/>php artisan serve<\/li>\n<li>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95eehttp:\/\/localhost:8000\uff0c\u5982\u679c\u770b\u5230Laravel\u7684\u6b22\u8fce\u9875\u9762\uff0c\u8bf4\u660e\u73af\u5883\u642d\u5efa\u6210\u529f\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u6570\u636e\u5e93\u51c6\u5907\uff1a<\/p>\n<ol>\n<li>\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\uff0c\u6253\u5f00.env\u6587\u4ef6\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u4fee\u6539\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre>DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=voting_system\nDB_USERNAME=root\nDB_PASSWORD=<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3avoting_system\u7684\u6570\u636e\u5e93\u3002<\/li>\n<\/ol>\n<p>\u4e09\u3001\u521b\u5efa\u6295\u7968\u76f8\u5173\u7684\u6a21\u578b\u3001\u8fc1\u79fb\u548c\u63a7\u5236\u5668\uff1a<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u521b\u5efa\u4e00\u4e2aPoll\u6a21\u578b\u548c\u76f8\u5e94\u7684\u6570\u636e\u8fc1\u79fb\u6587\u4ef6\uff1a<br \/>php artisan make:model Poll -m<\/li>\n<li>\n<p>\u5728\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49polls\u8868\u7684\u5b57\u6bb5\uff1a<\/p>\n<pre>public function up()\n{\n    Schema::create('polls', function (Blueprint $table) {\n        $table-&amp;gt;id();\n        $table-&amp;gt;string('title');\n        $table-&amp;gt;timestamps();\n    });\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u8fd0\u884c\u6570\u636e\u5e93\u8fc1\u79fb\uff1a<br \/>php artisan migrate<\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2aPollController\u63a7\u5236\u5668\uff0c\u5e76\u6dfb\u52a0create\u548cstore\u65b9\u6cd5\uff1a<\/p>\n<pre>php artisan make:controller PollController --resource<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u5728PollController\u4e2d\uff0c\u6dfb\u52a0create\u548cstore\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u4ee5\u4fbf\u4e8e\u521b\u5efa\u548c\u4fdd\u5b58\u6295\u7968\uff1a<\/p>\n<pre>&lt;?php namespace AppHttpControllers;\n\nuse IlluminateHttpRequest;\nuse AppPoll;\n\nclass PollController extends Controller\n{\n    public function create()\n    {\n        return view('polls.create');\n    }\n\n    public function store(Request $request)\n    {\n        $this-&gt;validate($request, [\n            'title' =&amp;gt; 'required'\n        ]);\n\n        $poll = Poll::create([\n            'title' =&amp;gt; $request-&amp;gt;title\n        ]);\n\n        \/\/ \u6dfb\u52a0\u6295\u7968\u9009\u9879\n        foreach($request-&amp;gt;options as $option) {\n            $poll-&amp;gt;options()-&amp;gt;create([\n                'name' =&amp;gt; $option\n            ]);\n        }\n\n        return redirect()-&amp;gt;route('poll.show', $poll-&amp;gt;id);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u521b\u5efaOption\u6a21\u578b\u548c\u76f8\u5e94\u7684\u6570\u636e\u8fc1\u79fb\u6587\u4ef6\uff1a<br \/>php artisan make:model Option -m<\/li>\n<li>\n<p>\u5728\u751f\u6210\u7684Option\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49options\u8868\u7684\u5b57\u6bb5\uff1a<\/p>\n<pre>public function up()\n{\n    Schema::create('options', function (Blueprint $table) {\n        $table-&amp;gt;id();\n        $table-&amp;gt;unsignedBigInteger('poll_id');\n        $table-&amp;gt;string('name');\n        $table-&amp;gt;timestamps();\n    });\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u8fd0\u884c\u6570\u636e\u5e93\u8fc1\u79fb\uff1a<br \/>php artisan migrate<\/li>\n<li>\n<p>\u5728Poll\u6a21\u578b\u4e2d\uff0c\u6dfb\u52a0\u4e0eOption\u6a21\u578b\u7684\u5173\u8054\u5173\u7cfb\uff1a<\/p>\n<pre>public function options()\n{\n    return $this-&amp;gt;hasMany(Option::class);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u56db\u3001\u521b\u5efa\u89c6\u56fe\u548c\u8def\u7531\uff1a<\/p>\n<ol>\n<li>\n<p>\u5728resources\/views\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3apolls\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2acreate.blade.php\u89c6\u56fe\u6587\u4ef6\uff1a<\/p>\n<pre><\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 @csrf \u6807\u9898\uff1a \u9009\u9879\uff1a <\/p>\n<ul>\n<li> <\/li>\n<\/ul>\n<p> \u6dfb\u52a0\u9009\u9879 \u63d0\u4ea4 &lt;script&gt; document.getElementById(&#8216;add-option&#8217;).addEventListener(&#8216;click&#8217;, function() { var option = document.createElement(&#8216;li&#8217;); option.innerHTML = &#8216;&lt;input type=&#8221;text&#8221; name=&#8221;options[]&#8221;&gt;&#8217;; document.getElementById(&#8216;options&#8217;).appendChild(option); }); &lt;\/script&gt; <\/li>\n<li>\n<p>\u5728routes\/web.php\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u8def\u7531\uff1a<\/p>\n<pre>Route::resource('poll', 'PollController');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u5237\u65b0\u8def\u7531\u7f13\u5b58\uff1a<br \/>php artisan route:cache<\/li>\n<\/ol>\n<p>\u4e94\u3001\u6d4b\u8bd5\uff1a<\/p>\n<ol>\n<li>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95eehttp:\/\/localhost:8000\/poll\/create\u3002<\/li>\n<li>\u8f93\u5165\u6295\u7968\u6807\u9898\u548c\u9009\u9879\uff0c\u70b9\u51fb&#8221;\u6dfb\u52a0\u9009\u9879&#8221;\u6309\u94ae\u53ef\u4ee5\u52a8\u6001\u6dfb\u52a0\u9009\u9879\u3002<\/li>\n<li>\u586b\u5199\u5b8c\u6bd5\u540e\uff0c\u70b9\u51fb&#8221;\u63d0\u4ea4&#8221;\u6309\u94ae\uff0c\u7cfb\u7edf\u4f1a\u5c06\u6295\u7968\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u8df3\u8f6c\u5230\u6295\u7968\u8be6\u60c5\u9875\u9762\u3002<\/li>\n<\/ol>\n<p>\u7ed3\u8bba\uff1a<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528Laravel\u6846\u67b6\u5982\u4f55\u5f00\u53d1\u4e00\u4e2a\u57fa\u672c\u7684\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\u3002\u901a\u8fc7\u914d\u7f6e\u73af\u5883\u3001\u521b\u5efa\u6a21\u578b\u3001\u8fc1\u79fb\u548c\u63a7\u5236\u5668\u4ee5\u53ca\u7f16\u5199\u89c6\u56fe\u548c\u8def\u7531\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u57fa\u672c\u7684\u6295\u7968\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u4e00\u6b65\u6269\u5c55\u548c\u5b8c\u5584\u7cfb\u7edf\uff0c\u4f8b\u5982\u589e\u52a0\u7528\u6237\u9274\u6743\u3001\u5c55\u793a\u6295\u7968\u7ed3\u679c\u7b49\u529f\u80fd\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5bf9\u60a8\u7684Laravel\u5f00\u53d1\u4e4b\u65c5\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\uff08\u6ce8\uff1a\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u4ec5\u4e3a\u53c2\u8003\uff0c\u8bf7\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u505a\u9002\u5f53\u8c03\u6574\u3002\uff09<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\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>\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u6295\u7968\u7cfb\u7edf \u5f15\u8a00\uff1a\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4e8b\u7269\u90fd\u53ef\u4ee5\u5728\u7ebf\u5b8c\u6210\uff0c\u5176\u4e2d\u5305\u62ec\u6295\u7968\u3002\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\u53ef\u4ee5\u65b9\u4fbf\u3001\u9ad8\u6548\u5730\u6536\u96c6\u5927\u91cf\u7528\u6237\u7684\u610f\u89c1\u548c\u53cd\u9988\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u6846\u67b6\u6765\u5f00\u53d1\u4e00\u4e2a\u57fa\u672c\u7684\u5728\u7ebf\u6295\u7968\u7cfb\u7edf\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u73af\u5883\u642d\u5efa\u4e0eLaravel\u5b89\u88c5\uff1a \u786e\u4fdd\u60a8\u7684\u673a\u5668\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u548cComposer\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u5148\u5b89\u88c5\u3002 \u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u4f7f\u7528Composer\u5b89\u88c5Laravel\uff1acomposer global require \/installer \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Laravel\u9879\u76ee\uff1alaravel new votingsystem \u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff1acd votingsystem \u542f\u52a8Laravel\u5f00\u53d1\u670d\u52a1\u5668\uff1aphp artisan serve \u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95eehttp:\/\/localhost:8000\uff0c\u5982\u679c\u770b\u5230Laravel\u7684\u6b22\u8fce\u9875\u9762\uff0c\u8bf4\u660e\u73af\u5883\u642d\u5efa\u6210\u529f\u3002 \u4e8c\u3001\u6570\u636e\u5e93\u51c6\u5907\uff1a \u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\uff0c\u6253\u5f00.env\u6587\u4ef6\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u4fee\u6539\u4ee5\u4e0b\u884c\uff1a DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=voting_system DB_USERNAME=root DB_PASSWORD= \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2a\u540d\u4e3avoting_system\u7684\u6570\u636e\u5e93\u3002 \u4e09\u3001\u521b\u5efa\u6295\u7968\u76f8\u5173\u7684\u6a21\u578b\u3001\u8fc1\u79fb\u548c\u63a7\u5236\u5668\uff1a \u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u521b\u5efa\u4e00\u4e2aPoll\u6a21\u578b\u548c\u76f8\u5e94\u7684\u6570\u636e\u8fc1\u79fb\u6587\u4ef6\uff1aphp artisan make:model Poll -m \u5728\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49polls\u8868\u7684\u5b57\u6bb5\uff1a public function up() { Schema::create(&#8216;polls&#8217;, function (Blueprint $table) { $table-&amp;gt;id(); $table-&amp;gt;string(&#8216;title&#8217;); $table-&amp;gt;timestamps(); }); } \u767b\u5f55\u540e\u590d\u5236 \u8fd0\u884c\u6570\u636e\u5e93\u8fc1\u79fb\uff1aphp artisan migrate \u521b\u5efa\u4e00\u4e2aPollController\u63a7\u5236\u5668\uff0c\u5e76\u6dfb\u52a0create\u548cstore\u65b9\u6cd5\uff1a php artisan [&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-24860","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24860","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=24860"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24860\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}