I want to use the painless script to group the response received after running the index. I want to group the response based on the group_id tag and return other tags also. So far, I have used the following script:
{
"size": 0,
"_source": true,
"aggs": {
"entity_group_aggregations": {
"terms": {
"field": "group_id",
"order": {
"_term": "asc"
}
}
}
}
}
However, this script is not returning the desired output.
My index response looks like the following
{
"hits": {
"total": {
"value": 8693,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "entity_group",
"_type": "_doc",
"_id": "100000003628-brokerAssignment-Outbound-100000000483-aldera",
"_score": 1.0,
"_source": {
"interaction_id": 100000000483,
"group_id": "AFV0001789",
"dest_status": "success",
"group_effective_date": "2021-11-30T18:30:00.000Z",
"exception_cause": "",
"executed_dest_json": [
{
"Type": "Validation",
"name": "validateGroupCountry",
"group": "sequence2",
"sequence": 20,
"resultType": "success",
"status": "processed",
"description": "Validation for group country.",
"resultReason": "Success"
},
{
"Type": "Validation",
"name": "validateProductName",
"group": "sequence2",
"sequence": 21,
"resultType": "success",
"status": "processed",
"description": "Validation for product name.",
"resultReason": "Success"
},
{
"Type": "Validation",
"name": "validateProductEffectiveDate",
"group": "sequence2",
"sequence": 22,
"resultType": "success",
"status": "processed",
"description": "Validation for product effective date.",
"resultReason": "Success"
}
],
"file_name": "brokerAssignment",
"no_of_records": 3,
"di_source": "everwellNgl",
"group_name": "CLAXTON HOBBS PHARMACY - MPA7",
"src_status": "success",
"acknowlegment_record": "{\"groupReturn\": [{\"EB ID#\": \"3391F2D6-3035-11EC-B867-DA6E058A38F8\", \" Group Number\": \"dev-MPA7\", \"Dental Group Rating EE\": null, \"Vision Group Rating EE\": \"5.7\", \"Dental Group Rating FAM\": null, \"Everwell Dental Plan ID\": \"\", \"Everwell Vision Plan ID\": \"11964\", \"Vision Group Rating FAM\": \"16.66\", \" Dental Group Number\": \"\", \" Vision Group Number\": \"AFV0001789\", \"Dental Group Rating EE+CH\": null, \"Dental Group Rating EE+SP\": null, \"Vision Group Rating EE+CH\": \"11.96\", \"Vision Group Rating EE+SP\": \"11.4\", \"Dental Minimum Participation Met\": \"\", \"Vision Minimum Participation Met\": \"Y\"}]}",
"pre_br_canonical": {
"groupDetail": {
"contacts": [
{
"faxNumbers": [
{
"faxNumber": "7702272428",
"type": "primary"
}
],
"firstName": null,
"lastName": null,
"phoneNumbers": [
{
"extension": null,
"type": "primary",
"phoneNumber": "7702272428"
}
],
"emailAddresses": [
{
"emailAddress": null,
"type": "primary"
}
],
"type": "group"
},
{
"faxNumbers": [
{
"faxNumber": null,
"type": "primary"
}
],
"firstName": "billingfn",
"lastName": "billingln",
"phoneNumbers": [
{
"extension": null,
"type": "primary",
"phoneNumber": null
}
],
"emailAddresses": [
{
"emailAddress": "contaclmail5343234#gmail.com",
"type": "primary"
}
],
"type": "billing"
},
{
"faxNumbers": [
{
"faxNumber": null,
"type": "primary"
}
],
"firstName": "contactfn",
"lastName": "contactln",
"phoneNumbers": [
{
"extension": null,
"type": "primary",
"phoneNumber": null
}
],
"emailAddresses": [
{
"emailAddress": "akajjamiapin#empoweredbenefits.com",
"type": "primary"
}
],
"type": "admin"
}
],
"transactionType": "A",
"marketer": null,
"ein": null,
"products": [
{
"plans": [
{
"planTakeOverFlag": "N",
"paymentDetail": null,
"networkID": null,
"otherInsuranceCoverageId": null,
"terminationDate": "11-30-2022",
"rating": {
"financialCode": null,
"rateTimeBasis": null,
"rateEffectiveDate": null,
"rateTerminationDate": null,
"coverageTierCode": null,
"initialRateGuaranteePeriod": null,
"rateByCoverageCode": [
{
"maxAge": null,
"rateAmount": "5.7",
"coverageCode": "EMP",
"gender": null,
"minAge": null
},
{
"maxAge": null,
"rateAmount": "11.4",
"coverageCode": "ESP",
"gender": null,
"minAge": null
},
{
"maxAge": null,
"rateAmount": "11.96",
"coverageCode": "ECH",
"gender": null,
"minAge": null
},
{
"maxAge": null,
"rateAmount": "16.66",
"coverageCode": "FAM",
"gender": null,
"minAge": null
}
],
"initialRateGuaranteePeriodType": null,
"rateTableId": null
},
"effectiveDate": "12-01-2021",
"planId": null,
"minParMet": null,
"contract": null,
"planYearType": null,
"enrolledLives": 2,
"externalPlanId": "11964",
"eligibilityValidationCode": null
}
],
"renewalDate": null,
"renewalTimeValue": null,
"product": "VIS",
"terminationDate": null,
"effectiveDate": null,
"renewalTimeBasis": null
}
],
"websiteUrl": null,
"groupName": "HOBBS PHARMACY - MPA7",
"signingProducer": {
"lastName": "agentln",
"email": "agentmailfsd2345#gmail.com",
"firstName": "agentfn",
"writingNumber": "ad126"
},
"tin": "331057747",
"associatedBrokers": [
{
"brokerId": "A123",
"brokerLastName": "Kazhamiakin",
"systemLob": null,
"primaryAgent": null,
"brokerNpn": "15341",
"customLob": null,
"terminationDate": null,
"effectiveDate": "10-18-2021",
"brokerFirstName": "Alex",
"agency": {
"agencyName": null,
"agencyId": null,
"agreementType": null
},
"commissionDetail": {
"agentLevel": null,
"commission": null,
"commissionType": "2",
"commissionPlan": null
},
"splitPercentage": "100"
}
],
"source": "EverwellGroupsetup_20211108155000_Test1.csv",
"groupType": "NGL",
"division": null,
"sicCode": "5912",
"addresses": [
{
"line1": "131 W TAYLOR ST",
"zip": "30223",
"line2": null,
"city": "GRIFFIN",
"state": "GA",
"type": "group",
"country": "US"
},
{
"line1": null,
"zip": null,
"line2": null,
"city": null,
"state": null,
"type": "billing",
"country": "US"
}
],
"businessLevel": null
},
"groupIds": [
{
"system": "everwell",
"parentId": null,
"id": "3391F2D6-3035-11EC-B867-DA6E058A38F8"
},
{
"system": "",
"parentId": null,
"id": "dev-MPA7"
},
{
"system": "",
"parentId": null,
"id": null
}
],
"groupEligibilityDetail": {
"nextRenewalDate": "12-01-2022",
"terminationDate": null,
"groupStatus": null,
"effectiveDate": "12-01-2021",
"lastRenewalDate": null,
"eligibleLives": 9,
"terminationReason": null
},
"groupConfigurations": {
"billingConfiguration": {
"billingFrequency": "M",
"paymentType": null,
"billingParameters": [
{
"billCheckDigit": null,
"paymentType": null,
"applyAutoCash": null,
"terminationDate": null,
"effectiveDate": null,
"invoiceTimingType": null,
"invoiceBasisDateType": null,
"numberOfDaysPriorACH": null,
"invoiceType": null,
"billingParameterIds": [
{
"system": "aldera",
"parentId": null,
"id": null,
"previousId": null
}
]
}
]
},
"miscellaneousConfiguration": {
"preferredLanguage": null,
"orthoMaxInitialPaymentAmount": null,
"orthoInitialPayment": null,
"erApplicationPDFfileName": "71650_CLAXTON_HO.pdf",
"programNumber": null,
"orthoMaxAdditionalPaymentAmount": null,
"orthoPaymentType": null
},
"memberEligibilityConfiguration": {
"cobValidDays": null,
"memberVerificationFrequency": null,
"memberEligibilityFrequency": null,
"memberBenefitExtensionDays": null,
"idCardProductionParmeter": null,
"memberVerificationSource": null,
"waitingMonths": 1,
"memberEligibilityType": null,
"bigLoadAutoGenMemberID": null,
"coverageCodeCalculation": null
},
"groupEligibilityConfiguration": {
"renewalNotificationDays": null,
"memberEligibilityRequired": null,
"cascadeTermToMembers": null,
"retroNumberOfPeriods": null,
"fullTimeEligibilityHours": 11,
"runOutDays": null,
"retroTimePeriodType": null,
"cascadeTermToSubGroups": null
},
"ratingConfiguration": {
"financialCode": null,
"rateTimeBasis": null,
"renewalTimeValue": null,
"rateTableDescription": null,
"genderRated": null,
"coverageTierId": null,
"renewalTimeBasis": null,
"rateTableId": null
}
}
},
"source_status": "warning",
"#version": "1",
"state": "processed",
"source_interaction_type": "sftp",
"processed_date": "2021-12-27T13:48:08.000Z",
"destination_response_result": null,
"exception_cause_source": "",
"destination": "aldera",
"file_interaction_type": "Outbound",
"dest_transaction_id": 3383,
"product": "vision",
"process": "Group",
"initialized_time": "2021-12-27T13:56:44.000Z",
"minparmet": "1",
"received_date": "2021-12-26T18:30:00.000Z",
"group_state": "GA",
"transaction_type": "add",
"destination_response_record": null,
"latest_transaction_date": "2021-12-27T13:48:08.000Z",
"source_file_id": 853,
"record_id": 100000003628,
"status": "success",
"#timestamp": "2022-08-05T10:23:49.002Z",
}
}
]
}
}
I'm planning to randomize the order of my query on quiz_question and choices but the quiz_question is the only one who gets randomized but the choices are still the same.
This is my code :
class QuestionByQuizID extends Controller
{
public function index(int $quiz_id){
$questions = quizQuestions::where('quiz_id', '=', $quiz_id)->with(['choices'])->inRandomOrder()->get();
if(is_null($questions)){
return response()->json('Record not found!', 401);
}
return response(['message'=>"Questions displayed successfully",
'quizQuestions'=>$questions],200);
}
}
This is the sample query using postman
"message": "Questions displayed successfully",
"quizQuestions": [
{
"id": 580,
"quiz_id": 36,
"question_num": 10,
"question_content": "What Are Those",
"correct_answer": null,
"created_by": null,
"updated_by": null,
"created_at": "2022-01-12T12:58:27.000000Z",
"updated_at": "2022-01-12T12:58:27.000000Z",
"question_image": null,
"tagalog_question": "Ano yan!",
"choices": [
{
"id": 1861,
"quiz_questions_id": 580,
"option": "choice 1",
"remarks": 0,
"created_at": "2022-01-12T12:58:27.000000Z",
"updated_at": "2022-01-12T12:58:27.000000Z",
"choices_image": null,
"tagalog_choices": "tagalog"
},
{
"id": 1862,
"quiz_questions_id": 580,
"option": "choice2",
"remarks": 0,
"created_at": "2022-01-12T12:58:27.000000Z",
"updated_at": "2022-01-12T12:58:27.000000Z",
"choices_image": null,
"tagalog_choices": "tagalog"
},
{
"id": 1863,
"quiz_questions_id": 580,
"option": "choice3",
"remarks": 0,
"created_at": "2022-01-12T12:58:27.000000Z",
"updated_at": "2022-01-12T12:58:27.000000Z",
"choices_image": null,
"tagalog_choices": "tagalog"
},
{
"id": 1864,
"quiz_questions_id": 580,
"option": "choice4",
"remarks": 1,
"created_at": "2022-01-12T12:58:27.000000Z",
"updated_at": "2022-01-12T12:58:27.000000Z",
"choices_image": null,
"tagalog_choices": "tagalog"
}
]
can anyone help me? thanks in advance.
You can do additional query conditions for the choices relationship:
$questions = quizQuestions::where('quiz_id', '=', $quiz_id)->with(['choices' => function($query){
$query->inRandomOrder();
}])->inRandomOrder()->get();
https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads
I am having a problem that i really can not understand. I am getting users in laravel where the role of the user is an admin or either a user. So when I am calling the users with the user role i am getting this (what I want):
[ { "id": 8, "username": "user", "firstname": "user", "lastname": "user", "email": "user#test.com", "email_verified_at": null, "phone_number": 6282223, "role": "user", "companyID": 1, "companyRole": "employee", "logged_in": 0, "created_at": "2021-11-03T15:25:29.000000Z", "updated_at": "2021-11-03T15:25:29.000000Z" }, { "id": 9, "username": "manager", "firstname": "manager", "lastname": "manager", "email": "manager#test.com", "email_verified_at": null, "phone_number": 623423, "role": "user", "companyID": 1, "companyRole": "manager", "logged_in": 0, "created_at": "2021-11-03T15:25:29.000000Z", "updated_at": "2021-11-03T15:25:29.000000Z" } ]
But when I call users with the role admin I am getting this (what i dont want):
{ "2": { "id": 10, "username": "mo", "firstname": "moemm", "lastname": "hus", "email": "hus#test.com", "email_verified_at": null, "phone_number": 6283, "role": "admin", "companyID": 0, "companyRole": "admin", "logged_in": 0, "created_at": "2021-11-03T15:25:29.000000Z", "updated_at": "2021-11-03T15:25:29.000000Z" } }
I littarly have the sme code for both but the only thing that changes is that i am calling different roles
public function getAllUsers(Request $request){
$users = User::all()->where('role', 'user');
return $users;
}
public function getAllAdmins(Request $request){
$admins = User::all()->where('role', 'admin');
return $admins;
}
And this is my api route for the both functions:
Route::get('/admins', [UserController::class, 'getAllAdmins']);
Route::get('/users', [UserController::class, 'getAllUsers']);
I'm making a chat api in Laravel. I have three models. User Chat and Message and they have their respective tables.
user table
id|name|email|other fields
chat table
id|user_id|receiver_id
message table
id|chat_id|message|created_at|updated_at|
Result is:
{
"data": [
{
"id": 7,
"chat_id": 5,
"message": "User 2 to 1",
"created_at": "2019-02-01 11:32:29",
"updated_at": "2019-02-01 11:32:29",
"chat": null
},
{
"id": 6,
"chat_id": 1,
"message": "Sample Chat",
"created_at": "2019-02-01 10:09:22",
"updated_at": "2019-02-01 10:09:22",
"chat": {
"id": 1,
"user_id": 1,
"receiver_id": 1,
"created_at": "2019-02-01 09:41:59",
"updated_at": "2019-02-01 10:09:22"
}
},
{
"id": 2,
"chat_id": 1,
"message": "Sample Chat",
"created_at": "2019-02-01 09:42:34",
"updated_at": "2019-02-01 09:42:34",
"chat": {
"id": 1,
"user_id": 1,
"receiver_id": 1,
"created_at": "2019-02-01 09:41:59",
"updated_at": "2019-02-01 10:09:22"
}
},
{
"id": 1,
"chat_id": 1,
"message": "Sample Chat",
"created_at": "2019-02-01 09:41:59",
"updated_at": "2019-02-01 09:41:59",
"chat": {
"id": 1,
"user_id": 1,
"receiver_id": 1,
"created_at": "2019-02-01 09:41:59",
"updated_at": "2019-02-01 10:09:22"
}
}
]
}
I've tried
$messages = Message::with(['chat' => function($query) use($user){
$query->where('user_id', $user->id);
}])
->latest()
->get();
As you can see the first data chat model is null.
I expect to only get all the messages connected to user id from the chat model.
use whereHas in your query will give only that chat belongs to purticular user.
$messages = Message::with(['chat'])->whereHas('chat', function ($query) {
$query->where('user_id', $user->id);
})
->latest()
->get();
I have few tables, they are room_types, rooms, rates, prices I want to get
room_types with rooms with rates and rates with prices where(price room_type_id = room_types.id) . I tried to do something like that
$roomTypeIds = [];
foreach ($hotel->rooms as $room) {
array_push($roomTypeIds, $room->roomtype->id);
}
$roomsByType = RoomType::with(['rooms' => function ($query) {
$query->with(['rates' => function($q) {
$q->with('policy', 'prices');
}]);
}])->whereIn('id', array_unique($roomTypeIds))->get();
but in this example Im getting all prices but I want to get only that prices which room_type_id IS EQUAL TO current room_type.id. So for that I have tried to do some think like this
$roomsByType = RoomType::with(['rooms' => function ($query) {
$query->with(['rates' => function($q) {
$q->with(['policy', 'prices' => function($q1) {
$q1->where('room_type_id', '');
}]);
}]);
}])->whereIn('id', array_unique($roomTypeIds))->get();
But I don't know what to put in where clouse. So are there any way to do this?
And result that I want to have is
[
{
"id": 2,
"name": "Twin/Double",
"created_at": "2017-12-11 08:56:16",
"updated_at": "2017-12-11 08:56:16",
"rooms": [
{
"id": 4,
"hotel_id": 1,
"room_type_id": 2,
"custom_name": null,
"room_name": "3",
"number_of_type": 2,
"number_of_bedrooms": null,
"number_of_livingrooms": null,
"number_of_bathrooms": null,
"created_at": "2017-12-12 06:37:34",
"updated_at": "2017-12-12 06:37:34",
"rates": [
{
"id": 4,
"user_id": 19,
"custom_name": "Default rate",
"price": 0,
"automatic": 0,
"rate_id": null,
"operand": null,
"amount": null,
"currency": null,
"policy_id": 1,
"meal_types": null,
"created_at": "2017-12-12 09:27:31",
"updated_at": "2017-12-12 09:27:29",
"pivot": {
"room_id": 4,
"rate_id": 4
},
"policy": {
"id": 1,
"name": "Free cancellation before 3 / 60 %",
"hotel_id": 1,
"free": 1,
"before_day": 3,
"before_day_price": 60,
"until_day_price": null,
"created_at": "2017-12-08 14:03:31",
"updated_at": "2017-12-08 14:03:31"
},
"prices": [
{
"id": 1,
"rate_id": 4,
"room_type_id": 2,
"from": "2017-12-01 09:18:46",
"to": "2017-12-18 09:18:57",
"amount": 100,
"created_at": "2017-12-18 09:19:11",
"updated_at": "2017-12-18 09:19:12"
},
{
"id": 3,
"rate_id": 4,
"room_type_id": 3,
"from": "2017-12-22 10:36:30",
"to": "2017-12-30 10:36:35",
"amount": 3000,
"created_at": null,
"updated_at": null
}
]
}
]
},
{
"id": 5,
"hotel_id": 1,
"room_type_id": 2,
"custom_name": null,
"room_name": "3",
"number_of_type": 2,
"number_of_bedrooms": null,
"number_of_livingrooms": null,
"number_of_bathrooms": null,
"created_at": "2017-12-12 06:37:34",
"updated_at": "2017-12-12 06:37:34",
"rates": [
{
"id": 4,
"user_id": 19,
"custom_name": "Default rate",
"price": 0,
"automatic": 0,
"rate_id": null,
"operand": null,
"amount": null,
"currency": null,
"policy_id": 1,
"meal_types": null,
"created_at": "2017-12-12 09:27:31",
"updated_at": "2017-12-12 09:27:29",
"pivot": {
"room_id": 5,
"rate_id": 4
},
"policy": {
"id": 1,
"name": "Free cancellation before 3 / 60 %",
"hotel_id": 1,
"free": 1,
"before_day": 3,
"before_day_price": 60,
"until_day_price": null,
"created_at": "2017-12-08 14:03:31",
"updated_at": "2017-12-08 14:03:31"
},
"prices": [
{
"id": 1,
"rate_id": 4,
"room_type_id": 2,
"from": "2017-12-01 09:18:46",
"to": "2017-12-18 09:18:57",
"amount": 100,
"created_at": "2017-12-18 09:19:11",
"updated_at": "2017-12-18 09:19:12"
},
{
"id": 3,
"rate_id": 4,
"room_type_id": 3,
"from": "2017-12-22 10:36:30",
"to": "2017-12-30 10:36:35",
"amount": 3000,
"created_at": null,
"updated_at": null
}
]
}
]
}
]
}
]
Look at the whereHas function instead of using with
https://laravel.com/docs/5.5/eloquent-relationships#querying-relations