Laravel 5.4中如何在master.blade展示数据库查询的页脚信息
Got it, let's work through your Laravel Blade requirement step by step—here's how to make everything come together smoothly:
Right now your controller is fetching all data from pjt_setting, but we should narrow it down to just the company_name and company_address to avoid unnecessary data transfer. Assuming your pjt_setting table holds a single row of company config data, update your index method like this:
public function index() { // Fetch only the required fields from the settings table $companyInfo = \DB::table('pjt_setting') ->select('company_name', 'company_address') // Replace with your actual field names if different ->first(); // Use first() since it's a single config row; use get() if multiple rows // Pass the data to the frontend view return view('frontend.index', compact('companyInfo')); }
Pro tip: If your table uses different field labels (like corp_name instead of company_name), just swap those out in the select() method.
Your frontend.index view needs to extend the master.blade.php layout and fill the @yield('content') block with the company data. Add this code to frontend/index.blade.php:
@extends('master') @section('content') <div class="company-details"> <h3>公司信息</h3> <!-- Handle cases where no data exists to avoid errors --> @if($companyInfo) <div class="info-item"> <span class="label">公司名称:</span> <span class="value">{{ $companyInfo->company_name }}</span> </div> <div class="info-item"> <span class="label">公司地址:</span> <span class="value">{{ $companyInfo->company_address }}</span> </div> @else <p>暂无可用的公司信息</p> @endif </div> @endsection
Since master.blade.php is your base layout, add the footer section after the @yield('content') directive so it appears on every page using this layout. You can even reuse the $companyInfo data here if you pass it to the layout (or share it globally via a view composer if needed):
<!-- Existing topbar and navigation code here --> @yield('content') <!-- Footer Section --> <footer class="site-footer"> <div class="footer-container"> <p>© {{ date('Y') }} {{ $companyInfo?->company_name ?? '未知企业' }} 版权所有</p> <p>联系地址:{{ $companyInfo?->company_address ?? '暂无地址信息' }}</p> </div> </footer>
Note: If you want the $companyInfo available in the master layout without passing it from every controller, you can set up a view composer in App\Providers\ViewServiceProvider to share the data globally.
内容的提问来源于stack exchange,提问作者story ks

