laravel渲染部分没有传参数据
laravel rendering section no passing parameters data
我在仅渲染一个部分时传递变量时遇到问题
一切正常,但传递给部分('sidebar')视图的数据数组会产生错误($data doesn't exist)
我的 blade 个文件是
Default.blade.php
..other html code before..
<body>
@include('includes.header')
<div class="container-fluid">
<div class="row">
@yield('sidebar')
<!-- main content -->
@include('includes.main')
</div>
<footer class="row">
@include('includes.footer')
</footer>
</div>
</body>
..other code after..
home.blade.php
@extends('layouts.default')
@section('sidebar')
@include('includes.sidebar')
@stop
sidebar.blade.php
..other html code before..
<h2>The current UNIX timestamp is {{ time() }}.</h2>
<ul>
@isset($data)
@foreach ($data as $item)
<li class="nav-item"> {{$item->polizza}}</li>
@endforeach
@endisset
</ul>
..other html code after..
控制器方法搜索
public function search(Request $request){
if ($request->ajax()) {
$data = Customers::select('id','contr_nom','email','targa','cliente')
->where('polizza',request('polizza'))
->get();
return view('pages.home',$data)->renderSections()['sidebar'];
}
//return json_encode($data);
}
我知道数组 $data 很好,因为我尝试 return 只是 JSON 并且我知道只是边栏刷新因为时间戳发生变化。
但是 $data 没有传递给刷新的侧边栏部分!!
为什么?
非常感谢
你的想法是正确的,你只需要以一种可以识别的形式发送变量。我将把它分解到一个极端,以帮助理解这些部分,但您可以轻松地重新组合以获得更短的代码。
$view = view('pages.home', compact('data')); // Compact with the text name ('data') sends the variable through
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
return $sections['sidebar']; // this will only return whats in the sidebar section of the view
使用 compact()
应该可以到达你需要的地方,这是关键。
我在仅渲染一个部分时传递变量时遇到问题
一切正常,但传递给部分('sidebar')视图的数据数组会产生错误($data doesn't exist)
我的 blade 个文件是
Default.blade.php
..other html code before..
<body>
@include('includes.header')
<div class="container-fluid">
<div class="row">
@yield('sidebar')
<!-- main content -->
@include('includes.main')
</div>
<footer class="row">
@include('includes.footer')
</footer>
</div>
</body>
..other code after..
home.blade.php
@extends('layouts.default')
@section('sidebar')
@include('includes.sidebar')
@stop
sidebar.blade.php
..other html code before..
<h2>The current UNIX timestamp is {{ time() }}.</h2>
<ul>
@isset($data)
@foreach ($data as $item)
<li class="nav-item"> {{$item->polizza}}</li>
@endforeach
@endisset
</ul>
..other html code after..
控制器方法搜索
public function search(Request $request){
if ($request->ajax()) {
$data = Customers::select('id','contr_nom','email','targa','cliente')
->where('polizza',request('polizza'))
->get();
return view('pages.home',$data)->renderSections()['sidebar'];
}
//return json_encode($data);
}
我知道数组 $data 很好,因为我尝试 return 只是 JSON 并且我知道只是边栏刷新因为时间戳发生变化。 但是 $data 没有传递给刷新的侧边栏部分!! 为什么? 非常感谢
你的想法是正确的,你只需要以一种可以识别的形式发送变量。我将把它分解到一个极端,以帮助理解这些部分,但您可以轻松地重新组合以获得更短的代码。
$view = view('pages.home', compact('data')); // Compact with the text name ('data') sends the variable through
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
return $sections['sidebar']; // this will only return whats in the sidebar section of the view
使用 compact()
应该可以到达你需要的地方,这是关键。