1
0
Эх сурвалжийг харах

Add hashtag model/migration and pivot table

Daniel Supernault 7 жил өмнө
parent
commit
d8303e7de5

+ 28 - 0
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;
+    }
+ 
+}

+ 35 - 0
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');
+    }
+}

+ 34 - 0
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');
+    }
+}