Tidy up blade templates using View Composer

Last Updated at : December 11, 2017 By - Usama Muneer | 1579

    The Problem

    It has always considered a poor practice of Developers on directly querying database from the blade files in Laravel. For example, in your template file, you may have used something like this:

    @foreach(App\User::all() as $user)

    This may work Ok, but you’re directly calling the User model inside of your blade file. Good practice says your view file shouldn’t know whats happening inside & outside your database at all.

    We’ll look on a couple of solution to tackle this efficiently.

    Solution # 1 – Pass it from the View

    In your UserController method you’re using. You may pass your variables like so

    $users = App\User::all();
    return view('user',compact('users'));

    This may be a workaround, but think of it as you have to pass on users to every single view you want to display your users on. Well don’t just lose hope, Laravel got you covered.

    Solution # 2 – View Composers

    In your AppServiceProvider.php, utilize your boot() method to pass on the view composer to your application and use it wherever you want to.

    public function boot()

    You can now use users variable inside of any of your application’s blade files without any hassle.

    The Use-Case

    Now you can simply use in your file.blade.php

    @foreach($users as $user)


    You can create a separate ServiceProvider for your View Composers, but thats not necessary unless your appServiceProvider is overloading. Play around with it & let us know if you stuck in between.

    If you liked this blog, do share it with your fellow developer friends. You can also follow us on Twitter.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Laravel Live Islamabad, ended on 24th Nov'18 & we got a lot to learn from the top-notch of speakers & influencers. Thanks to the sponsors for pulling off such a great event in the capital of Pakistan.

    Previously Localization in Laravel was done by creating separate directories for each locales and place in under resources/lang/ and add key value pairs to every single keyword in a number of files as per their modules.

    Another #LaraconUS in the books & It was the 6th instalment of #LaraconUS happened at Chicago with full swing & we got a lot to learn this time with exceptional line of speakers and never-ending community support by fellow Artisans

    Laravel is one of a famous and widely used PHP framework which is growing with immense pace since its version 4.2. Learning Laravel & Setting up your application locally is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle.