Permalink
Browse files

Episode 39

  • Loading branch information...
JeffreyWay committed May 17, 2017
1 parent 8e48764 commit d6b780072a7aa47ac11889db9f485f1a789aea80
@@ -11,7 +11,7 @@ class ThreadFilters extends Filters
*
* @var array
*/
protected $filters = ['by', 'popular'];
protected $filters = ['by', 'popular', 'unanswered'];
/**
* Filter the query by a given username.
@@ -37,4 +37,14 @@ protected function popular()
return $this->builder->orderBy('replies_count', 'desc');
}
/**
* Filter the query according to those that are unanswered.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function unanswered()
{
return $this->builder->where('replies_count', 0);
}
}
@@ -29,6 +29,22 @@ class Reply extends Model
*/
protected $appends = ['favoritesCount', 'isFavorited'];
/**
* Boot the reply instance.
*/
protected static function boot()
{
parent::boot();
static::created(function ($reply) {
$reply->thread->increment('replies_count');
});
static::deleted(function ($reply) {
$reply->thread->decrement('replies_count');
});
}
/**
* A reply has an owner.
*
@@ -31,10 +31,6 @@ protected static function boot()
{
parent::boot();
static::addGlobalScope('replyCount', function ($builder) {
$builder->withCount('replies');
});
static::deleting(function ($thread) {
$thread->replies->each->delete();
});
@@ -84,7 +80,6 @@ public function replies()
* Add a reply to the thread.
*
* @param array $reply
* @return Reply
* @return Model
*/
public function addReply($reply)
@@ -17,6 +17,7 @@ public function up()
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('channel_id');
$table->unsignedInteger('replies_count')->default(0);
$table->string('title');
$table->text('body');
$table->timestamps();
Oops, something went wrong.

0 comments on commit d6b7800

Please sign in to comment.