{"id":24804,"date":"2024-11-21T12:53:38","date_gmt":"2024-11-21T04:53:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24804\/"},"modified":"2024-11-21T12:53:38","modified_gmt":"2024-11-21T04:53:38","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%88%bf%e4%ba%a7%e5%b9%b3%e5%8f%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24804\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169889105694669.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\uff0c\u623f\u5730\u4ea7\u884c\u4e1a\u4e5f\u9010\u6e10\u5411\u5728\u7ebf\u5e73\u53f0\u8f6c\u578b\u3002\u5728\u5f00\u53d1\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\u65f6\uff0cLaravel\u6210\u4e3a\u4e86\u8bb8\u591a\u5f00\u53d1\u8005\u7684\u9996\u9009\u6846\u67b6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5Laravel<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5Laravel\u3002\u53ef\u4ee5\u901a\u8fc7Composer\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>composer create-project --prefer-dist laravel\/laravel property-platform<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aproperty-platform\u7684\u9879\u76ee\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u66f4\u6539\u9879\u76ee\u540d\u79f0\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u542f\u52a8\u670d\u52a1\uff1a<\/p>\n<pre>cd property-platform\nphp artisan serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u5e76\u5728\u9879\u76ee\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u6253\u5f00.env\u6587\u4ef6\uff0c\u4fee\u6539\u4ee5\u4e0b\u90e8\u5206\uff1a<\/p>\n<pre>DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=property_platform\nDB_USERNAME=root\nDB_PASSWORD=<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cDB_DATABASE\u3001DB_USERNAME\u3001DB_PASSWORD\u4e3a\u81ea\u5df1\u7684\u6570\u636e\u5e93\u4fe1\u606f\u3002 <\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aproperty_platform\u7684\u6570\u636e\u5e93\uff1a<\/p>\n<pre>CREATE DATABASE property_platform;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u7740\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u623f\u4ea7\u4fe1\u606f\u8868\u3002\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:migration create_properties_table --create=properties<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u6253\u5f00\u8fc1\u79fb\u6587\u4ef6\uff0c\u5728up\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u8868\u7ed3\u6784\uff1a<\/p>\n<pre>public function up()\n{\n    Schema::create('properties', function (Blueprint $table) {\n        $table-&amp;gt;increments('id');\n        $table-&amp;gt;string('title');\n        $table-&amp;gt;text('description');\n        $table-&amp;gt;string('address');\n        $table-&amp;gt;integer('price');\n        $table-&amp;gt;timestamps();\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6267\u884c\u8fc1\u79fb\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u6a21\u578b\u548c\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u623f\u4ea7\u4fe1\u606f\u7684\u6a21\u578b\u548c\u5bf9\u5e94\u7684\u63a7\u5236\u5668\u3002\u5728app\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aProperty\u7684\u6a21\u578b\uff1a<\/p>\n<pre>php artisan make:model Property<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u5728app\/Http\/Controllers\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aPropertyController\u7684\u63a7\u5236\u5668\uff1a<\/p>\n<pre>php artisan make:controller PropertyController --resource<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6211\u4eec\u4f7f\u7528\u4e86&#8211;resource\u9009\u9879\u6765\u751f\u6210\u63a7\u5236\u5668\uff0c\u5e76\u4e14Laravel\u5c06\u81ea\u52a8\u751f\u6210RESTful\u98ce\u683c\u7684\u8def\u7531\u548c\u76f8\u5e94\u7684\u65b9\u6cd5\u3002\u6253\u5f00\u63a7\u5236\u5668\u6587\u4ef6\uff0c\u5728index\u65b9\u6cd5\u4e2d\u67e5\u8be2\u6240\u6709\u623f\u4ea7\u4fe1\u606f\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u89c6\u56fe\u6587\u4ef6\uff1a<\/p>\n<pre>public function index()\n{\n    $properties = Property::all();\n    return view('properties.index', compact('properties'));\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u89c6\u56fe\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u521b\u5efa\u89c6\u56fe\u6587\u4ef6\u6765\u6e32\u67d3\u9875\u9762\u3002\u5728resources\/views\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aproperties\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.blade.php\u7684\u6a21\u677f\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u623f\u4ea7\u4fe1\u606f\uff0c\u5e76\u663e\u793a\u5728\u9875\u9762\u4e0a\uff1a<\/p>\n<pre>@foreach($properties as $property)\n    &lt;div class=\"property\"&gt;\n        &lt;h2&gt;{{ $property-&amp;gt;title }}&lt;\/h2&gt;\n        &lt;p&gt;{{ $property-&amp;gt;description }}&lt;\/p&gt;\n        &lt;p&gt;{{ $property-&amp;gt;price }}&lt;\/p&gt;\n        &lt;p&gt;{{ $property-&amp;gt;address }}&lt;\/p&gt;\n    &lt;\/div&gt;\n@endforeach<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u8868\u5355\u548c\u63a7\u5236\u5668\u65b9\u6cd5<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u6dfb\u52a0\u623f\u4ea7\u4fe1\u606f\u7684\u8868\u5355\u548c\u5bf9\u5e94\u7684\u63a7\u5236\u5668\u65b9\u6cd5\u3002\u5728resources\/views\/properties\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3acreate.blade.php\u7684\u8868\u5355\u6587\u4ef6\uff1a<\/p>\n<pre><\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    {{ csrf_field() }}  \u6807\u9898\uff1a    \u63cf\u8ff0\uff1a    \u5730\u5740\uff1a    \u4ef7\u683c\uff1a    \u6dfb\u52a0   <\/p>\n<p>\u5728PropertyController\u4e2d\u6dfb\u52a0create\u548cstore\u65b9\u6cd5\uff1a<\/p>\n<pre>public function create()\n{\n    return view('properties.create');\n}\n\npublic function store(Request $request)\n{\n    $property = new Property;\n    $property-&amp;gt;title = $request-&amp;gt;title;\n    $property-&amp;gt;description = $request-&amp;gt;description;\n    $property-&amp;gt;address = $request-&amp;gt;address;\n    $property-&amp;gt;price = $request-&amp;gt;price;\n    $property-&amp;gt;save();\n\n    return redirect('\/properties');\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>create\u65b9\u6cd5\u6e32\u67d3\u8868\u5355\u9875\u9762\uff0cstore\u65b9\u6cd5\u63a5\u6536\u8868\u5355\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u4fdd\u5b58\u81f3\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<ol>\n<li>\u8bbe\u7f6e\u8def\u7531<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u8def\u7531\u6765\u5c06URL\u4e0e\u63a7\u5236\u5668\u65b9\u6cd5\u7ed1\u5b9a\u3002\u6253\u5f00routes\/web.php\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u8def\u7531\uff1a<\/p>\n<pre>Route::get('\/properties', 'PropertyController@index');\nRoute::get('\/properties\/create', 'PropertyController@create');\nRoute::post('\/properties', 'PropertyController@store');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8fd0\u884c\u5e94\u7528<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\u5e94\u7528\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8\u670d\u52a1\uff1a<\/p>\n<pre>php artisan serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:8000\/properties\u5373\u53ef\u67e5\u770b\u6240\u6709\u623f\u4ea7\u4fe1\u606f\u3002\u70b9\u51fb\u201c\u6dfb\u52a0\u623f\u4ea7\u201d\u6309\u94ae\u8df3\u8f6c\u81f3\u6dfb\u52a0\u623f\u4ea7\u4fe1\u606f\u9875\u9762\uff0c\u586b\u5199\u4fe1\u606f\u540e\u70b9\u51fb\u201c\u6dfb\u52a0\u201d\u6309\u94ae\u5373\u53ef\u4fdd\u5b58\u623f\u4ea7\u4fe1\u606f\u81f3\u6570\u636e\u5e93\u3002<\/p>\n<ol>\n<li>\u5c0f\u7ed3<\/li>\n<\/ol>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\uff0c\u5305\u62ec\u5b89\u88c5Laravel\u3001\u521b\u5efa\u6570\u636e\u5e93\u3001\u521b\u5efa\u6a21\u578b\u548c\u63a7\u5236\u5668\u3001\u521b\u5efa\u89c6\u56fe\u6587\u4ef6\u3001\u521b\u5efa\u8868\u5355\u548c\u63a7\u5236\u5668\u65b9\u6cd5\u4ee5\u53ca\u8bbe\u7f6e\u8def\u7531\uff0c\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3Laravel\u5728\u5f00\u53d1\u5728\u7ebf\u5e73\u53f0\u5e94\u7528\u4e2d\u7684\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\u53ca\u7528\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u5e94\u7528\u5230\u5176\u4ed6\u7c7b\u4f3c\u7684\u5e94\u7528\u5f00\u53d1\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\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\u623f\u4ea7\u5e73\u53f0 \u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\uff0c\u623f\u5730\u4ea7\u884c\u4e1a\u4e5f\u9010\u6e10\u5411\u5728\u7ebf\u5e73\u53f0\u8f6c\u578b\u3002\u5728\u5f00\u53d1\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\u65f6\uff0cLaravel\u6210\u4e3a\u4e86\u8bb8\u591a\u5f00\u53d1\u8005\u7684\u9996\u9009\u6846\u67b6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u623f\u4ea7\u5e73\u53f0\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u5b89\u88c5Laravel \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5Laravel\u3002\u53ef\u4ee5\u901a\u8fc7Composer\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u4e0b\u6240\u793a\uff1a composer create-project &#8211;prefer-dist laravel\/laravel property-platform \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aproperty-platform\u7684\u9879\u76ee\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u66f4\u6539\u9879\u76ee\u540d\u79f0\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u542f\u52a8\u670d\u52a1\uff1a cd property-platform php artisan serve \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u6570\u636e\u5e93 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u5e76\u5728\u9879\u76ee\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u6253\u5f00.env\u6587\u4ef6\uff0c\u4fee\u6539\u4ee5\u4e0b\u90e8\u5206\uff1a DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=property_platform DB_USERNAME=root DB_PASSWORD= \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cDB_DATABASE\u3001DB_USERNAME\u3001DB_PASSWORD\u4e3a\u81ea\u5df1\u7684\u6570\u636e\u5e93\u4fe1\u606f\u3002 \u521b\u5efa\u4e00\u4e2a\u540d\u4e3aproperty_platform\u7684\u6570\u636e\u5e93\uff1a CREATE DATABASE property_platform; \u767b\u5f55\u540e\u590d\u5236 \u63a5\u7740\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u623f\u4ea7\u4fe1\u606f\u8868\u3002\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\u6587\u4ef6\uff1a php artisan make:migration create_properties_table &#8211;create=properties \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u6253\u5f00\u8fc1\u79fb\u6587\u4ef6\uff0c\u5728up\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u8868\u7ed3\u6784\uff1a public function up() { Schema::create(&#8216;properties&#8217;, function (Blueprint $table) { $table-&amp;gt;increments(&#8216;id&#8217;); $table-&amp;gt;string(&#8216;title&#8217;); $table-&amp;gt;text(&#8216;description&#8217;); $table-&amp;gt;string(&#8216;address&#8217;); $table-&amp;gt;integer(&#8216;price&#8217;); $table-&amp;gt;timestamps(); [&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-24804","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24804","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=24804"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24804\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}