如何在公共布局文件中从db获取和显示数据,并在laravel 5.2中的所有视图上显示

如何在公共布局文件中从db获取和显示数据,并在laravel 5.2中的所有视图上显示

问题描述:

Im new to laravel and im doing a study project in laravel 5.2. I had created a web template, various pages and given their routes, created corresponding controllers and models required to display the webpages. The datas on each of the webpages is fetched from the database. Upto this part its working fine. Right now the logo and some header information in the website is hard-coded inside header.blade.php which i stored inside views/includes folder and it is called from the view/layouts folder via default.blade.php and fetching data on each page as @extends('layouts.default'). I want to fetch that header data also from database and it has to be shown on all the webpages.

Below given is my header.blade.php file code:

<div class="top">
    <div class="zerogrid" align="center" style="padding-bottom:2px; padding-top:2px;">
        <ul class="number f-left">
            <li class="mail"><p><a href="mailto:email-comes-from-db-here"><font color="#FFFFFF">email-number-comes-from-db-here</font></a></p></li>
            <li class="phone"><p><a href="tel:phone-number-comes-from-db-here"><font color="#FFFFFF">phone-number-comes-from-db-here</font></a></p></li>
        </ul>
        <ul class="top-social f-right" style="padding-bottom:5px;">
            <li><a href="https://facebook.com/page-name-comes-from-db-here" target="_blank"><i class="fa fa-facebook"></i></a></li>
            <li><a href="https://plus.google.com/page-name-comes-from-db-here" target="_blank"><i class="fa fa-google-plus"></i></a></li>
            <li><a href="https://twitter.com/page-name-comes-from-db-here" target="_blank"><i class="fa fa-twitter"></i></a></li>
            <li><a href="https://linkedin.com/page-name-comes-from-db-here" target="_blank"><i class="fa fa-linkedin"></i></a></li>
            <li><a href="https://youtube.com/page-name-comes-from-db-here" target="_blank"><i class="fa fa-youtube"></i></a></li>
            <li><a href="http://website-name-comes-from-db-here" target="_blank"><i class="fa fa-globe"></i></a></li>                
        </ul>
    </div>
</div>
    <div class="zerogrid">
        <center><div class="logo"><img src="{{ asset('assets/images/logo-image-name-comes-from-db-here') }}"></div></center>
    </div>
<div class="site-title">
    <div class="zerogrid">
        <div class="row">
            <h2 class="t-center">Slogan comes from db here</h2>
        </div>
    </div>
</div>

Below given is my routes file code(routes.php):

Route::get('/', 'GuestController@home');
Route::get('aboutus', 'GuestController@aboutus');
Route::get('services', 'GuestController@services');

Below given is my controller file code(GuestController.php):

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GuestController extends Controller
{
    public function home() {
        return view('guest.home');
    }   
    public function aboutus() {
        $result=DB::table('contents')->where('menuname','aboutus')->get();
        return view('guest.aboutus')->with('data',$result);
    }
    public function services() {
        $result=DB::table('contents')->where('menuname','services')->get();
        return view('guest.services')->with('data',$result);
    }
}

For displaying each webpage data from db table on a single webpage is already done via routes. But to display the same header data from db to all my pages, what modification in routes/controller files i have to do? Please help. Thanks in advance...

You can use a view ccomposer, for example:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{

    public function boot()
    {

        // Using Closure based composers
        view()->composer('guest.includes.header', function ($view) {
            $headerData = \DB::table('headers')->where('...')->get();
            $view->headerData = $headerData;
        });
    }

    public function register()
    {
        //
    }
}

Add this provider in the providers array in the config\app.php file and in the header.blade.php you will get a $headerData variable whenever the guest.includes.header view is loaded/composed so access that variable and loop it and print the data you get from your DB. For example:

// guest/includes/header.blade.php
<ul>
    <li>
        <a href="{{$headerData['facebook']}}" target="_blank">
            <i class="fa fa-facebook"></i>
        </a>
    </li>

    <!-- You may add more li here-->
</ul>

Also, it' possible to do a loop like:

<ul>
    @foreach($headesData as $fieldName => $fieldValue)
        {{-- $fieldName will help to determine the icons: facebook/google --}} 
        <li><a href="{{$fieldValue}}"></li>
    @endforeach
</ul>

This may not provide direct answer to your question because you didn't clerified every bit but anyways, it'll give you the right direction.