You’re setting the variable $posts to view, but you have only defined $query, so that’s certainly a mistake.
It’s also important to understand that a query builder function like $this->Posts->find(‘all’) only creates an SQL query and does NOT execute it automatically.
To execute it, you can do something like:
$this->Posts->find()->all()
or:
$this->Posts->find()->toArray()
or you can loop through it with a foreach like you did at the bottom of your action.
So the proper code is:
<?php
namespace App\Controller;
use App\Controller\AppController;
class PostsController extends AppController {
public function index() {
$posts = $this->Posts->find();
$this->set(compact('posts'));
}
}
And then in your view:
foreach ($posts as $post) {
debug($post->name);
debug($post->content);
}
public function posts() {
//query all posts
$posts = $this->Posts->find('all');
//pagination
$this->set('posts', $this->paginate($posts));
//retour du resultat
$this->set(compact('posts'));
}
and view single where get id :
public function single($id) {
$single = $this->Posts->get($id);
$this->set(['single' => $single]);
}