在导航选项卡中显示数据库查询的结果

时间:2018-12-13 22:28:21

标签: php html mysql laravel xampp

我想在主页的导航选项卡中显示数据库查询的结果。如何在主页的posts选项卡(最后一个代码块)中调用$ results变量。谢谢

PostController.php

class NoteController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $posts = DB::select(....);
return view('posts.index',['results' => $posts]);
}

职位索引刀片

@extends('layouts.app')

@section('content')

<div class="container">
  <div class="col-md-8">
    @foreach($results as $notes)
      <div class="panel panel-default">
        <div class="panel-body">
          {{$notes->note}}
        </div>
      </div>
    @endforeach
  </div>
</div>

@endsection

家庭索引文件

@extends('layouts.app')

@section('content')
<div class="container">
 <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#posts">Posts</a></li>
    <div class="tab-content">
      <div id="posts" class="tab-pane fade in active">
      </div>
    </div>
</div>
@endsection

1 个答案:

答案 0 :(得分:1)

要在多个视图中使用保存功能,可以在App-> Providers中使用AppServiceProvider.php文件。在那里,您可以使composer视图在所有视图中传递变量,请参见以下示例:

View::composer('*', function ($view) {
  $ads = Ad::all();
  foreach($ads as $ad){
    $customJs = $ad->js;
    $customCss = $ad->css;
  }
  $view->with('customJs', $customJs);
  $view->with('customCss', $customCss);
});

现在通过此示例,我可以在我想要的任何视图中使用$customJs$customCss

希望有帮助。