duanaoshu1989 2016-02-21 18:55
浏览 115
已采纳

如何在公共布局文件中从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...

  • 写回答

1条回答 默认 最新

  • dongyao9762 2016-02-21 19:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)