Skip to content

Instantly share code, notes, and snippets.

@bcho
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save bcho/3095ce2a426a2dbd6809 to your computer and use it in GitHub Desktop.

Select an option

Save bcho/3095ce2a426a2dbd6809 to your computer and use it in GitHub Desktop.
<?php namespace Vtmer\Blog\Model;
trait PostAuthorTrait {
/**
* 文章作者关联
*
* @return \Illumniate\Database\Eloquent\Relations\BelongsTo
*/
public function author()
{
return $this->belongsTo('Vtmer\Blog\Model\User', 'author_id');
}
}
<?php namespace Vtmer\Blog\Model;
trait PostVisitTrait {
/**
* 增加文章访问量
*
* @return $this
*/
public function increasePageViews()
{
$this->increments($this->getPageViewsField());
return $this;
}
/**
* 获取访问量高的文章
*
* @return \Illumniate\Database\Query
*/
public function scopePopular($query)
{
$threshold = $this->getPopularThreshold();
return $query->where($this->getPageViewsField(), '>', $threshold);
}
abstract protected function getPageViewsField();
abstract protected function getPopularThreshold();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment