在我的index.blade上,我想显示两组不同的数据,一组是计算机列表,另一组是类别列表。两种不同类型的数据。在我的控制器中我有
public function index()
{
// Select data from table assets
$assets = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10); //pass all the employees to a Var and send it to our page using "with" also puts page # at the bottom
//select data from table assetrefs
$categories = DB::table('assetrefs')->get();
//bind data and send using 'with'
return view('assets.index')->with('assets', $assets)->with('categories', $categories);
}
在我看来,我有这两个循环
@if(count($categories) > 1)
@foreach($categories as $row)
{!!Form::open(['action' => ['AssetsController@showType', $row->title], 'method'=> 'POST', 'class'=> 'pull-left'])!!}
{{Form::submit('Show '. $row->title, ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
@endforeach
@else
<p> no content </p>
@endif
这显示了一堆带有类别的按钮
<div class="well"></div>
<!-- lists all the available assets in a div all types are mixed but sorted -->
@if(count($assets) > 1)
@foreach($assets as $asset)
<div class="well">
<h3>{{$asset->type}}, {{$asset->make}} - {{$asset->model}} </h3>
<small> {{$asset->sn}}</small>
<small> <a href="/asset/{{$asset->id}}">View this account</a></small>
</div>
@endforeach
{{$assets->links()}}
@else
<p> no content </p>
@endif
这显示了所有计算机的列表
当我加载页面时,我得到了
2/2 ErrorException
Undefined variable: categories (View: resources\views\assets\index.blade.php)
答案 0 :(得分:0)
你可以通过使用数组而不是像下面那样传递数据来查看:
public function index()
{
// passing data without with
$data['assets'] = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10);
$data['categories'] = DB::table('assetrefs')->get();
return view('assets.index',$data);
// in case if you want to use with then
// return view('assets.index')->with($data);
}
或者即使你不想使用$ data变量想要保持$ asset和$ categories变量,那么
public function index()
{
// passing data without with
$assets = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10);
$categories = DB::table('assetrefs')->get();
return view('assets.index',compact('assets','categories'));
}
你可以通过url检查php中的紧凑功能是如何工作的 - &gt; http://php.net/manual/en/function.compact.php
答案 1 :(得分:0)
在您的控制器中尝试此操作,代替您的view()调用:
<Button>
查看Passing Data To Views,您会看到 return view('assets.index', compact('assets', 'categories'));
适合传递单个数据,而多个变量则需要将数组作为第二个参数传递。在这里,我们通过PHP compact函数传递已定义的view()->with()
和assets
数组。