{"id":24873,"date":"2024-11-21T12:56:46","date_gmt":"2024-11-21T04:56:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24873\/"},"modified":"2024-11-21T12:56:46","modified_gmt":"2024-11-21T04:56:46","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%e9%a2%84%e8%ae%a2%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24873\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169898177711009.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\u548c\u53d1\u5c55\uff0c\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u8d8a\u6765\u8d8a\u53d7\u6b22\u8fce\u3002\u65e0\u8bba\u662f\u9152\u5e97\u3001\u673a\u7968\u8fd8\u662f\u9910\u9986\uff0c\u6d88\u8d39\u8005\u90fd\u5e0c\u671b\u80fd\u591f\u901a\u8fc7\u4e92\u8054\u7f51\u65b9\u4fbf\u5730\u8fdb\u884c\u9884\u8ba2\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u6846\u67b6\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u3002<\/p>\n<ol>\n<li>\u73af\u5883\u642d\u5efa<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u3001Composer\u548cLaravel\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Laravel\u9879\u76ee\uff1a<\/p>\n<pre>composer create-project --prefer-dist laravel\/laravel booking-system<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u81ea\u52a8\u5b89\u88c5Laravel\u6846\u67b6\u53ca\u5176\u4f9d\u8d56\u3002<\/p>\n<ol>\n<li>\u6570\u636e\u5e93\u8bbe\u8ba1<\/li>\n<\/ol>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u9884\u8ba2\u7cfb\u7edf\uff0c\u5176\u4e2d\u5305\u542b\u4e24\u4e2a\u5173\u952e\u5b9e\u4f53\uff1a\u7528\u6237\u548c\u9884\u8ba2\u3002\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e24\u4e2a\u6570\u636e\u5e93\u8868\u6765\u5b58\u50a8\u8fd9\u4e9b\u5b9e\u4f53\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6253\u5f00\u9879\u76ee\u4e2d\u7684 .env \u6587\u4ef6\uff0c\u5e76\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:migration create_users_table --create=users\nphp artisan make:migration create_bookings_table --create=bookings<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5206\u522b\u8fd0\u884c\u8fd9\u4e24\u4e2a\u547d\u4ee4\u5c06\u521b\u5efa\u4e24\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u6253\u5f00\u8fd9\u4e9b\u6587\u4ef6\u5e76\u5b9a\u4e49\u8868\u7684\u7ed3\u6784\u3002<\/p>\n<p>\u5728 create_users_table \u8fc1\u79fb\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>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}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 create_bookings_table \u8fc1\u79fb\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>public function up()\n{\n    Schema::create('bookings', function (Blueprint $table) {\n        $table-&amp;gt;increments('id');\n        $table-&amp;gt;integer('user_id')-&amp;gt;unsigned();\n        $table-&amp;gt;dateTime('start_date');\n        $table-&amp;gt;dateTime('end_date');\n        $table-&amp;gt;timestamps();\n\n        $table-&amp;gt;foreign('user_id')-&amp;gt;references('id')-&amp;gt;on('users');\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6267\u884c\u8fc1\u79fb\u5e76\u521b\u5efa\u6570\u636e\u5e93\u8868\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u6a21\u578b\u4e0e\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u6a21\u578b\u548c\u5b9a\u4e49\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efaUser\u548cBooking\u6a21\u578b\uff1a<\/p>\n<pre>php artisan make:model User\nphp artisan make:model Booking<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 User \u6a21\u578b\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>public function bookings()\n{\n    return $this-&amp;gt;hasMany('AppBooking');\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 Booking \u6a21\u578b\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>public function user()\n{\n    return $this-&amp;gt;belongsTo('AppUser');\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8def\u7531\u548c\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u8def\u7531\u548c\u76f8\u5e94\u7684\u63a7\u5236\u5668\u65b9\u6cd5\u6765\u5904\u7406\u9884\u8ba2\u7cfb\u7edf\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u6253\u5f00 routes\/web.php \u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>Route::get('\/bookings', 'BookingController@index');\nRoute::post('\/bookings', 'BookingController@store');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a BookingController \u63a7\u5236\u5668\uff1a<\/p>\n<pre>php artisan make:controller BookingController<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 BookingController \u63a7\u5236\u5668\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>use AppBooking;\nuse IlluminateHttpRequest;\n\nclass BookingController extends Controller\n{\n    public function index()\n    {\n        $bookings = Booking::with('user')-&amp;gt;get();\n        return view('bookings.index', compact('bookings'));\n    }\n\n    public function store(Request $request)\n    {\n        \/\/ \u9a8c\u8bc1\u9884\u8ba2\u8bf7\u6c42\u7684\u8f93\u5165\u53c2\u6570\n\n        $booking = new Booking;\n        $booking-&amp;gt;start_date = $request-&amp;gt;start_date;\n        $booking-&amp;gt;end_date = $request-&amp;gt;end_date;\n        $booking-&amp;gt;user_id = $request-&amp;gt;user_id;\n        $booking-&amp;gt;save();\n\n        return redirect()-&amp;gt;back()-&amp;gt;with('success', '\u9884\u8ba2\u6210\u529f');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u89c6\u56fe<\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u89c6\u56fe\u6587\u4ef6\u6765\u663e\u793a\u9884\u8ba2\u5217\u8868\u548c\u9884\u8ba2\u8868\u5355\u3002<\/p>\n<p>\u5728 resources\/views \u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a bookings \u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u4ee5\u4e0b\u4e24\u4e2a\u89c6\u56fe\u6587\u4ef6\u3002<\/p>\n<p>index.blade.php \u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<pre>@foreach ($bookings as $booking)\n    &lt;p&gt;\u7528\u6237: {{ $booking-&amp;gt;user-&amp;gt;name }}&lt;\/p&gt;\n    &lt;p&gt;\u5f00\u59cb\u65e5\u671f: {{ $booking-&amp;gt;start_date }}&lt;\/p&gt;\n    &lt;p&gt;\u7ed3\u675f\u65e5\u671f: {{ $booking-&amp;gt;end_date }}&lt;\/p&gt;\n@endforeach<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>create.blade.php \u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<pre><\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    @csrf \u5f00\u59cb\u65e5\u671f  \u7ed3\u675f\u65e5\u671f  \u7528\u6237ID  \u9884\u8ba2  @if (session(&#8216;success&#8217;)) <\/p>\n<p>{{ session(&#8216;success&#8217;) }}<\/p>\n<p> @endif <\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u7684\u5f00\u53d1\u3002\u901a\u8fc7\u8fd9\u4e2a\u7cfb\u7edf\uff0c\u7528\u6237\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7684\u9884\u8ba2\u5217\u8868\uff0c\u5e76\u8fdb\u884c\u65b0\u7684\u9884\u8ba2\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5982\u7528\u6237\u8ba4\u8bc1\u3001\u641c\u7d22\u7b49\u3002\u4f46\u662f\u8fd9\u4e2a\u793a\u4f8b\u5e94\u8be5\u4e3a\u4f60\u63d0\u4f9b\u4e86\u4e00\u4e2a\u826f\u597d\u7684\u8d77\u70b9\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u5bf9\u4e8e\u6b63\u5728\u5b66\u4e60Laravel\u6846\u67b6\u548c\u5f00\u53d1\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u7684\u8bfb\u8005\u6709\u6240\u5e2e\u52a9\u3002\u795d\u4f60\u6210\u529f\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u9884\u8ba2\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\u9884\u8ba2\u7cfb\u7edf \u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\u548c\u53d1\u5c55\uff0c\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u8d8a\u6765\u8d8a\u53d7\u6b22\u8fce\u3002\u65e0\u8bba\u662f\u9152\u5e97\u3001\u673a\u7968\u8fd8\u662f\u9910\u9986\uff0c\u6d88\u8d39\u8005\u90fd\u5e0c\u671b\u80fd\u591f\u901a\u8fc7\u4e92\u8054\u7f51\u65b9\u4fbf\u5730\u8fdb\u884c\u9884\u8ba2\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u6846\u67b6\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u9884\u8ba2\u7cfb\u7edf\u3002 \u73af\u5883\u642d\u5efa \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u3001Composer\u548cLaravel\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Laravel\u9879\u76ee\uff1a composer create-project &#8211;prefer-dist laravel\/laravel booking-system \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u81ea\u52a8\u5b89\u88c5Laravel\u6846\u67b6\u53ca\u5176\u4f9d\u8d56\u3002 \u6570\u636e\u5e93\u8bbe\u8ba1 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u9884\u8ba2\u7cfb\u7edf\uff0c\u5176\u4e2d\u5305\u542b\u4e24\u4e2a\u5173\u952e\u5b9e\u4f53\uff1a\u7528\u6237\u548c\u9884\u8ba2\u3002\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e24\u4e2a\u6570\u636e\u5e93\u8868\u6765\u5b58\u50a8\u8fd9\u4e9b\u5b9e\u4f53\u7684\u4fe1\u606f\u3002 \u9996\u5148\uff0c\u6253\u5f00\u9879\u76ee\u4e2d\u7684 .env \u6587\u4ef6\uff0c\u5e76\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\uff1a php artisan make:migration create_users_table &#8211;create=users php artisan make:migration create_bookings_table &#8211;create=bookings \u767b\u5f55\u540e\u590d\u5236 \u5206\u522b\u8fd0\u884c\u8fd9\u4e24\u4e2a\u547d\u4ee4\u5c06\u521b\u5efa\u4e24\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u6253\u5f00\u8fd9\u4e9b\u6587\u4ef6\u5e76\u5b9a\u4e49\u8868\u7684\u7ed3\u6784\u3002 \u5728 create_users_table \u8fc1\u79fb\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a 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(); }); } \u767b\u5f55\u540e\u590d\u5236 \u5728 create_bookings_table \u8fc1\u79fb\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a public function up() [&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-24873","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24873","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=24873"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24873\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}