1

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="#">&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

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(); ?>

1 Answer 1

0

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')
Sign up to request clarification or add additional context in comments.

13 Comments

so update the view name to -> viewclientlist? Will this also not have to be updated in the controller too, so it does not throw an undefined view name?
You need to update the @include() filename to be whatever your file is "View client list" that you put in your question. If you are not sure - update your question with the exact file names of your views and I'll tell you what to write.
The Shift Exchange, I updated the route -as statement, controller, main view and changed the name of the file to viewclientlist. Updated above.
Crippy - let me put it to you another way - You are doing "Return view::make('File1'). Then inside "File1" you are doing @include(File1). It should be @include(File2). You need to fix up your code to not have an infinite loop.
The Shift Exchange - I am doing "Return view::make('File1')". Inside my "File1" it contains the @foreach. Im including the "File1" template inside my "home" view as "@include(File1)" to render the foreach as a set of results. Is this totally wrong, now a little confused, appreciate your time over this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.