Skip to content

Instantly share code, notes, and snippets.

@gravitano
Created September 10, 2014 08:14
Show Gist options
  • Save gravitano/3d291f68c996045331ca to your computer and use it in GitHub Desktop.
Save gravitano/3d291f68c996045331ca to your computer and use it in GitHub Desktop.
<?php
trait VoteableTrait {
public function votes()
{
return $this->morphMany('Vote', 'voteable');
}
public function voted($id, $name = null)
{
$model = $name ?: $this->getClassName();
$data = $this->votes()->where(['voteable_id' => $id, 'voteable_type' => $model])->first();
return ! is_null($data);
}
public function getClassName()
{
return str_replace('\\', '', Str::studly(class_basename($this)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment