{"id":24914,"date":"2024-11-21T16:55:29","date_gmt":"2024-11-21T08:55:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24914\/"},"modified":"2024-11-21T16:55:29","modified_gmt":"2024-11-21T08:55:29","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8laravel%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e7%bb%9f%e8%ae%a1%e5%92%8c%e5%88%86%e6%9e%90%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24914\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169907095879062.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd<\/p>\n<p>Laravel\u662f\u4e00\u6b3e\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u6784\u5efa\u9ad8\u6548\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\uff0c\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u662f\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Laravel\u6846\u67b6\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6eLaravel<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6eLaravel\u6846\u67b6\u3002\u53ef\u4ee5\u901a\u8fc7Composer\u547d\u4ee4\u6765\u5b89\u88c5Laravel\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer global require laravel\/installer<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Laravel\u9879\u76ee\uff1a<\/p>\n<pre>laravel new data-analysis-app<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<pre>cd data-analysis-app\nphp artisan serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/localhost:8000\uff0c\u5982\u679c\u770b\u5230Laravel\u6b22\u8fce\u9875\u9762\uff0c\u8bf4\u660e\u5b89\u88c5\u548c\u914d\u7f6e\u6210\u529f\u3002<\/p>\n<p>\u4e8c\u3001\u521b\u5efa\u6570\u636e\u5e93\u548c\u6570\u636e\u8868<br \/>\u5728\u8fdb\u884c\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u4e4b\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u76f8\u5e94\u7684\u6570\u636e\u5e93\u548c\u6570\u636e\u8868\u3002\u53ef\u4ee5\u4f7f\u7528Laravel\u7684\u8fc1\u79fb\u529f\u80fd\u6765\u521b\u5efa\u6570\u636e\u8868\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:migration create_statistics_table --create=statistics<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fc1\u79fb\u6587\u4ef6\u5c06\u4f1a\u751f\u6210\u5728\u201cdatabase\/migrations\u201d\u76ee\u5f55\u4e0b\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u201cup\u201d\u65b9\u6cd5\u548c\u4e00\u4e2a\u201cdown\u201d\u65b9\u6cd5\u3002\u5728\u201cup\u201d\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6570\u636e\u8868\u7684\u5b57\u6bb5\u548c\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u201cstatistics\u201d\u6570\u636e\u8868\uff0c\u5305\u542b\u201cid\u201d\u3001 \u201cuser_id\u201d\u3001 \u201cpage_views\u201d\u548c\u201ccreated_at\u201d\u5b57\u6bb5\uff1a<\/p>\n<pre>use IlluminateDatabaseMigrationsMigration;\nuse IlluminateDatabaseSchemaBlueprint;\nuse IlluminateSupportFacadesSchema;\n\nclass CreateStatisticsTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('statistics', function (Blueprint $table) {\n            $table-&amp;gt;id();\n            $table-&amp;gt;unsignedBigInteger('user_id');\n            $table-&amp;gt;integer('page_views');\n            $table-&amp;gt;timestamps();\n        });\n    }\n\n    public function down()\n    {\n        Schema::dropIfExists('statistics');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fd0\u884c\u8fc1\u79fb\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cstatistics\u201d\u7684\u6570\u636e\u8868\u3002<\/p>\n<p>\u4e09\u3001\u521b\u5efa\u6a21\u578b\u548c\u63a7\u5236\u5668<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6a21\u578b\u6765\u64cd\u4f5c\u6570\u636e\u8868\u3002\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u6a21\u578b\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:model Statistic<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6a21\u578b\u6587\u4ef6\u5c06\u4f1a\u751f\u6210\u5728\u201capp\u201d\u76ee\u5f55\u4e0b\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u6a21\u578b\u6587\u4ef6\u4e2d\u5b9a\u4e49\u548c\u64cd\u4f5c\u6570\u636e\u8868\u7684\u5b57\u6bb5\u548c\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u201cUser\u201d\u5173\u8054\uff0c\u4e00\u4e2a\u201cgetTotalViews\u201d\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u603b\u6d4f\u89c8\u91cf\uff1a<\/p>\n<pre>namespace App;\n\nuse IlluminateDatabaseEloquentModel;\n\nclass Statistic extends Model\n{\n    public function user()\n    {\n        return $this-&amp;gt;belongsTo(User::class);\n    }\n\n    public static function getTotalViews()\n    {\n        return Statistic::sum('page_views');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u63a7\u5236\u5668\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:controller StatisticController<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a7\u5236\u5668\u6587\u4ef6\u5c06\u4f1a\u751f\u6210\u5728\u201capp\/Http\/Controllers\u201d\u76ee\u5f55\u4e0b\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u5728\u63a7\u5236\u5668\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\u7684\u67e5\u8be2\u548c\u5c55\u793a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u201cindex\u201d\u65b9\u6cd5\u6765\u663e\u793a\u603b\u6d4f\u89c8\u91cf\uff1a<\/p>\n<pre>namespace AppHttpControllers;\n\nuse AppStatistic;\n\nclass StatisticController extends Controller\n{\n    public function index()\n    {\n        $totalViews = Statistic::getTotalViews();\n\n        return view('statistics.index', compact('totalViews'));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u521b\u5efa\u8def\u7531\u548c\u89c6\u56fe<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8def\u7531\u6765\u6307\u5411\u63a7\u5236\u5668\u4e2d\u7684\u65b9\u6cd5\u3002\u5728\u201croutes\/web.php\u201d\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>use AppHttpControllersStatisticController;\n\nRoute::get('\/statistics', [StatisticController::class, 'index']);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95eehttp:\/\/localhost:8000\/statistics\uff0c\u5e94\u8be5\u53ef\u4ee5\u770b\u5230\u603b\u6d4f\u89c8\u91cf\u7684\u9875\u9762\u3002<\/p>\n<p>\u5728\u201cresources\/views\u201d\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cstatistics\u201d\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cindex.blade.php\u201d\u7684\u89c6\u56fe\u6587\u4ef6\u3002\u5728\u89c6\u56fe\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5c55\u793a\u603b\u6d4f\u89c8\u91cf\u7684\u6570\u636e\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790&lt;\/title&gt;&lt;h1&gt;\u603b\u6d4f\u89c8\u91cf\uff1a{{ $totalViews }}&lt;\/h1&gt;\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u603b\u7ed3<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528Laravel\u6846\u67b6\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528Laravel\u7684\u8fc1\u79fb\u3001\u6a21\u578b\u3001\u63a7\u5236\u5668\u548c\u89c6\u56fe\u7b49\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u636e\u5e93\u548c\u5c55\u793a\u6570\u636e\u3002\u5f53\u7136\uff0c\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u8fdb\u884c\u6570\u636e\u7684\u5904\u7406\u548c\u5206\u6790\uff0c\u4f8b\u5982\u4f7f\u7528Eloquent\u67e5\u8be2\u6784\u5efa\u5668\u548c\u805a\u5408\u51fd\u6570\u7b49\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f7f\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\u7684\u5f00\u53d1\u8005\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\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\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd Laravel\u662f\u4e00\u6b3e\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u6784\u5efa\u9ad8\u6548\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\uff0c\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u662f\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Laravel\u6846\u67b6\u5b9e\u73b0\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6eLaravel\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6eLaravel\u6846\u67b6\u3002\u53ef\u4ee5\u901a\u8fc7Composer\u547d\u4ee4\u6765\u5b89\u88c5Laravel\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer global require laravel\/installer \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Laravel\u9879\u76ee\uff1a laravel new data-analysis-app \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff1a cd data-analysis-app php artisan serve \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/localhost:8000\uff0c\u5982\u679c\u770b\u5230Laravel\u6b22\u8fce\u9875\u9762\uff0c\u8bf4\u660e\u5b89\u88c5\u548c\u914d\u7f6e\u6210\u529f\u3002 \u4e8c\u3001\u521b\u5efa\u6570\u636e\u5e93\u548c\u6570\u636e\u8868\u5728\u8fdb\u884c\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790\u4e4b\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u76f8\u5e94\u7684\u6570\u636e\u5e93\u548c\u6570\u636e\u8868\u3002\u53ef\u4ee5\u4f7f\u7528Laravel\u7684\u8fc1\u79fb\u529f\u80fd\u6765\u521b\u5efa\u6570\u636e\u8868\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff1a php artisan make:migration create_statistics_table &#8211;create=statistics \u767b\u5f55\u540e\u590d\u5236 \u8fc1\u79fb\u6587\u4ef6\u5c06\u4f1a\u751f\u6210\u5728\u201cdatabase\/migrations\u201d\u76ee\u5f55\u4e0b\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u201cup\u201d\u65b9\u6cd5\u548c\u4e00\u4e2a\u201cdown\u201d\u65b9\u6cd5\u3002\u5728\u201cup\u201d\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6570\u636e\u8868\u7684\u5b57\u6bb5\u548c\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u201cstatistics\u201d\u6570\u636e\u8868\uff0c\u5305\u542b\u201cid\u201d\u3001 \u201cuser_id\u201d\u3001 \u201cpage_views\u201d\u548c\u201ccreated_at\u201d\u5b57\u6bb5\uff1a use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateStatisticsTable extends Migration { public function up() { Schema::create(&#8216;statistics&#8217;, function (Blueprint $table) { $table-&amp;gt;id(); $table-&amp;gt;unsignedBigInteger(&#8216;user_id&#8217;); $table-&amp;gt;integer(&#8216;page_views&#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-24914","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24914","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=24914"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24914\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}