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条回答 默认 最新

相关推荐 更多相似问题