Ejemplo n.º 1
0
 /**
  * Show people who has liked a post
  *
  * @param \App\Eloquent\Post $post
  * @param \App\Eloquent\User $viewer
  * @return string
  */
 public function showPostLikes($post = null, $viewer = null)
 {
     //No likes
     if ($post->likes()->count() == 0) {
         //We return blank
         return '';
     }
     //return the string of like presentation
     return '<span>' . $post->getLikesAsHumanReadable($viewer) . '</span>';
 }
Ejemplo n.º 2
0
 /**
  * Returns the feeds for a user
  *
  * @param \App\Eloquent\User $user
  * @param int $numberPerPage
  * @return \Illuminate\Pagination\LengthAwarePaginator|null
  */
 public function feeds($user = null, $numberPerPage = 3)
 {
     try {
         //Get CI super object
         $ci =& get_instance();
         //Calculate page
         $page = $ci->input->get('page') !== false ? $ci->input->get('page') : 1;
         //Count the posts
         $postsCount = $this->post->with(['user'])->whereHas('user', function ($query) use($user) {
             $query->whereIn('id', $user->friends()->lists('users.id')->merge([$user->id])->all());
         })->count();
         //No posts found
         if ($postsCount == 0) {
             //Return null
             return null;
         }
         //Limiting posts
         $limitsPosts = $this->post->with(['user'])->whereHas('user', function ($query) use($user) {
             $query->whereIn('id', $user->friends()->lists('users.id')->merge([$user->id])->all());
         })->skip($numberPerPage * ($page - 1))->take($numberPerPage)->orderBy('created_at', 'desc')->get();
         //No posts on this page
         if ($limitsPosts->count() == 0) {
             //Return null
             return null;
         }
         //Return paginator
         return new LengthAwarePaginator($limitsPosts, $postsCount, $numberPerPage, $page, ['path' => current_url()]);
     } catch (Exception $e) {
         //Unexpected error
         return null;
     }
 }
 /**
  * Seeding likes on a post
  */
 public function seedLikesOnPosts()
 {
     //Unguard model
     Model::unguard();
     $totalGenerated = 0;
     foreach (Post::all() as $post) {
         $users = User::orderByRaw('RAND()')->limit(mt_rand(1, 30))->get();
         //Create a new like on post by the user
         foreach ($users as $user) {
             $randDate = $this->faker->dateTimeBetween('-1years', 'now');
             $post->likes()->create(['user_id' => $user->id, 'created_at' => $randDate, 'updated_at' => $randDate]);
         }
         $totalGenerated += $users->count();
     }
     echo $totalGenerated . ' likes created on posts';
     Model::reguard();
 }