From 8bd30d03019a1b6cf3ad3546864c176021d2997f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Tue, 20 Sep 2016 01:04:23 +0200 Subject: [PATCH] FEATURE: add number of views in page --- app/Http/Controllers/Visitor/ContentPage.php | 3 ++ app/Models/Content.php | 8 +++++ ...16_09_19_225527_AddNumberOfViewsOfPage.php | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 database/migrations/2016_09_19_225527_AddNumberOfViewsOfPage.php diff --git a/app/Http/Controllers/Visitor/ContentPage.php b/app/Http/Controllers/Visitor/ContentPage.php index 6ee4ac70..1ea86abb 100644 --- a/app/Http/Controllers/Visitor/ContentPage.php +++ b/app/Http/Controllers/Visitor/ContentPage.php @@ -50,6 +50,9 @@ class ContentPage extends Controller throw new \Exception('Cannot access protected resources'); } + // add one new "view" for page + $page->view(); + // get handler $handler = new $page->module_handler; diff --git a/app/Models/Content.php b/app/Models/Content.php index 8d13ee10..19128cfe 100644 --- a/app/Models/Content.php +++ b/app/Models/Content.php @@ -435,4 +435,12 @@ class Content extends Model // save history ContentHistory::create($history); } + + public function view() + { + $this->timestamps = false; + $this->viewed ++; + $this->save(); + $this->timestamps = true; + } } diff --git a/database/migrations/2016_09_19_225527_AddNumberOfViewsOfPage.php b/database/migrations/2016_09_19_225527_AddNumberOfViewsOfPage.php new file mode 100644 index 00000000..d4061908 --- /dev/null +++ b/database/migrations/2016_09_19_225527_AddNumberOfViewsOfPage.php @@ -0,0 +1,34 @@ +integer('viewed') + ->comment('How many times has been this content page displayed?') + ->after('updated_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('contents', function (Blueprint $table) { + $table->dropColumn('viewed'); + }); + } +}