Any way to clean up my BlogController ?

Posted 2 years ago by alanaasmaa

Hi,

I need to get categories and tags array for my every blog view to print them on my sidebar. I'm pretty new to php and i was thinking is there a way to make these public to whole controller ?

This is my BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Article;
use App\Models\Category;
use App\Models\Tag;
use App\Models\User;

class BlogController extends Controller
{
    public function index() {
        $articles = Article::with('tags', 'category')->latest()->paginate(9);
        $categories = Category::all();
        $tags = Tag::all();

        return view('pages.blog', compact('articles','categories','tags'));
    }

    public function article($slug) {
        $article = Article::findBySlug($slug);
        $categories = Category::all();
        $tags = Tag::all();

        return view('pages.articles_show', compact('article','categories','tags'));
    }

    public function categories($slug) {
        $articles = Category::findBySlug($slug)->articles()->latest()->paginate(9);
        $categories = Category::all();
        $tags = Tag::all();

        return view('pages.categories', compact('articles','categories','tags'));
    }

    public function tags($slug) {
        $articles = Tag::findBySlug($slug)->articles()->latest('articles.created_at')->paginate(9);
        $categories = Category::all();
        $tags = Tag::all();
        
        return view('pages.tags', compact('articles','categories','tags'));
    }

    public function portfolio() {
        $articles = Article::with('tags', 'category')->where('type', 2)->latest()->paginate(9);
        $categories = Category::all();
        $tags = Tag::all();

        return view('pages.portfolio', compact('articles','categories','tags'));
    }
    public function portfolioItem($slug) {
        $article = Article::findBySlug($slug);
        $categories = Category::all();
        $tags = Tag::all();

        return view('pages.portfolio_show', compact('article','categories','tags'));
    }
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.