Answer the question
In order to leave comments, you need to log in
Yii2 -> how to get category id in controller?
There is a controller:
$categorys = Category::find()->where(['parent_id' => $id, 'status' => '1'])->all();
$categorys1 = $categorys->id;
$query = Product::find()->where(['category_id' => $id, 'status' => '1'])->andWhere(['category_id' => $categorys1])->addOrderBy('id DESC');
$categorys1 = $categorys->id;
$categorys1 = ['38', '43']
- 38 and 43 - this is the id that should be received from the request to the model Category
andWhere(['category_id' => $categorys1])
$categorys = Category::find()->where(['parent_id' => $id, 'status' => '1'])->all();
- does not give Answer the question
In order to leave comments, you need to log in
1. all() must return an array, and you treat it as an object?
2. you make a request to the products "category_id = $id AND category_id = $id2", which of course will not return anything.
3. it turns out you need to do IN (), somehow it was like this:
->where(['status' => '1'])->andWhere(['in', 'category_id', $categoryIds])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question