Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: vitaliyprokopov
  5.  * Date: 9/23/17
  6.  * Time: 1:56 PM
  7.  */
  8.  
  9. namespace App\Traits;
  10.  
  11.  
  12. use App\Category;
  13. use App\Comment;
  14. use App\Post;
  15.  
  16. trait Commentable
  17. {
  18.     private $type = null;
  19.  
  20.     /**
  21.      * Scope a query to only include popular users.
  22.      *
  23.      * @param \Illuminate\Database\Eloquent\Builder $query
  24.      * @return \Illuminate\Database\Eloquent\Builder
  25.      */
  26.     public function scopeComment($query)
  27.     {
  28.         if ($this instanceof Post) {
  29.             $this->type = Comment::TYPE_POST;
  30.         } else if ($this instanceof Category) {
  31.             $this->type = Comment::TYPE_CATEGORY;
  32.         }
  33.  
  34.         if ($this->type) {
  35.             return Comment::where('target_id',  $this->id)->where('target_type', $this->type)->orderBy('created_at', 'desc')->get();
  36.         }
  37.        
  38.         return null;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement