App\Repositories\PostRepository::all PHP 메소드

all() 공개 메소드

Get all posts.
public all ( integer $n = null, boolean $published = true ) : mixed
$n integer pagination
$published boolean whether fetch unpublished posts.
리턴 mixed
    public function all($n = null, $published = true)
    {
        $posts = $this->model->select('id', 'title', 'summary', 'comment_count', 'view_count', 'favorite_count', 'created_at', 'slug', 'published')->orderBy('created_at', 'desc');
        if ($published) {
            $posts = $posts->wherePublished(true);
        }
        return $n ? $posts->paginate($n) : $posts->get();
    }

Usage Example

 /**
  * Get all posts grouped by create date.
  *
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  */
 public function groupByDate()
 {
     $posts = $this->post->all();
     $group = [];
     foreach ($posts as $post) {
         $date = date_parse($post->created_at);
         $mYear = $date['year'];
         $mMonth = $date['month'];
         // Month or Year not exists current.
         if (!array_key_exists($mYear, $group)) {
             $group[$mYear] = [];
         }
         if (!array_key_exists($mMonth, $group[$mYear])) {
             $group[$mYear][$mMonth] = [];
         }
         array_push($group[$mYear][$mMonth], $post);
     }
     return view('front.archive.date', compact('group'));
 }
All Usage Examples Of App\Repositories\PostRepository::all