Laravel Foreach: Undefined Variable
Im trying to pull all customer information, it should work as its is a simple foreach looping over information. Totally confused as to why I'm getting an undefined variable, in theory should this not work?
Route File:
Route::group(array('before' => 'auth'), function() {
// Get Customer (GET)
Route::get('/customer/clientlist', array(
'as' => 'clientlist',
'uses' => 'CustomerController@getCustomers'
));
});
Model:
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');
}
}
Controller:
class CustomerController extends BaseController {
public function getCustomers() {
$customers = Customer::all();
//var_dump($customers);
return View::make('customer.clientlist', array('customers'=>$customers));
}
}
View home:
@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
View clientlist:
@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
This is the error exception Im currently getting, I cant understand as to why I keep getting this, if I comment out the controller and put static information into the view it does work.
ErrorException
Undefined variable: customers (View:/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(); ?>
I see the problem - you are including the customer.clientlist
file in itself.
In your controller you call
return View::make('customer.clientlist', array('customers'=>$customers));
then in your view you call it again
@include('customer.clientlist', array('customers'=>$customers))
creating an infinite loop. I'm guessing it should be
@include('customer.viewclientlist', array('customers'=>$customers))
And as mentioned by @Joseph Silber - you dont actually need to pass in the array on the include anymore - so make it this to clean up your code:
@include('customer.viewclientlist')
链接地址: http://www.djcxy.com/p/53080.html