From d8303e7de5d7b5c34cf522bf90a8dfa8f33b1ec1 Mon Sep 17 00:00:00 2001
From: Daniel Supernault <danielsupernault@gmail.com>
Date: Sat, 19 May 2018 20:57:55 -0600
Subject: [PATCH] Add hashtag model/migration and pivot table

---
 app/Hashtag.php                               | 28 +++++++++++++++
 ...018_05_06_214815_create_hashtags_table.php | 35 +++++++++++++++++++
 ...06_215006_create_status_hashtags_table.php | 34 ++++++++++++++++++
 3 files changed, 97 insertions(+)
 create mode 100644 app/Hashtag.php
 create mode 100644 database/migrations/2018_05_06_214815_create_hashtags_table.php
 create mode 100644 database/migrations/2018_05_06_215006_create_status_hashtags_table.php

diff --git a/app/Hashtag.php b/app/Hashtag.php
new file mode 100644
index 000000000..497bc94ee
--- /dev/null
+++ b/app/Hashtag.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Hashtag extends Model
+{
+    protected $fillable = ['name','slug'];
+
+    public function posts()
+    {
+      return $this->hasManyThrough(
+        Status::class,
+        StatusHashtag::class,
+        'hashtag_id',
+        'id',
+        'id',
+        'status_id'
+      );
+    }
+
+    public function url()
+    {
+      return config('routes.hashtag.base') . $this->slug;
+    }
+ 
+}
diff --git a/database/migrations/2018_05_06_214815_create_hashtags_table.php b/database/migrations/2018_05_06_214815_create_hashtags_table.php
new file mode 100644
index 000000000..d8a3497c9
--- /dev/null
+++ b/database/migrations/2018_05_06_214815_create_hashtags_table.php
@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateHashtagsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('hashtags', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('name')->unique()->index();
+            $table->string('slug')->unique()->index();
+            $table->boolean('is_nsfw')->default(false);
+            $table->boolean('is_banned')->default(false);
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('hashtags');
+    }
+}
diff --git a/database/migrations/2018_05_06_215006_create_status_hashtags_table.php b/database/migrations/2018_05_06_215006_create_status_hashtags_table.php
new file mode 100644
index 000000000..a4311d063
--- /dev/null
+++ b/database/migrations/2018_05_06_215006_create_status_hashtags_table.php
@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateStatusHashtagsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('status_hashtags', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->bigInteger('status_id')->unsigned()->index();
+            $table->bigInteger('hashtag_id')->unsigned()->index();
+            $table->unique(['status_id', 'hashtag_id']);
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('status_hashtags');
+    }
+}