Add hashtag model/migration and pivot table

This commit is contained in:
Daniel Supernault 2018-05-19 20:57:55 -06:00
parent 92c0ef8a74
commit d8303e7de5
3 changed files with 97 additions and 0 deletions

28
app/Hashtag.php Normal file
View file

@ -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;
}
}

View file

@ -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');
}
}

View file

@ -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');
}
}