I just started to learn Yii2 and immediately ran into a misunderstanding of how to display the result of a query to the database in view. I created a post table and try to pull a dataset out of it. It seems everything is logical but does not work.
Model: localhost \ yii2-basic \ models \ Post.php
namespace app\models; use yii\db\ActiveRecord; class Post extends ActiveRecord { public static function tableName() { return 'post'; } }
Controller: localhost \ yii2-basic \ controllers \ PostController.php
namespace app\controllers; use yii\web\Controller; use app\models\Post; class PostController extends Controller { public function actionIndex(){ $posts = Post::find()->all(); return $this->render('index', compact('posts')); } }
View: localhost \ yii2-basic \ views \ post \ index.php
var_dump($posts);
When trying to output, phpstorm already tells you that this $posts
variable is not declared and underlines it. Why it happens? It seems that the controller should render it in a view, but this does not happen. Tell me why this is happening?