Given this data:
{
"current_page": 1,
"data": [
{
"id": 1,
"delivery_id": 1,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Monday",
"location": {
"direction": "North"
}
}
}
},
{
"id": 2,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "09/28/2022",
"delivery" {
"day": "Friday",
"location": {
"direction": "South"
}
}
}
},
{
"id": 3,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Wednesday",
"location": {
"direction": "Northeast"
}
}
}
},
{
"id": 4,
"delivery_id": 3,
"deliveries_sub": {
"first_delivery_date": "09/02/2022",
"delivery" {
"day": "Tueday",
"location": {
"direction": "North"
}
}
}
}
],
"first_page_url": "http://localhost/mypage/list?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost/mypage/list?page=1",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://localhost/mypage/list?page=1",
"label": "1",
"active": true
},
{
"url": null,
"label": "Next »",
"active": false
}
],
"next_page_url": null,
"path": "http://localhost/mypage/list",
"per_page": 10,
"prev_page_url": null,
"to": 4,
"total": 4
}
I would like to sort it by the first_delivery_date. I have fetched this one using:
DeliveryDays::with('deliverySub.delivery.location')
->orderBy('deliverySub.first_delivery_date')
->paginate(10);
It seems that the orderBy clause is not working. I've also tried this approach:
DeliveryDays::with(['deliverySub' => function ($query) {
$query->orderBy('first_delivery_date');
}, 'deliverySub.delivery.location'])
->paginate(10);
But it also doesn't work. My desired result would be the sorted first_delivery_date data:
{
"current_page": 1,
"data": [
{
"id": 1,
"delivery_id": 1,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Monday",
"location": {
"direction": "North"
}
}
}
},
{
"id": 3,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Wednesday",
"location": {
"direction": "Northeast"
}
}
}
},
{
"id": 4,
"delivery_id": 3,
"deliveries_sub": {
"first_delivery_date": "09/02/2022",
"delivery" {
"day": "Tueday",
"location": {
"direction": "North"
}
}
}
},
{
"id": 2,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "09/28/2022",
"delivery" {
"day": "Friday",
"location": {
"direction": "South"
}
}
}
}
],
"first_page_url": "http://localhost/mypage/list?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost/mypage/list?page=1",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://localhost/mypage/list?page=1",
"label": "1",
"active": true
},
{
"url": null,
"label": "Next »",
"active": false
}
],
"next_page_url": null,
"path": "http://localhost/mypage/list",
"per_page": 10,
"prev_page_url": null,
"to": 4,
"total": 4
}
What would be the best approach for sorting the results? Is it within the eager load or after fetching the results? Thanks.
Try this
DeliveryDays::with(['deliverySub' => function($query){
$query->orderBy('first_delivery_date');
}, 'deliverySub.delivery.location'])->paginate(10);
Related
I want to filter out only entries from a list "NestedRecords" inside each "Record" entry.
Let's say I have these Id's of nested records which should be returned:
nestedRecordsToBeReturned = [1,3]
This is the given table with records and nestedRecords inside each record:
{
"Records": [
{
"Record": "record1",
"NestedRecords": [
{
"id": 1,
"label": "l1"
},
{
"id": 2,
"label": "l2"
},
{
"id": 3,
"label": "l3"
},
{
"id": 4,
"label": "l4"
},
{
"id": 6,
"label": "l6"
}
]
},
{
"Record": "record2",
"Records": [
{
"id": 1,
"label": "l1"
},
{
"id": 2,
"label": "l2"
},
{
"id": 4,
"label": "l4"
},
{
"id": 7,
"label": "l7"
}
]
},
{
"Record": "record3",
"Records": [
{
"id": 2,
"label": "l2"
},
{
"id": 3,
"label": "l3"
},
{
"id": 9,
"label": "l9"
},
{
"id": 10,
"label": "l10"
}
]
}
]
}
This is what I want to be returned:
{
"Records": [
{
"Record": "record1",
"NestedRecords": [
{
"id": 1,
"label": "l1"
},
{
"id": 3,
"label": "l3"
},
]
},
{
"Record": "record2",
"Records": [
{
"id": 1,
"label": "l1"
},
]
},
{
"Record": "record3",
"Records": [
{
"id": 3,
"label": "l3"
},
]
}
]
}
How do I achieve this with an elastic query?
Is it even possible to filter a list which is embedded in a document?
I have a list of nested multi level objects.
This is from eloquent query with relationship.
A Place has many Orders
An Order has many Details and one Customer
A Detail has one product and one deliver Period
[
{
"place_id": 1,
"name": "A Building",
"address": "A Street, A Ward, A City",
"orders": [
{
"customer": {
"id": 1,
"name": "Peter",
"phone": "011111111"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
},
{
"customer": {
"id": 2,
"name": "Mary",
"phone": "022222222222"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 3,
"product": {
"name": "Glass of Milk Shake"
}
}
]
}
]
},
{
"place_id": 2,
"name": "B Building",
"address": "B Street, B Ward, B City",
"orders": [
{
"customer": {
"id": 3,
"name": "Catherine",
"phone": "0333333333"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 2,
"product": {
"name": "A Cup of Coffee"
}
}
]
},
{
"customer": {
"id": 4,
"name": "Tom",
"phone": "04444444444444"
},
"order_details": [
{
"period_id": 2,
"period": {
"id": 1,
"start_time": "10:00:00",
"end_time": "10:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
}
]
}
]
I need group this list by "period" then "place"
Expect result:
[
{
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00",
"places": [
{
"place_id": 1,
"name": "A Building",
"address": "A Street, A Ward, A City",
"orders": [
{
"customer": {
"id": 1,
"name": "Peter",
"phone": "011111111"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
},
{
"customer": {
"id": 2,
"name": "Mary",
"phone": "022222222222"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 3,
"product": {
"name": "Glass of Milk Shake"
}
}
]
}
]
},
{
"place_id": 2,
"name": "B Building",
"address": "B Street, B Ward, B City",
"orders": [
{
"customer": {
"id": 3,
"name": "Catherine",
"phone": "0333333333"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 2,
"product": {
"name": "A Cup of Coffee"
}
}
]
}
]
}
]
},
{
"id": 2,
"start_time": "10:00:00",
"end_time": "10:30:00",
"places": [
{
"place_id": 2,
"name": "B Building",
"address": "B Street, B Ward, B City",
"orders": [
{
"customer": {
"id": 4,
"name": "Tom",
"phone": "04444444444444"
},
"order_details": [
{
"period_id": 2,
"period": {
"id": 1,
"start_time": "10:00:00",
"end_time": "10:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
}
]
}
]
}
]
Here is my detail code :
class Place extends Model
{
public function orderDetails()
{
return $this->hasManyThrough(OrderDetail::class,
Order::class,'place_id','order_id','id','id');
}
}
class Period extends Model
{
public function details()
{
return $this->hasMany(OrderDetail::class, 'period_id', 'id');
}
}
class Order extends Model
{
public function place()
{
return $this->belongsTo(Place::class, 'place_id', 'id');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function details()
{
return $this->hasMany(OrderDetail::class, 'order_id', 'id');
}
}
class OrderDetail extends Model
{
public function period()
{
return $this->belongsTo(Period::class, 'period_id', 'id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id', 'id');
}
public function order()
{
return $this->belongsTo(Order::class, 'order_id', 'id');
}
}
Place::with(['orderDetails', 'orderDetails.product', 'orderDetails.period', 'orderDetails.order.user'])->get()->toArray();
I m using laravel so better use collection helper of laravel to solve this faster.
Please help me . Thanks
Updated:
i used another query and group
In OrderDetail model add:
protected $appends = ['place_id'];
public function getPlaceIdAttribute()
{
return $this->order->place_id;
}
Then use this query i can group period than place but it 's only contain id.
OrderDetail::with(['order','order.user','order.place','product','period'])->get()->groupBy(['period_id','place_id'])->toArray();
I solved this .
OrderDetail::with(['order','order.user','order.place','product','period'])->get()->groupBy(['period.id','order.place.id'])->map(function ($periods) {
$period = $periods->first()->first()->period;
return [
"start_time" => $period->start_time,
"end_time" => $period->end_time,
"places" => $periods->map(function ($places) {
$place = $places->first()->place;
return [
"name" => $place->name,
"address" => $place->address,
"details" => $places->toArray()
];
})->toArray()
];
})->toArray();
i have a collection data
{
"success": true,
"doctor": [
{
"id": 1,
"name": "Dr. Mayank",
"dob": "1975-01-01",
"about": "This is description",
"status": 1,
"rating": 2,
"rating_given_by": 1,
"alternative_number": "7686876876",
"profile_photo": [],
"speciality": [
{
"id": 3,
"name": "Acupuncture",
"image": null,
"dashboard_flag": 1
},
{
"id": 4,
"name": "Acupuncturist",
"image": null,
"dashboard_flag": 1
},
{
"id": 1,
"name": "Accident and emergency medicine",
"image": "http://192.168.16.21/remidify/media/174/detail.png",
"dashboard_flag": 1
}
],
"service": [
{
"id": 78,
"name": "Correction of gummy smile",
"cost": "12.00"
},
{
"id": 77,
"name": "Dental aesthetics",
"cost": "43.00"
}
],
"clinics": [
{
"id": 1,
"name": "akram",
"entity_id": 1,
"entity_type": "App\Doctor",
"contact_number": "2132132132132",
"status": 0,
"consultancy_fee": "12.00",
"available_today": "No",
"owner_name": "Dr. Mayank",
"pivot": {
"doctor_id": 1,
"clinic_id": 1
},
"address": {
"id": 1,
"address_1": "asdasdasdsa",
"address_2": "",
"locality": "downtown",
"city": "noida",
"state": "up",
"postal_code": "41561566"
},
"speciality": [],
"service": [
{
"id": 11,
"name": "Laminates",
"cost": "20.00"
},
{
"id": 12,
"name": "Dental surgery",
"cost": "300.00"
}
],
"clinic_image": [
{
"id": 7,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "clinic_image",
"file_name": "1494863957588.566162.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/7/1494863957588.566162.jpg"
}
],
"id_image": [
{
"id": 8,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "id_image",
"file_name": "1494863966218.348877.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/8/1494863966218.348877.jpg"
}
],
"location": {
"id": 1,
"latitude": 0,
"longitude": 0,
"entity_id": 1,
"entity_type": "App\Clinic",
"created_at": "2017-05-16 03:00:10",
"updated_at": "2017-05-16 03:00:10"
},
"clinic_timings": [
{
"day": "sun",
"opens_at": "09:28:00",
"closes_at": "21:28:00"
}
]
}
],
"education": [
{
"id": 19,
"degree": "MBBS",
"university": "Univercity",
"year": "2017",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:11",
"updated_at": "2017-05-16 05:44:11",
"location": "Delhi"
}
],
"experience": [
{
"id": 19,
"hospital": "Hospital name",
"post": "pta ni hai",
"from": "1970-01-01",
"to": "0000-00-00",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:12",
"updated_at": "2017-05-16 05:44:12",
"location": "Locations12",
"is_currently_working": 1
}
],
"registration": {
"id": 1,
"registration_number": "Reg # 2324324",
"registration_year": 1975,
"registration_council": "Council",
"experience": null,
"doctor_id": 1,
"created_at": "2017-05-16 02:56:37",
"updated_at": "2017-05-16 02:56:37",
"adhaar_number": "232131231232",
"id_proof": [
{
"id": 2,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "id_proof",
"file_name": "1494863680447.329102.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/2/1494863680447.329102.jpg"
}
],
"registration_proof": [
{
"id": 3,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "registration_proof",
"file_name": "1494863687436.266846.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/3/1494863687436.266846.jpg"
}
],
"qualification_proof": [
{
"id": 4,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "qualification_proof",
"file_name": "1494863695576.803955.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/4/1494863695576.803955.jpg"
}
]
},
"preference": {
"availability": 1,
"appointment_confirmation_method": "manual",
"average_time": 7,
"holiday_from": null,
"holiday_till": null,
"patients_per_hour": null,
"preferred_appointment_type": "timeslot",
"appointment_frequency": null,
"preferred_payment_method": [
{
"payment_method": "cash"
},
{
"payment_method": "online"
}
]
},
"user": null,
"doctor_clinic": [
{
"doctor_id": 1,
"clinic_id": 1,
"consultancy_fee": "12.00",
"deleted_at": null,
"workdays": [
{
"day": "sun",
"available": 1,
"workhours": [
{
"from": "09:28:00",
"to": "21:28:00"
}
]
}
],
"service": []
}
]
}
]
}
Now how can i find the doctors whose "about or specialty name" matches with some given search string.
Thanks in advance.
Try using dd() helper and use like this below
$youcollectionvariables = { "success": true, "doctor": [ { "id": 1, "name": "Dr. Mayank", "dob": "1975-01-01"................. }
dd($youcollectionvariables)
you will see a formatted data and can find what you need clearly.
Hope that helps.
I am trying to persist some objects that have a composite id. If I am only sending an array with one element it works fine, but it the array has more than one it throws an exception when saving the first one. public boolean
addParamsToChart(List<ChartParams> chartParams, Long chartId) {
List<ChartParams> chartParamsList = new ArrayList<>();
for(ChartParams chartParam: chartParams) {
ChartParamsId id = new ChartParamsId();
// id.setChartId(chartId);
id.setChartId(chartParam.getChart().getId());
id.setParamId(chartParam.getParam().getId());
id.setContextSourceId(chartParam.getContextSource().getId());
chartParam.setChartParamsId(id);
if(chartParamsRepository.save(chartParams) !=null) {
chartParamsList.add(chartParam);
}
}
if(chartParamsList.size()!=chartParams.size()) {
// something went wrong, delete previous inserted
deleteChartParams(chartParamsList);
chartRepository.delete(chartId);
return false;
}
return true;
}
[{
"chart": {
"id": 49,
"cv": {
"id": 1,
"name": "Money",
"category": {
"id": 1,
"name": "Euros"
},
"definition": "\"European curreny.\" [EU:euro]",
"enabled": true,
"cvid": "CC:1010"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Euro saving charts"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 3,
"name": "euro",
"internal": "eu",
"abbreviatedName": "eu"
} }]
But for this having two objects inside instead of one is not working, throwing an exception at the first save.
[{
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 6,
"name": "stock",
"internal": "st",
"abbreviatedName": "st"
} }, {
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 7,
"name": "Sold stock",
"internal": "st_sold",
"abbreviatedSequence": "st_sold"
} }]
The exception I got is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class eu.stocks.chart.chartParams.ChartParams
I am trying to display a chart with tool tips but I cannot seem to figure out how you put in tool tips when you write your own JSON for the chart.
Is this possible?
Here is the JSON I have written so far:
var days_of_week_chart = {
"elements": [
{
"type": "bar",
"values": [33,0,0,0,0,0,0,],
}
],
"title": {
"text": "Visitors By Day of Week"
},
"x_axis":{
"stroke":1,
"tick_height":10,
"colour":"#d000d0",
"grid_colour":"#00ff00",
"labels": {
"labels": ["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday",]
}
},
"y_axis": {
"stroke": 4,
"steps": 5,
"tick_length": 5,
"colour": "#d000d0",
"grid_colour": "#00ff00",
"offset": 0,
"max": 33 }
};
You need to have the tooltip element, and tip element in the data series:
{ "elements": [ { "type": "hbar", "values": [ { "right": 4 }, { "right": 8 }, { "right": 3 }, { "right": 4 }, { "right": 7 }, { "right": 8 } ], "colour": "#86BBEF", "tip": "Months: #val#" } ], "title": { "text": "Total hours on project mayhem" }, "x_axis": { "offset": false, "min": 0, "max": 10 }, "y_axis": { "offset": 1, "labels": [ "Jeff", "Geoff", "Bob", "Terry", "Duncan", "monk.e.boy" ] }, "tooltip": { "mouse": 2, "stroke": 1, "colour": "#000000", "background": "#ffffff" } }