phpyii2yii2-basic-appyii-components

Unable to transfer data to views


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


Solution

  • 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.