Skip to content

Commit f0c730b

Browse files
committed
Added views to post model.
1 parent 8f6b397 commit f0c730b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddViewsToPosts extends Migration
8+
{
9+
const TABLE = 'blog_etc_posts';
10+
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::table(self::TABLE, function (Blueprint $table) {
19+
$table->unsignedInteger('views')->default(0)->after('user_id');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table(self::TABLE, function (Blueprint $table) {
31+
$table->dropColumn('views');
32+
});
33+
}
34+
}

src/Models/Post.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,13 @@ public function post_body_output(): string
519519
{
520520
return $this->renderBody();
521521
}
522+
523+
/**
524+
* Increments amount of post views.
525+
*/
526+
public function gotViewed()
527+
{
528+
$this->increment('views');
529+
$this->save();
530+
}
522531
}

0 commit comments

Comments
 (0)