You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Tweak the Controller Query to Get Only Needed Fields

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.

2. Display Company Info in the Frontend View

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>&copy; {{ 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:31:45