Hi all. The objective is to create a drop menu that will produce a brand_id which will in turn produce products based on that brand selection. I’m hoping someone can look at this code and tell me where I’ve went astray in my efforts to capture an id from a dropdown. This might be a slight step forward as previously I was unable define the $model properly. Thanks in advance for your help.
Controller
public function actionIndex($id)
{
$model= Brands::findOne($id)
->where(['brand_id' => 'id']);
if ($id=== null) {
echo 'This is a bad start';
throw new NotFoundHttpException;
} else {
echo 'This is a good start';
$query = Products::find()
->where(['brand_id' => $id])
->joinWith(['power', 'categories']);
$pagination = new Pagination([
'defaultPageSize' =>4,
'totalCount' => $query->count(),
]);
$products = $query
->orderBy('product_name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'products' => $products,
'pagination' => $pagination,
]);
}
}
view
<?= $this->render('_form', [
'model' => $model,
]) ?>
form
<?= $form->field($model, 'brand_id', [
'horizontalCssClasses' => [
'wrapper' => 'col-sm-3',
]])->dropDownList($dataBrandName,
['prompt'=>' ----- Choose a Brand -----']) ?>