laravel渲染部分不传递参数数据
我在仅渲染一个部分的过程中传递变量时遇到问题 所有这些都可以正常工作,但传递到“侧栏”视图的数据数组创建错误$data不存在 我的刀片文件是 Default.blade.phplaravel渲染部分不传递参数数据,laravel,parameters,rendering,laravel-blade,Laravel,Parameters,Rendering,Laravel Blade,我在仅渲染一个部分的过程中传递变量时遇到问题 所有这些都可以正常工作,但传递到“侧栏”视图的数据数组创建错误$data不存在 我的刀片文件是 Default.blade.php ..other html code before.. <body> @include('includes.header') <div class="container-fluid"> <div class="row">
..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..
@extends('layouts.default')
@section('sidebar')
@include('includes.sidebar')
@stop
..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..
sidebar.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..
@extends('layouts.default')
@section('sidebar')
@include('includes.sidebar')
@stop
..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..
我知道array$data很好,因为我尝试只返回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应该可以在需要的地方找到您的变量,这是关键。如果您有正确的想法,只需以可以识别的形式发送变量即可。我将把它扩展到一个极端,以帮助理解各个部分,但您可以轻松地重新组合以获得较短的代码
$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应该可以满足您的需要,这是关键。事实上,我现在看到侧边栏在我拥有的表格主体中重新创建,但这将是我稍后会思考的另一个问题:D现在参数在现实中,我看到侧边栏在我拥有的表格主体中重新创建,但这将是另一个问题,我以后会想:现在参数非常奇怪!我理解你的回答,我完全同意你的看法,但它也有同样的错误例外:E_error未定义变量:data View:/home2/wlimaser/public_html/rimborsi/resources/views/includes/sidebar.blade.php View:/home2/wlimaser/public_html/rimborsi/resources/views/includes/sidebar.phpOK。问题1的解决方案是如上所述使用compact。但是,如何调用视图可能存在一个复杂的问题。尝试:将sidebar.blade.php中的所有视图代码复制到@section'sidebar'并删除@include'includes.sidebar'。这是一个测试。如果这样做有效,您可以保持这种方式,或者只根据侧边栏视图文件重构视图渲染。是的,对不起!!我尝试在foreach循环中访问一个我在select:D中没有指定的列,这是有效的,也是更好的删除include.sidebar的解决方案…….现在我不明白为什么它会在@include'includes.main'中的表体中放置刷新后的侧边栏html,但我认为这是另一个问题:S。太奇怪了!我理解你的回答,我完全同意你的看法,但它也有同样的错误例外:E_error未定义变量:data View:/home2/wlimaser/public_html/rimborsi/resources/views/includes/sidebar.blade.php View:/home2/wlimaser/public_html/rimborsi/resources/views/includes/sidebar.phpOK。问题1的解决方案是如上所述使用compact。但是,如何调用视图可能存在一个复杂的问题。尝试:将sidebar.blade.php中的所有视图代码复制到@section'sidebar'并删除@include'includes.sidebar'。这是一个测试。如果这样做有效,您可以保持这种方式,或者只根据侧边栏视图文件重构视图渲染。是的,对不起!!我尝试在foreach循环中访问一个我在select:D中没有指定的列,这是有效的,也是更好的删除include.sidebar的解决方案…….现在我不明白为什么它会在@include'includes.main'中的表体中放置刷新后的侧边栏html,但我认为这是另一个问题:S。Thx很多