mardi 3 mai 2016

Laravel 5.2.3 repeated part of code in several controllers/methods

I don't know if it's good practice. But I think that I can put this part of code:

        $categories = DB::table('categories')
        //[something]
        ->get();

somewhere to not ctrl+c ctrl+v in many places. Can you tell me what can I do with it in Laravel? full example:

class FirstController extends Controller
{
    public function index()
    {
        $articles = DB::table('articles')
            //[something]
            ->get();


        $categories = DB::table('categories')
            //[something]
            ->get();

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

    public function show($id)
    {
        $categories = DB::table('categories')
            //[something]
            ->get();
        $article = Article::findOrFail($id);
        return view('pages.show', compact('article', 'categories'));
    }
}

class SecondController extends Controller
{
    public function index()
    {
        $categories = DB::table('categories')
            //[something]
            ->get();

        return view('pages.contact')->with('categories', $categories);
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SKHUOE
via IFTTT

Aucun commentaire:

Enregistrer un commentaire