Jesse Luke Orange
Jesse Luke Orange

Reputation: 1999

Putting data into master blade - Laravel

I have seen a few similar questions to mine, with the common answer being to use a view composer. I have a HomeController that shows articles from a database by passing query data to an associated view, which works see this image link

As you can see there is a nav bar, which is generated by the master layout, layout.master.

For each title in the navigation I am trying to show each article for that section via a for loop which generates the links.

My code is this.

public function index()
{
    $loans_articles = Article::byDepartment('Loans')->get();
    $digital_articles = Article::byDepartment('Digital')->get();
    $consulting_articles = Article::byDepartment('Consulting')->get();

    return view('welcome', 
        [
        'loans_articles' => $loans_articles, 
        'digital_articles' => $digital_articles, 
        'consulting_articles' => $consulting_articles, 
        ]);

}

As you can see I'm returning this data to the welcome blade.

In my nav bar I tried

@if(count($loans_articles) > 0)
            @foreach($loans_articles as $ls)

        <!--for each loop which grabs the articles with department Loans-->
        <li><a href="/article/{{ $ls->id }}">{{  $ls->title }}</a></li>

            @endforeach
        @endif

But as soon as you navigate away from the home page the nav bar doesn't know what $loans_article is.

Is there a clean way to pass this data to the master blade navigation without sending the same data to every subview?

Upvotes: 5

Views: 3189

Answers (4)

Ali_Hr
Ali_Hr

Reputation: 4645

if you would like to share data with all of your blade templates, a simple way is trough appServiceProvider.php and boot method. you can find this file in app/Providers/AppServideProvider.php and do the changes like bellow:


namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }
}

after that you can access to the value in all blade files using {{ $key }}

note: do not forget to use Illuminate\Support\Facades\View in top of the class

Upvotes: 1

Prashant Kapse
Prashant Kapse

Reputation: 1

<?php $contact = DB:table('tbl_contact')->get(); ?>

Use this code in master.blade.php in top section And Use such as

<a href='{{ $contact[0]->fbLink ? $contact[0]->fbLink : " " }}'>

Upvotes: -1

Jonathon
Jonathon

Reputation: 16283

The way I tend to achieve this is by making a variable available to every view in this way:

All of your controller should extend a base controller, which is usually located in app/Http/Controllers/Controller.php. Inside this controller you can put some code that will be used by all extending controllers.

In this base controller you can make a variable available to all views, like this...

class Controller extends BaseController
{
    public function __construct()
    {
        // Load your objects
        $loans_articles = Article::byDepartment('Loans')->get();

        // Make it available to all views by sharing it
        view()->share('loans_articles', $loans_articles);
    }
}

All of your controllers must extend this controller for this to work.

If any of your controllers have their own constructors, you must also make sure to call parent::__construct() to ensure the above code is run. If your controllers don't have their own constructors, you can omit calling parent::__construct().

public class HomeController extends Controller
{
    public function __construct()
    {
        parent::__construct();

        // Your constructor code here..
    }
}

This way you should be able to use $loans_articles in all of your views.

Upvotes: 9

Pranab
Pranab

Reputation: 229

You must use View Composer to achieve what you are trying to do :

https://laravel.com/docs/5.2/views

Upvotes: 2

Related Questions