{"id":22568,"date":"2024-11-21T15:40:50","date_gmt":"2024-11-21T07:40:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22568\/"},"modified":"2024-11-21T15:40:50","modified_gmt":"2024-11-21T07:40:50","slug":"laravel%e6%a1%86%e6%9e%b6%e6%95%b0%e6%8d%ae%e5%ba%93%e4%bf%ae%e6%94%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22568\/","title":{"rendered":"laravel\u6846\u67b6\u6570\u636e\u5e93\u4fee\u6539"},"content":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u975e\u5e38\u53d7\u6b22\u8fce\u7684php web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u4fee\u6539\u7684\u8fc7\u7a0b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8laravel\u6846\u67b6\u7684\u6570\u636e\u5e93\u4fee\u6539\u76f8\u5173\u5185\u5bb9\u3002<\/p>\n<ol>\n<li>\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ol>\n<p>\u5728Laravel\u6846\u67b6\u4e2d\uff0c\u6570\u636e\u5e93\u8fc1\u79fb\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u5e93\u7684\u7ed3\u6784\u4ece\u4e00\u79cd\u7248\u672c\u8f6c\u79fb\u5230\u53e6\u4e00\u79cd\u7248\u672c\u3002\u901a\u8fc7\u4f7f\u7528\u8fc1\u79fb\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u4fee\u6539\u8868\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5217\u3001\u66f4\u6539\u7d22\u5f15\u548c\u7ea6\u675f\u7b49\u3002\u8fc1\u79fb\u6587\u4ef6\u5b58\u50a8\u5728\u201cdatabase\/migrations\u201d\u76ee\u5f55\u4e0b\u3002\u6bcf\u4e2a\u8fc1\u79fb\u6587\u4ef6\u4ee3\u8868\u4e00\u4e2a\u6570\u636e\u5e93\u8fc1\u79fb\u64cd\u4f5c\uff0c\u5e76\u5305\u542b\u7528\u4e8e\u4fee\u6539\u6570\u636e\u5e93\u7684\u201cup\u201d\u548c\u201cdown\u201d\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd0\u884c\u201cphp artisan migrate\u201d\u547d\u4ee4\uff0c\u5c06\u81ea\u52a8\u6267\u884c\u6240\u6709\u672a\u6267\u884c\u7684\u8fc1\u79fb\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8fc1\u79fb\u6587\u4ef6\uff0c\u7528\u4e8e\u521b\u5efa\u201cusers\u201d\u8868\uff1a<\/p>\n<pre>use IlluminateSupportFacadesSchema;\nuse IlluminateDatabaseSchemaBlueprint;\nuse IlluminateDatabaseMigrationsMigration;\n\nclass CreateUsersTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::create('users', function (Blueprint $table) {\n            $table-&amp;gt;increments('id');\n            $table-&amp;gt;string('name');\n            $table-&amp;gt;string('email')-&amp;gt;unique();\n            $table-&amp;gt;string('password');\n            $table-&amp;gt;rememberToken();\n            $table-&amp;gt;timestamps();\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        Schema::dropIfExists('users');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8be5\u8fc1\u79fb\u6587\u4ef6\u4f7f\u7528\u201cSchema\u201d\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u201cusers\u201d\u8868\uff0c\u5305\u62ec\u81ea\u589eID\u3001\u540d\u79f0\u3001\u552f\u4e00\u7535\u5b50\u90ae\u4ef6\u3001\u5bc6\u7801\u548c\u65f6\u95f4\u6233\u7b49\u5217\u3002\u5728\u201cdown\u201d\u65b9\u6cd5\u4e2d\uff0c\u5220\u9664\u4e86\u201cusers\u201d\u8868\u3002<\/p>\n<ol>\n<li>\u6570\u636e\u586b\u5145<\/li>\n<\/ol>\n<p>\u5728Laravel\u6846\u67b6\u4e2d\uff0c\u6570\u636e\u586b\u5145\u662f\u6307\u5411\u6570\u636e\u5e93\u6dfb\u52a0\u6d4b\u8bd5\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u4f7f\u7528\u6570\u636e\u586b\u5145\u53ef\u7b80\u5316\u6d4b\u8bd5\u548c\u6f14\u793a\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u8fc7\u7a0b\u3002\u6570\u636e\u586b\u5145\u6587\u4ef6\u5b58\u50a8\u5728\u201cdatabase\/seeds\u201d\u76ee\u5f55\u4e0b\u3002\u6267\u884c\u201cphp artisan db:seed\u201d\u547d\u4ee4\u53ef\u4ee5\u586b\u5145\u6570\u636e\u3002<\/p>\n<pre>use IlluminateDatabaseSeeder;\nuse IlluminateSupportFacadesDB;\nuse IlluminateSupportFacadesHash;\nuse AppUser;\n\nclass UsersTableSeeder extends Seeder\n{\n    \/**\n     * Run the database seeds.\n     *\n     * @return void\n     *\/\n    public function run()\n    {\n        DB::table('users')-&amp;gt;delete();\n\n        $users = array(\n            array(\n                'name' =&amp;gt; 'admin',\n                'email' =&amp;gt; 'admin@admin.com',\n                'password' =&amp;gt; Hash::make('password'),\n            ),\n            array(\n                'name' =&amp;gt; 'user',\n                'email' =&amp;gt; 'user@user.com',\n                'password' =&amp;gt; Hash::make('password'),\n            ),\n        );\n\n        foreach ($users as $user) {\n            User::create($user);\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8be5\u6587\u4ef6\u521b\u5efa\u4e86\u4e24\u4e2a\u7528\u6237\uff0c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u6570\u636e\u5e93\u4e2d\u3002\u6ce8\u610f\uff0c\u201cpassword\u201d\u5217\u4f7f\u7528\u4e86Laravel\u7684\u201cHash\u201d\u7c7b\u8fdb\u884c\u52a0\u5bc6\u3002\u5728\u8fd0\u884c\u201cphp artisan db:seed\u201d\u547d\u4ee4\u4e4b\u540e\uff0c\u201cusers\u201d\u8868\u5c06\u5305\u542b\u8fd9\u4e24\u4e2a\u7528\u6237\u7684\u8bb0\u5f55\u3002<\/p>\n<ol>\n<li>\u6a21\u578b\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u5728Laravel\u6846\u67b6\u4e2d\uff0c\u901a\u8fc7\u5728\u6a21\u578b\u4e4b\u95f4\u521b\u5efa\u5173\u8054\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u6570\u636e\u5e93\u4e2d\u7684\u76f8\u5173\u6570\u636e\u3002\u6709\u4e09\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\uff1a\u4e00\u5bf9\u4e00\u3001\u4e00\u5bf9\u591a\u548c\u591a\u5bf9\u591a\u3002\u5728\u6a21\u578b\u4e2d\u5b9a\u4e49\u76f8\u5173\u5173\u7cfb\u540e\uff0c\u60a8\u53ef\u4ee5\u50cf\u8bbf\u95ee\u6a21\u578b\u672c\u8eab\u4e00\u6837\u8bbf\u95ee\u5173\u8054\u6570\u636e\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u201cUser\u201d\u6a21\u578b\u548c\u4e00\u4e2a\u201cPost\u201d\u6a21\u578b\uff0c\u60a8\u53ef\u4ee5\u5728\u201cUser\u201d\u6a21\u578b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u4e00\u5bf9\u591a\u5173\u7cfb\uff0c\u5e76\u8f7b\u677e\u5730\u8bbf\u95ee\u7528\u6237\u521b\u5efa\u7684\u6240\u6709\u5e16\u5b50\uff1a<\/p>\n<pre>class User extends Model\n{\n    \/**\n     * Get the posts for the user.\n     *\/\n    public function posts()\n    {\n        return $this-&amp;gt;hasMany(Post::class);\n    }\n}\n\n$user = User::find(1);\n\nforeach ($user-&amp;gt;posts as $post) {\n    echo $post-&amp;gt;title;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8be5\u4ee3\u7801\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u4e00\u4e2a\u7528\u6237\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u7528\u6237\u7684\u201cposts\u201d\u5c5e\u6027\u8bbf\u95ee\u8be5\u7528\u6237\u521b\u5efa\u7684\u6240\u6709\u5e16\u5b50\u3002\u53ef\u4ee5\u5728\u201cPost\u201d\u6a21\u578b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u53cd\u5411\u5173\u7cfb\uff0c\u4ee5\u4fbf\u5728\u8bbf\u95ee\u5e16\u5b50\u65f6\u8f7b\u677e\u5730\u8bbf\u95ee\u5176\u521b\u5efa\u7528\u6237\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Laravel\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u5de5\u5177\u548c\u529f\u80fd\uff0c\u7528\u4e8e\u7b80\u5316\u6570\u636e\u5e93\u4fee\u6539\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528\u8fc1\u79fb\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u4fee\u6539\u8868\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5217\u3001\u66f4\u6539\u7d22\u5f15\u548c\u7ea6\u675f\u7b49\u3002\u4f7f\u7528\u6570\u636e\u586b\u5145\u53ef\u7b80\u5316\u6d4b\u8bd5\u548c\u6f14\u793a\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u5728\u6a21\u578b\u4e4b\u95f4\u521b\u5efa\u5173\u8054\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u6570\u636e\u5e93\u4e2d\u7684\u76f8\u5173\u6570\u636e\u3002\u5728\u60a8\u5f00\u59cb\u8fdb\u884c\u6570\u636e\u5e93\u4fee\u6539\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u5907\u4efd\u91cd\u8981\u7684\u6570\u636e\uff0c\u5e76\u5c0f\u5fc3\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u6846\u67b6\u6570\u636e\u5e93\u4fee\u6539\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>laravel\u662f\u4e00\u6b3e\u975e\u5e38\u53d7\u6b22\u8fce\u7684php web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u4fee\u6539\u7684\u8fc7\u7a0b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8laravel\u6846\u67b6\u7684\u6570\u636e\u5e93\u4fee\u6539\u76f8\u5173\u5185\u5bb9\u3002 \u6570\u636e\u5e93\u8fc1\u79fb \u5728Laravel\u6846\u67b6\u4e2d\uff0c\u6570\u636e\u5e93\u8fc1\u79fb\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u5e93\u7684\u7ed3\u6784\u4ece\u4e00\u79cd\u7248\u672c\u8f6c\u79fb\u5230\u53e6\u4e00\u79cd\u7248\u672c\u3002\u901a\u8fc7\u4f7f\u7528\u8fc1\u79fb\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u4fee\u6539\u8868\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5217\u3001\u66f4\u6539\u7d22\u5f15\u548c\u7ea6\u675f\u7b49\u3002\u8fc1\u79fb\u6587\u4ef6\u5b58\u50a8\u5728\u201cdatabase\/migrations\u201d\u76ee\u5f55\u4e0b\u3002\u6bcf\u4e2a\u8fc1\u79fb\u6587\u4ef6\u4ee3\u8868\u4e00\u4e2a\u6570\u636e\u5e93\u8fc1\u79fb\u64cd\u4f5c\uff0c\u5e76\u5305\u542b\u7528\u4e8e\u4fee\u6539\u6570\u636e\u5e93\u7684\u201cup\u201d\u548c\u201cdown\u201d\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd0\u884c\u201cphp artisan migrate\u201d\u547d\u4ee4\uff0c\u5c06\u81ea\u52a8\u6267\u884c\u6240\u6709\u672a\u6267\u884c\u7684\u8fc1\u79fb\u64cd\u4f5c\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8fc1\u79fb\u6587\u4ef6\uff0c\u7528\u4e8e\u521b\u5efa\u201cusers\u201d\u8868\uff1a use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateUsersTable extends Migration { \/** * Run the migrations. * * @return void *\/ public function up() { Schema::create(&#8216;users&#8217;, function (Blueprint $table) { $table-&amp;gt;increments(&#8216;id&#8217;); $table-&amp;gt;string(&#8216;name&#8217;); $table-&amp;gt;string(&#8217;email&#8217;)-&amp;gt;unique(); $table-&amp;gt;string(&#8216;password&#8217;); $table-&amp;gt;rememberToken(); $table-&amp;gt;timestamps(); }); } \/** * Reverse the migrations. * * @return void *\/ [&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-22568","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22568","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=22568"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22568\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}