Hello <@U0259FCN8AV> :wave: &gt; i ran into a sce...
# community-help
a
Hello @Channel Tools 👋
i ran into a scenario related to references/joins in typesense,
my scenario is get all the users with their cars. which could be done in laravel in that simple way below using one to many joins,
$searchParameters = [
'per_page' => 250,
'sort_by' => 'scheduled_date:asc',
'filter_by' => '$cars(id:*)',
'q' => $searchQuery,
'include_fields' => '$cars(*),
'query_by' => 'lead_id',
];
using this i get all the users with their cars (one to many).
But now i have to apply filter that get all the users against which one its car has Booked status. so when i am doing it: 'filter_by' => '$cars(status:Booked)', it get user only one car, but i want to get all the cars.
Desired Result is:
$result = [{
"id": "user1",
"cars": [
{ "id": "car_1", "status": "Booked" },
Copy code
{
  "id": "car_2",
  "status": "Available"
}
]
}]
Result i am getiing
$result = [{
"id": "user1",
"cars": [
Copy code
{
  "id": "car_1",
  "status": "Booked"
}
]
}]
Can we do something like this? i will appreciate your help, Thankyou
f
For that use-case, I would advise you add an additional boolean field called
hasBooked
. You set that to true if the user has a car booked, and then add that filter to the JOIN condition
a
Since i have multiple statuses, boolean wont work.
f
What you were describing is if a user has at least one booked car, return that list. The status field should remain. Is there something I didn't understand correctly?
a
'filter_by' => '$cars(status: AnyAppliedFilter )',
if user has any car that matches the result then bring that user with all of its cars
f
That filter will return the user with all of their cars that have that status. In the example you first posted, the desired outcome will include cars that have other statuses. Is status optional? Also, is status an enum field, or do you create different statuses for each user / car?
h
@Areeba Ayub You can break your query into two steps: • First use
filter_by: $cars(status:Booked)
and collect all the user_ids that are returned. • Then send
filter_by: id:[<ids from first step>] && $cars(id:*)
There is no way to get the details of all the cars where at least one car has status Booked in one query.
a
ok i wanted to know if there is a way. thankyou @Harpreet Sangar @Fanis Tharropoulos