Answer the question
In order to leave comments, you need to log in
How to use indexBy to shorten array code?
I made something like this array building an array, something like this
$shops = $request->get('ShiftsSearch')['shop_id'] == null ? Shop::find()->all() : Shop::find($request->get('ShiftsSearch')['shop_id'])->all();
foreach ($shops as $shop){
$arrShop[$shop->name] = Shifts::find()->with('shop')->andWhere(['between', 'date', $date_start->format('Y-m-d'), $date_end->format('Y-m-d')])->andWhere(['shop_id' => $shop->id])->all();
}
query => [
'Сибирский тракт' => [
[
'id' => int 34
'user_id' => int 5
'shop_id' => int 1
'date' => string '2018-03-20' (length=10)
'start_time' => string '08:10:00' (length=8)
'end_time' => string '18:15:00' (length=8)
'created_at' => string '2018-03-23 10:25:13' (length=19)
'updated_at' => string '2018-03-23 10:25:13' (length=19)
],
[
'id' => int 35
'user_id' => int 6
'shop_id' => int 1
'date' => string '2018-03-20' (length=10)
'start_time' => string '08:10:00' (length=8)
'end_time' => string '18:15:00' (length=8)
'created_at' => string '2018-03-23 10:25:13' (length=19)
'updated_at' => string '2018-03-23 10:25:13' (length=19)
],
[
'id' => int 36
'user_id' => int 8
'shop_id' => int 1
'date' => string '2018-03-20' (length=10)
'start_time' => string '08:10:00' (length=8)
'end_time' => string '18:15:00' (length=8)
'created_at' => string '2018-03-23 10:25:13' (length=19)
'updated_at' => string '2018-03-23 10:25:13' (length=19)
],
//и тд
]
]
$query = Shifts::find()->with('shop')->andWhere(['between', 'date', $start, $end]);
$query = $request->get('ShiftsSearch')['shop_id'] == null ? $query->indexBy('shop.name')->all() : $query->andWhere(['shop_id' => $request->get('ShiftsSearch')['shop_id']])->indexBy('shop.name')->all();
public 'query' =>
array (size=1)
'Сибирский тракст' =>
object(app\models\Shifts)[153]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=8)
'id' => int 34
'user_id' => int 5
'shop_id' => int 1
'date' => string '2018-03-20' (length=10)
'start_time' => string '08:10:00' (length=8)
'end_time' => string '18:15:00' (length=8)
'created_at' => string '2018-03-23 10:25:13' (length=19)
'updated_at' => string '2018-03-23 10:25:13' (length=19)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question