Add DiscoverCategory model

This commit is contained in:
Daniel Supernault 2019-02-07 00:34:32 -07:00
parent f4a6ccda90
commit 32d1667311
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 109 additions and 0 deletions

58
app/DiscoverCategory.php Normal file
View file

@ -0,0 +1,58 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\{Status, StatusHashtag};
class DiscoverCategory extends Model
{
protected $fillable = ['slug'];
public function media()
{
return $this->belongsTo(Media::class);
}
public function url()
{
return url('/discover/c/'.$this->slug);
}
public function editUrl()
{
return url('/i/admin/discover/category/edit/' . $this->id);
}
public function thumb()
{
return $this->media->thumb();
}
public function items()
{
return $this->hasMany(DiscoverCategoryHashtag::class, 'discover_category_id');
}
public function hashtags()
{
return $this->hasManyThrough(
Hashtag::class,
DiscoverCategoryHashtag::class,
'discover_category_id',
'id',
'id',
'hashtag_id'
);
}
public function posts()
{
return Status::select('*')
->join('status_hashtags', 'statuses.id', '=', 'status_hashtags.status_id')
->join('hashtags', 'status_hashtags.hashtag_id', '=', 'hashtags.id')
->join('discover_category_hashtags', 'hashtags.id', '=', 'discover_category_hashtags.hashtag_id')
->join('discover_categories', 'discover_category_hashtags.discover_category_id', '=', 'discover_categories.id')
->where('discover_categories.id', $this->id);
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DiscoverCategoryController extends Controller
{
//
}

View file

@ -0,0 +1,41 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDiscoverCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('discover_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('slug')->unique()->index();
$table->boolean('active')->default(false)->index();
$table->tinyInteger('order')->unsigned()->default(5);
$table->bigInteger('media_id')->unsigned()->unique()->nullable();
$table->boolean('no_nsfw')->default(true);
$table->boolean('local_only')->default(true);
$table->boolean('public_only')->default(true);
$table->boolean('photos_only')->default(true);
$table->timestamp('active_until')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('discover_categories');
}
}