array values with where condition in laravel query - php

I have an array called assetIDs as below
$assetIDs = Collection {#505 ▼
#items: array:2 [▼
0 => 4
1 => 7
]
}
and I have the data in the table as below shown
and I'm doing query on the above table using this
$supplier_id = SupplierAsset::whereIn('asset_id',$asset_ids)->pluck('supplier_id');
and result for the above query is below
Collection {#510 ▼
#items: array:3 [▼
0 => 1
1 => 1
2 => 2
]
}
here whereIn is returning all the possible rows which satisfies the condition. Actually I need to get the result as like which supplier_id has both the values of assetIDs array.In my table supplier_id=1 has the both values 4 and 7 Just like below collection.
Collection {#510 ▼
#items: array:3 [▼
0 => 1
1 => 1
]
}
Can anybody suggest me the solution for this please?

You can try:
$supplier_id = SupplierAsset::whereIn('asset_id',$asset_ids)
->groupBy('supplier_id')
->havingRaw('count(distinct asset_id) = ' . count($assetIDs))
->pluck('supplier_id');

Here is the mysql you should do :
1- get all the id who have more than 1 supplier_id :
SELECT supplier_id, Count(distinct asset_id) as dist_asst
FROM Table
GROUP BY supplier_id
HAVING dist_asst > 1
2- then doing a join :
SELECT t1.supplier_id
FROM Table t1
INNER JOIN (SELECT supplier_id, Count(distinct asset_id) as dist_asst
FROM Table
GROUP BY supplier_id
HAVING dist_asst > 1) t2 on t2.supplier_id = t1.supplier_id

Related

Count multiple columns with groupBy - laravel

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 :)

get array output as key value on doctrine querybuilder

I have two tables for example
student
id name
1 John
2 Doe
subject
id sub student
1 eng 1
2 maths 1
I have a query as follows on student repository
$this->createQueryBuilder('stud')
->select('stud.name,s.sub')
->leftJoin('AppBundle:subject', 's', 'WITH', 's.student = stud.id')
->getQuery()
->getArrayResult();
But I am getting 3 rows with 2 rows as john with different sub and one row as doe.
How could I make it two rows with the sub as an array on the result? I am new to query writing. Hope someone could help
Getting output
array:3 [▼
0 => array:2 [▼
"name" => John
"sub" => maths
]
1 => array:2 [▼
"name" => John
"sub" => eng
]
2 => array:2 [▼
"name" => Doe
"sub" => null
]
]
Expecting output
array:2 [▼
0 => array:2 [▼
"name" => John
"sub" => array:2['eng','maths']
]
2 => array:2 [▼
"name" => Doe
"sub" => null
]
]
The LEFT JOIN returns all records from the left table (here: student), and the matched records from the right table (here: subject). The result is NULL from the right side if there is no match(here: student with id:2).
Here, you should select from subject, then LEFT JOIN it with students. it solves your problem.

Laravel eloquent, create collection based on Column value

I have an example table like the following:
| id | is_new | cate |
| 0 | 1 | [5] |
| 1 | 1 | [5] |
| 2 | 1 | [7] |
| 3 | 1 | [6] |
After I call the following function
$product = Products::where('is_new', 1)->get();
the collection has the following data structure
Collection {#1284 ▼
#items: array:4 [▼
0 => Products {#1285 ▶}
1 => Products {#1286 ▶}
2 => Products {#1287 ▶}
3 => Products {#1288 ▶}
]
}
I can also return a list of category id through:
$category = Category::where('type', 2)->pluck('id')->all();
I wonder is there any good way to achieve following structure (or similar) based on cate. I can use brute force way to create it by looping through the $category then add to new collection manually, I think there is a better way to do it.
Collection {#1284 ▼
#items: array:3 [▼
5 => array:2 [▼
0 => Products {#1285 ▶}
1 => Products {#1286 ▶}
]
6 => Products {#1287 ▶} or 6 => array:1 [ 0 => Products {#1287 ▶} ]
7 => Products {#1288 ▶}
]
}
Solution Update based on answer:
$product->groupBy(function($item) {
return $item['cate'][0]; //because the entry is a list
});
You should take a look at using something like groupBy to group your categories together. It can be done on a database level (assuming you have referential integrity) with foreign keys. You'll also get a guarantee that what is returned is an array for each category, which will make things more consistent (you won't need to make a check for if it is an array or an instance of Products, then).
You might need to change your database layout if your categories are stored like arrays, but for this you should use an intermediary table - look at many to many relationships.

How to properly get the info from the arrays so is possible to insert in database?

After a user fill a form to register two participants in the conference and click in "Register", there is an array with the request data.
The request data has the array name that stores the name of the participant being registered in the registration_type_id "1" (Jake) and the name of the participant being registered in the registration_type_id "4" (John).
The same for the surname array, this surname array stores each surname for each registration type.
And the same for the answer array that stores each answer for each registration type.
"name" => array:2 [▼
1 => array:1 [▼
1 => "Jake"
]
4 => array:1 [▼
1 => "John"
]
]
"surname" => array:2 [▼
1 => array:1 [▼
1 => "W"
]
4 => array:1 [▼
1 => "K"
]
]
"answer" => array:1 [▼
1 => array:1 [▼
1 => array:6 [▼
1 => "answer1"
2 => "answer2"
]
]
]
Doubt: My doubt is how to get the info from this array structure above in order to store each participant in the participants table and also how to store each answer in the answers table.
With the array content above the participants table should stay like:
id registration_type_id name surname registration_id
1 1 Jake W 1
2 4 John K 1
The answers table should stay like:
id participant_id question_id answer
1 1 1 answer1
1 1 2 answer2
Do you know how to properly achieve that?
I have the code below that first insert an entry in the registrations table to store the registration:
# user object
$user = Auth::user();
# add registration to Database
$registration = Registration::create([
'conference_id' => $id,
'user_that_did_registration' => $user->id]
);
But then is necessary to store each participant in the articipants table and also each answer in the answers table. Do you know how to properly get the necessary info from the above array to store in the participants table and in the answers table with the code below?
To insert in the participants table it is necessary to use:
$participant_result = Participant::create([
'name' => how to get the name of each participant from the array?,
'surname' => how to get the surname of each participant from the array?,
'registration_id' => $registration->id,
'registration_type_id' => how to get the registration_type_id from the array?
]);
To insert in the answers table it is necessary to use:
$answer = Answer::create([
'question_id' => how to get each question id from the array?,
'participant_id' => $participant_result->id,
'answer' => how to get each answer from the array?
]);

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')

Categories