title: How to fix Notice (8): Undefined variable: books [APP/Template\books\index.ctp, line 6] in cakePHP?
body: <p>I'm totally a new guy in using CakePhp. I have solved some of my problems, yet I'm stock again with this basic problem. Can you help me fix this.</p>
  
  <pre><code>Notice (8): Undefined variable: books [APP/Template\books\index.ctp, line 6]
  Warning (2): Invalid argument supplied for foreach() [APP/Template\books\index.ctp, line 6]
  </code></pre>
  
  <p>Here's my code where the Error Message is pointing out:</p>
  
  <pre><code>&lt;table&gt;
  &lt;thead&gt;
  &lt;th&gt;ISBN&lt;/th&gt;&lt;th&gt;Title&lt;/th&gt;&lt;th&gt;Author&lt;/th&gt;
  &lt;/thead&gt;
  &lt;?php foreach($books as $book): ?&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;?php echo $book['books']['isbn'] ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $book['books']['title'] ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $book['authors']['name'] ?&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Here's my BooksController.php:</p>
  
  <pre><code>&lt;?php
   /**
   * @property BooksController $BooksController
   */
   namespace App\Controller;
   use App\Controller\AppController;
   class BooksController extends AppController {
    public function display()
    {
      function index() {
          $this-&gt;books-&gt;recursive = 1;
          $books = $this-&gt;books-&gt;find('all');
          $this-&gt;set('books', $books);
      }
      $this-&gt;render('index');
    }
   }
   ?&gt;
  </code></pre>
  
  <p>Here's my books.php:</p>
  
  <pre><code>&lt;?php
   /**
   * @property books $books
   */
  
    class books extends AppModel
    {  
      var $name = 'books';
      var $belongsTo = 'authors';
    }
    ?&gt;
  </code></pre>