How to fetch data from database

I am trying to get data from database, so I tried to doit with $Get. Sİnce there will not be any user entered data.
This is my controller
class HotelController extends Controller
{
public function actionIndex($id) {

    $this->view->title = 'List Hotels';
    $items = ArrayHelper::map(Hotel::find()->all(),'id','name');
    $model = Hotel::find($id)->all();

    return $this->render('index', [
        'items' => $items,
        'model' => $model
    ]) ;

}

This is the my view code
<?php

/* @var $this yii\web\View */

use yii\helpers\Html;

$this->title = ‘Hotel list’;
$this->params[‘breadcrumbs’][] = $this->title;
?>

<?php foreach ($model->items as $item): ?>

<? $item->name ?>

<?php endforeach; ?>

The error I got bad request missing required parameter $id. How can I fix that?
—public function actionIndex($id)— does this code( —public function actionIndex($id)— ) says bring the id column from database or something else? I am beginner in yii I am sorry if the question is so easy. Thanks

Hi yasoyase, when you set the $id parameter for the action, the way you are doing, it becomes a required param, so you have to pass it through your URL, like calling: mysite/hotel?id=10.

If you don’t pass that ID in the URL, it will not be loaded and will throw the error. Can read more on parameter passing here: Action Parameters

Still, I would change this index passing the $id, a better approach would be generating that code using the Gii tool. This way you will have a good boilerplate to start working upon.

Read more about the Gii tool here: Generating Code with Gii

1 Like

I am sorry, your controller & view structure is wrong. In index view, you don’t need $model. You either pass items or model to fetch its items.
It is very straightforward section in Yii Guide in Display Data Documentation. You can use Data Widgets for start… Displaying Data: Data Widgets | The Definitive Guide to Yii 2.0 | Yii PHP Framework

1 Like

So, as you are a beginner, I’ll provide some extra information from the answers above.

As @bpanatta said, when you use this line:

public function actionIndex($id) {

the program expects you send the parameter [ id ] along with your url. E.g.: https://yoursite.com/hotel/index?id=10.

This is usually used in the update method, when you are loading a specific record. E.g:

public function actionUpdate($id)
{
    $model = Hotel::findOne($id);  
    // this will try to load the hotel model with the ID passed in your URL 
    // also important to say that you need to do some check if this ID doesn't exist. 

    return $this->render('update', [
        'model' => $model
    ]);
}

But you’re trying to list all your hotel models, right?
Then, in your controller, you could simply do something like this:

public function actionIndex() // no $id parameter needed here
{
    $models = Hotel::find()->all(); // this will bring all your hotel models as an array of objects;

    return $this->render('index', [
        'models' => $models // you are passing this array with models to your view
    ]);
}

Then, in your view, you could use your loop exactly like you did.

But, allow me to suggest you to read some documentation in order to take advantage of some incredible features already developed in the framework:

Have in mind that Yii is an incredible tool with plenty of widgets and resources ready to use.
A great way to study is also taking advantage of the GII module, where you can automatically generate the CRUD (create, reade, update, delete) views and controller for a model.

And that’s it.
Keep up and you’ll be amazed by the power of this tool!

Some extra tips:

  • You don’t need to set you page title in the controller like you did in the second line of your actionIndex() … unless you need to for some other reason. You can do it directly in your view, like you’re also doing when you set $this->title = ‘Hotel list’; .

Kind regards,

2 Likes

Thanks a lot. I got it now.