我试图提取所有客户信息,它应该工作,因为它是一个循环信息的简单foreach。完全混淆为什么我得到一个未定义的变量,理论上这应该不起作用?
路线档案
Route::group(array('before' => 'auth'), function() {
// Get Customer (GET)
Route::get('/customer/clientlist', array(
'as' => 'clientlist',
'uses' => 'CustomerController@getCustomers'
));
});
型号:
class Customer extends Eloquent {
//Mass assignment for security
protected $fillable = array('address', 'city', 'postcode',
'phone', 'mobile', 'contact', 'user_id');
//protected $table = 'customers';
public function user() {
return $this->hasMany('User');
}
}
控制器:
class CustomerController extends BaseController {
public function getCustomers() {
$customers = Customer::all();
//var_dump($customers);
return View::make('customer.clientlist', array('customers'=>$customers));
}
}
查看主页
@extends('layout.main')
@section('content')
<div class="container">
@if(Auth::check())
<div class="row push">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<h2 class="intro">Welcome, {{ Auth::user()->username }}</h2>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<button type="button" class="c-add btn btn-danger pull-right">
Add Client
</button>
</div>
</div>{{-- Close Row --}}
<div class="client-form">
@include('customer.client-add-form')
</div>{{-- Close client form --}}
<section id="cs-grid">
<div class="customer-card col-md-4">
<h2>Direct Furniture</h2>
<ul>
<li>Address: <b>595 Boom Road</b></li>
<li>City: <b>Manchester</b></li>
<li>Postcode: <b>BT11 9AB</b></li>
<li>Tel: <b>028 9011 1162</b></li>
<li>Mobile: <b>078 902 815 611</b></li>
<li>Contact: <b>Brian McNally</b></li>
</ul>
</div>
<div class="customer-card col-md-4">
<h2>Direct Furniture</h2>
<address>
795 Folsom Ave, Suite 600<br>
San Francisco, CA 94107<br>
<abbr title="Phone">Phone:</abbr> (123) 456-7890<br>
<abbr title="Phone">Mobile:</abbr> (123) 456-7890
</address>
<address>
<strong>Contact</strong><br>
<a href="tel:555-555-5555">Andrew Ellis</a>
</address>
</div>
@include('customer.clientlist')
</section>
<div class="row">
<div class="col-md-12 text-center">
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">»</a></li>
</ul>
</div>
</div>
@endif
</div>{{-- Close Container --}}
@stop
查看客户列表:
@foreach($customers as $customer)
<div class="customer-card col-md-4">
<h2>{{ $customer->name }}</h2>
<address>
795 Folsom Ave, Suite 600<br>
San Francisco, CA 94107<br>
<abbr title="Phone">Phone:</abbr> (123) 456-7890<br>
<abbr title="Phone">Mobile:</abbr> (123) 456-7890
</address>
<address>
<strong>Contact</strong><br>
<a href="tel:555-555-5555">Andrew Ellis</a>
</address>
</div>
@endforeach
这是我目前得到的错误异常,我无法理解为什么我继续这样做,如果我注释掉控制器并将静态信息放入视图中它确实有效。
ErrorException
未定义的变量:customers(查看:/Users/Sites/nit/app/app/views/customer/clientlist.blade.php)
<?php echo $__env->make('customer.clientlist', array('customers'=>$customers), array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
答案 0 :(得分:0)
我看到了问题 - 你自己包含customer.clientlist
文件。
在您的控制器中拨打
return View::make('customer.clientlist', array('customers'=>$customers));
然后在你的视图中再次调用
@include('customer.clientlist', array('customers'=>$customers))
创建无限循环。我猜它应该是
@include('customer.viewclientlist', array('customers'=>$customers))
正如@Joseph Silber所提到的那样 - 你实际上不需要再传递包含在数组中的数组了 - 所以这样做是为了清理你的代码:
@include('customer.viewclientlist')