简体   繁体   中英

Laravel 5.1 - How to load content from database for Master Page

I'm making an administrable website in Laravel. I have a custom class that loads the content of a page / widget from a database and returns HTML.

/**
 * Loads a page content from the database
 * 
 * @param Id of the page, it can be a number or class constant
 * @return html
 */
public function pageContent($id)
{
    $content = DB::table('pages')->where('id', '=', $id)->first();
    return $content->content;
}

Let's say the website administrator put in that page something like:

"All Rights reserved, blablabla 2015, Contact Us: 488 889 77 88"

And I want it to appear in the website footer. Some of my master page code is.

<body>

    @include('layouts.mainmenu')

    @yield('body')

    <div class="footer">
        {{ I WANT TO HAVE THE FOOTER FROM THE DATABASE HERE! }}
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="{{ asset('js/bootstrap.min.js') }}"></script>
    <script src="{{ asset('js/main.js') }}"></script>

    @yield('bottom-js')

</body>

If your function is inside your HomeController

HomeController:

  public function pageContent($id)
 {
   $content = DB::table('pages')->where('id', '=', $id)->first();
   return $content;
  }

  public function index()
  {
    $content = $this->pageContent();
    return view('your_master_blade', compact('content'));
  }

View inside your master blade:

<body>

@include('layouts.mainmenu')

@yield('body')

<div class="footer">
    @foreach($content as $cont)
       {{$cont->id}}
         //so on and so forth
    @endforeach
    {{ I WANT TO HAVE THE FOOTER FROM THE DATABASE HERE! }}
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<script src="{{ asset('js/main.js') }}"></script>

@yield('bottom-js')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM