Laravel Foreach:未定义变量

我试图拉取所有的客户信息,它应该工作,因为它是一个简单的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="#">&laquo;</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="#">&raquo;</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(); ?> <?php echo $__env->make('customer.clientlist', array('customers'=>$customers), array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>


我看到了问题 - 你自己包含了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')
链接地址: http://www.djcxy.com/p/53079.html

上一篇: Laravel Foreach: Undefined Variable

下一篇: PHP Laravel break foreach loop 2 times