Film.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. class Film extends Eloquent {
  3. protected $table = "films";
  4. public function comments() {
  5. return $this->hasMany('Comment', 'film');
  6. }
  7. public function votes() {
  8. return $this->hasMany('Vote', 'film');
  9. }
  10. public function besitzer() {
  11. return $this->belongsTo('User', 'user');
  12. }
  13. public function scopeZuletztGesehen($query) {
  14. return $query->whereNotNull('gesehen')->orderBy('gesehen', 'DESC')->take(5);
  15. }
  16. public function scopeNeuesteVorschlage($query) {
  17. return $query->whereNull('gesehen')->orderBy('updated_at', 'DESC')->take(5);
  18. }
  19. public function scopeMeistgewunschteVorschlage($query) {
  20. $nextfilm = Dumbo::find(1)->film;
  21. return $query
  22. ->addSelect(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes'))
  23. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  24. ->whereNull('gesehen')
  25. ->where('films.id', "!=", is_object($nextfilm) ? $nextfilm->id : 0)
  26. ->groupBy('films.id')
  27. ->orderBy('upvotes', 'DESC')
  28. ->orderBy('vorgeschlagen', 'ASC')
  29. ->take(5);
  30. }
  31. public function getVotes() {
  32. return Vote::where('film', '=', $this->id)->count();
  33. }
  34. public function getUpvotes() {
  35. return Vote::where('film', '=', $this->id)->whereRaw('stimme IS TRUE')->count();
  36. }
  37. public function getBewertung() {
  38. if(!is_null($this->gesehen)) {
  39. $count = Comment::where('film', $this->id)->where('bewertung', '>', 0)->count();
  40. return $count > 0 ? Comment::where('film', $this->id)->where('bewertung', '>', 0)->sum('bewertung') / $count : 0;
  41. } else return (0);
  42. }
  43. }