Count multiple columns with groupBy - laravel - php

I would like to count two columns in a table and group them in one query. My table:
id
is_correct
question_id
answer_id
1
0
18
67
2
0
18
67
4
1
18
68
2
0
18
67
My queries look like this:
$questions_ids = array(1, 5, 8, 10, 18, 20);
$count_total_answers = $this->whereIn('question_id', $questions_ids)
->select('question_answer_id', DB::raw('count(*) as count_answer_id'))
->addSelect('question_id')
->groupBy(['question_answer_id'])
->get()->toArray();
$count_total_questions = $this->whereIn('question_id', $questions_ids)
->select('question_id', DB::raw('count(*) as count_question_id'))
->groupBy('question_id')
->get()->toArray();
result:
result
I would like to obtain such an array:
array:2 [▼
0 => array:3 [▼
"question_answer_id" => 67
"count_answer_id" => 3
"count_question_id" => 4
]
1 => array:3 [▼
"question_answer_id" => 68
"count_answer_id" => 1
"count_question_id" => 4
]
]
Is this possible in a single query?
Greetings :)

Related

How to get values from an array in Laravel

I have a 3 level category. I need to gain businesses on level 3 category.
I write this code:
$main_cat = Category::where(['slug' => $url])->first();
$lev_cat2 = Category::where(['parent_id' => $main_cat->id, 'status' => '1'])->get();
foreach ($lev_cat2 as $subCategory) {
$cat_ids_lv2[] = $subCategory->id . ',';
}
foreach ($lev_cat2 as $subCat) {
$lev_cat3[] = Category::where(['parent_id' => $subCat->id, 'status' => '1'])->pluck('id')->toArray();
}
dd($lev_cat3);
Then I got this array which is correct:
array:5 [▼
0 => array:3 [▼
0 => 145
1 => 146
2 => 147
]
1 => array:3 [▼
0 => 148
1 => 149
2 => 150
]
2 => array:3 [▼
0 => 151
1 => 152
2 => 153
]
3 => array:3 [▼
0 => 154
1 => 155
2 => 156
]
4 => []
]
now I dont know how can I get values like 145,146,147,148,149,... to pass theme to
Business::where(['category_id'=> [145,146,147,148,...]]->get();
of course dynamic.
You can use laravel collection helpers :
Business::whereIn('category_id', collect($lev_cat3)->flatten()->all())->get();
Since PHP 5.5.0 there is a built-in function array_column which does exactly this.
You can use it to Converting php array of arrays into single array then use it like this
$category_ids = array_column($lev_cat3);
Business::where(['category_id'=> $category_ids]]->get();

Multiple Column SUM with Join table - Laravel 5.2

I have a two tables winners and check_winners.
winners:
id check_winner_id username win_number ....
1 1 xxx 1
2 2 xxx 1
3 3 yyy 1
4 4 yyy 1
check_winners:
id user_id chance_of_win....
1 1 1
2 1 1
3 2 1
4 2 1
5 2 1
6 2 1
7 2 1
8 2 1
Now i want to join two tables and want to sum chance_of_win column and win_number column group by username. I have tried an way but its not give exactly result for sum of win_number. How can i achieve this?
$winners = DB::table('winners')
->groupBy('winners.username')
->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
->selectRaw('*, sum(check_winners.chance_of_win) as chance_sum, sum(winners.win_number) as win_sum')
dd($winners);
with this i get output:
array:2 [▼
0 => {#241 ▼
+"id": 1
+"check_winner_id": 1
+"username": "xxx"
+"chance_sum": "2"
+"win_sum": "2"
}
1 => {#242 ▼
+"id": 2
+"check_winner_id": 2
+"username": "yyy"
+"chance_sum": "6"
+"win_sum": "6"
}
]
but win_sum should be 2 for both array. Where i did wrong? Thanks in advance.
You should try this:
$winners = DB::table('winners')
->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
->selectRaw('*, sum(check_winners.chance_of_win) as chance_sum, sum(winners.win_number) as win_sum')
->groupBy('winners.username')
dd($winners);
Updated answer
$winners = DB::table('winners')
->select('*', DB::raw("SUM(check_winners.chance_of_win) as chance_sum"), DB::raw("SUM(winners.win_number) as win_sum"))
->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
->groupBy('winners.username')

Laravel collection by month

I've got a collection and I need the total users by month.
So I've done this:
array_replace(array_fill_keys(range(0, 11), 0), $users->groupBy('created_at.month')->toArray())
It's sort of working because I get this:
array:12 [▼
0 => 0
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => array:1 [▶]
7 => array:2 [▶]
8 => 0
9 => 0
10 => 0
11 => 0
]
The problem I face now is that I don't need the 2 arrays at position 6 and 7 but I need the counts.
So I was thinking of something like:
$users->groupBy('created_at.month')->count()->toArray();
But, that's obviously not working. Any ideas ?
try using map method
$collection->map(function ($item, $key) {
return count($item);
});

Laravel : How to get values from table available in array?

I am using laravel 5 and having following array:
array:3 [▼
0 => 3,
1 => 4,
2 => 5
]
Now i wanted to get all values/rows from table say 'X' having id's 3,4,5
Try this query
$array = [ 0 => 3,1 => 4, 2 => 5];
$results = DB::table('x')
->whereIn('id',$array)
->get();
You can see the query sample for Laravel 5
$result=DB::table('x')->whereIn('id',[3,4,5])->get();

Fill multidimensional array from other array

I have a multidimensional array $elements where I need to fill it with values from the array $ratings. The array $ratings is built so the first value will fit into the first slot in elements, the next in the second and so on.
$elements
4 => array:3 [▼
2 => 0
3 => 0
4 => 0
]
5 => array:3 [▼
2 => 0
3 => 0
4 => 0
]
7 => array:3 [▼
2 => 0
3 => 0
4 => 0
]
I now need to fill $elements with 9 specific values from
$ratings
array:9 [▼
0 => 3
1 => 2
2 => 1
3 => 3
4 => 3
5 => 2
6 => 3
7 => 2
8 => 1
9 => 3
]
If I manage to loop through $elements, inserting values from $ratings one by one, I will have solved my problem.
So $elements[4][2] should have the value of 3, $elements[4][3] should have value of 2 etc.
Also you can manipulate these by array_fill using loop.
Try this:
<?php
$elements = [
4=>[2=>0, 3=>0, 4=>0],
5=>[2=>0, 3=>0, 4=>0],
7=>[2=>0, 3=>0, 4=>0],
];
$ratings = [ 0 => 3, 1 => 2, 2 => 1, 3 => 3, 4 => 3, 5 => 2, 6 => 3, 7 => 2, 8 => 1, 9 => 3 ];
$ratingsIndex = 0;
foreach(array_keys($elements) as $ElementsIndex) {
foreach(array_keys($elements[$ElementsIndex]) as $ElementsSubIndex) {
$elements[$ElementsIndex][$ElementsSubIndex] = $ratings[$ratingsIndex++];
}
}
echo "<pre>";
print_r($elements);
echo "</pre>";
?>

Categories