Skip to content

Commit

Permalink
Added views to post model.
Browse files Browse the repository at this point in the history
  • Loading branch information
ipaliakou committed Mar 2, 2020
1 parent 8f6b397 commit f0c730b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
34 changes: 34 additions & 0 deletions migrations/2020_03_02_164516_add_views_to_posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddViewsToPosts extends Migration
{
const TABLE = 'blog_etc_posts';

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table(self::TABLE, function (Blueprint $table) {
$table->unsignedInteger('views')->default(0)->after('user_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(self::TABLE, function (Blueprint $table) {
$table->dropColumn('views');
});
}
}
9 changes: 9 additions & 0 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,13 @@ public function post_body_output(): string
{
return $this->renderBody();
}

/**
* Increments amount of post views.
*/
public function gotViewed()
{
$this->increment('views');
$this->save();
}
}

0 comments on commit f0c730b

Please sign in to comment.