Unable to transfer data to views

82 views Asked by At

To get a list of products, I created a model "ProductList"

namespace app\models;
use yii\helpers\Html;
use yii\db\ActiveRecord;

class ProductList extends ActiveRecord{

      public static function tableName()
      {
            return 'product'; // Имя таблицы в БД
      }

}

Next, I created a controller to transfer data to views

namespace app\controllers;

use app\models\ProductList;
use yii\web\Controller;
class SliderController extends Controller
{
    public function actionIndex()
    {
        $product = ProductList::find()->all();
        return $this->render('index',compact('product'));
    }  
}

But checking in the index

<?php
    var_dump($product);
    die;
?>

I get NULL

1

There are 1 answers

0
Karlis Pokkers On BEST ANSWER

In your Controller change actionIndex() method to :

public function actionIndex()
{
    $product = ProductList::find()->all();
    return $this->render('index',['productModel' => $product]);
}

And in view file first of all define your product variable and then use in list or gridviews, or whatever.

<?php

use app\models\ProductList;

/* @var $productModel ProductList */

var_dump($productModel);
?>

In view, I modified $product variable to $productModel so you will be more clear how that works.