Hey all, I have tour and company table. Company can has more than one tour. I want to list all the tours that company has but I could not achieve it. I fetched only one data from database. I tried to use foreach but I got error. This is my controller:
public function actionView($id)
{
$this->view->title = 'Company';
$model = Tour::findOne(['company_id' => $id]);
$company = Company::findOne($id);
return $this->render('view', [
'model' => $model,
'company' => $company
]);
}
this is the view part:
<tr>
<td><?= $model->id ?></a></td>
<td><?= $model->title ?></td>
</tr>
I inserted foreach before tr tag like this: <?php foreach ($model as $model): ?>
Got this error: Trying to get property ‘id’ of non-object. After that I inserted the getCompany function like so: <?= $model->company->id ?>… Still cant list the all the tours are related to that company. How can I fix it? What am I doing wrong here?
` public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}`