How to insert header tags to API Json - laravel

Hello im making a GET api in laravel that shows values from database and im trying to make it that the returned json result to have a header tag an example is this public api
https://dummy.restapiexample.com/api/v1/employees
As you can see this api shows the status:success and then data: , this is my code:
function getDevice()
{
return Client::all();
}
And this is the result i get in postman:
data [ //here to have a header
{
"id": 1,
"name": "Mr. Omari Schaefer DVM",
"email": "kulas.julian#example.net",
"phone_number": "0697830800",
"age": "29",
"salary": "150",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 2,
"name": "Hattie Brakus",
"email": "deckow.coby#example.com",
"phone_number": "0697830800",
"age": "30",
"salary": "565",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 3,
"name": "Sabrina Rosenbaum",
"email": "kautzer.madison#example.net",
"phone_number": "0697830800",
"age": "31",
"salary": "254",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 4,
"name": "Ms. Fiona Fritsch",
"email": "juwan.damore#example.net",
"phone_number": "0697830800",
"age": "32",
"salary": "029",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 5,
"name": "Michael Dooley",
"email": "hayes.reinhold#example.org",
"phone_number": "0697830800",
"age": "33",
"salary": "265",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 6,
"name": "Prof. Kelley Koepp Jr.",
"email": "kutch.jessie#example.org",
"phone_number": "0697830800",
"age": "34",
"salary": "688",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 7,
"name": "Herminia McClure PhD",
"email": "ywillms#example.com",
"phone_number": "0697830800",
"age": "35",
"salary": "103",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 8,
"name": "Morton Considine",
"email": "lesly.pfeffer#example.com",
"phone_number": "0697830800",
"age": "36",
"salary": "804",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 9,
"name": "Katlyn Muller",
"email": "peter93#example.net",
"phone_number": "0697830800",
"age": "37",
"salary": "695",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 10,
"name": "Wilber Stehr",
"email": "bmckenzie#example.net",
"phone_number": "0697830800",
"age": "38",
"salary": "941",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 11,
"name": "Test User",
"email": "test#example.com",
"phone_number": "0697830800",
"age": "60",
"salary": "448",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
}
]
What im trying to do is:
[
{
"id": 1,
"name": "Mr. Omari Schaefer DVM",
"email": "kulas.julian#example.net",
"phone_number": "0697830800",
"age": "29",
"salary": "150",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 2,
"name": "Hattie Brakus",
"email": "deckow.coby#example.com",
"phone_number": "0697830800",
"age": "30",
"salary": "565",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 3,
"name": "Sabrina Rosenbaum",
"email": "kautzer.madison#example.net",
"phone_number": "0697830800",
"age": "31",
"salary": "254",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 4,
"name": "Ms. Fiona Fritsch",
"email": "juwan.damore#example.net",
"phone_number": "0697830800",
"age": "32",
"salary": "029",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 5,
"name": "Michael Dooley",
"email": "hayes.reinhold#example.org",
"phone_number": "0697830800",
"age": "33",
"salary": "265",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 6,
"name": "Prof. Kelley Koepp Jr.",
"email": "kutch.jessie#example.org",
"phone_number": "0697830800",
"age": "34",
"salary": "688",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 7,
"name": "Herminia McClure PhD",
"email": "ywillms#example.com",
"phone_number": "0697830800",
"age": "35",
"salary": "103",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 8,
"name": "Morton Considine",
"email": "lesly.pfeffer#example.com",
"phone_number": "0697830800",
"age": "36",
"salary": "804",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 9,
"name": "Katlyn Muller",
"email": "peter93#example.net",
"phone_number": "0697830800",
"age": "37",
"salary": "695",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 10,
"name": "Wilber Stehr",
"email": "bmckenzie#example.net",
"phone_number": "0697830800",
"age": "38",
"salary": "941",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 11,
"name": "Test User",
"email": "test#example.com",
"phone_number": "0697830800",
"age": "60",
"salary": "448",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
}
]

function getDevice()
{
return ['data' =>
Client::all(),
};

Please refer to the below solution for every response :
return response()->json('status' => 'success','message' => 'Client listing successfully.','data' => Client::all());
If you know how to use the helper function in Laravel then Please used the below for all responses:
Example :
In Controller, Just add the below line :
return apiResponse('success','Client listed successfully',Client::all()]);
if (!function_exists('apiResponse')) {
function apiResponse($status = null, $message = null, $data = null, $code = 200, $extra = []) {
$response = [
'status' => $status,
'message' => $message,
'data' => $data,
];
if(!empty($extra)){
$response = array_merge($response,$extra);
}
return response()->json($response, $code);
}
}
I think this will help you a lot.

Related

How to get one element from array of objects in elasticsearch

I have a products index which has an offers field. Offers is an array of objects.
I want to return one offer by seller_id in an array or in a new field.
Input:
with seller_id=5
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offers":[
{
"product_id":"138",
"seller_id":"1",
"sale_status":"2",
"external_id":"1267631",
"store_id":"2",
"qty":"44",
"storage_address":"",
"base_price":"312.84",
"updated_at":"2022-11-15T10:42:49.000000Z",
"created_at":"2022-11-15T10:42:49.000000Z",
"id":74
},
{
"product_id":"138",
"seller_id":"2",
"sale_status":"1",
"external_id":"2795841",
"store_id":"2",
"qty":"1",
"storage_address":"",
"base_price":"1812.3",
"updated_at":"2022-11-15T10:44:50.000000Z",
"created_at":"2022-11-15T10:44:50.000000Z",
"id":76
},
{
"product_id":"138",
"seller_id":"3",
"sale_status":"1",
"external_id":"32219",
"store_id":"1",
"qty":"32",
"storage_address":"",
"base_price":"1556.25",
"updated_at":"2022-11-15T10:50:16.000000Z",
"created_at":"2022-11-15T10:50:16.000000Z",
"id":77
},
{
"product_id":"138",
"seller_id":"4",
"sale_status":"1",
"external_id":"967427",
"store_id":"1",
"qty":"35",
"storage_address":"",
"base_price":"137.62",
"updated_at":"2022-11-15T10:50:18.000000Z",
"created_at":"2022-11-15T10:50:18.000000Z",
"id":78
},
{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
},
{
"product_id":"138",
"seller_id":"6",
"sale_status":"1",
"external_id":"522912",
"store_id":"1",
"qty":"61",
"storage_address":"",
"base_price":"306.39",
"updated_at":"2022-11-15T10:50:20.000000Z",
"created_at":"2022-11-15T10:50:20.000000Z",
"id":80
}
]
}
}
Expected:
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offers":[
{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
}
]
}
}
Or expected:
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offer":{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
}
}
}
Thanks for help!
If the offers field is nested type you can to use inner hits to get only object match in list.
The object you expected will in "inner_hits" response.
Query
GET idx_nested/_search?filter_path=hits.hits
{
"query": {
"nested": {
"path": "offers",
"query": {
"match": {
"offers.seller_id": "5"
}
},
"inner_hits": {}
}
}
}
Response:
{
"hits": {
"hits": [
{
"_index": "idx_nested",
"_id": "kYyYf4QBgXg8h_rctd1z",
"_score": 1.540445,
"_source": {
"is_adult": false,
"status_id": 3,
"allow_publish": false,
"name": "Consequuntur expedita sit perferendis est.",
"category_id": 816,
"brand_id": 363,
"description": "Nec.",
"type": 3,
"vendor_code": "4968258909901",
"barcode": "98735976",
"code": "consequuntur-expedita-sit-perferendis-est",
"updated_at": "2022-11-15T10:42:33.000000Z",
"created_at": "2022-11-15T10:42:33.000000Z",
"id": 138,
"offers": [
{
"product_id": "138",
"seller_id": "1",
"sale_status": "2",
"external_id": "1267631",
"store_id": "2",
"qty": "44",
"storage_address": "",
"base_price": "312.84",
"updated_at": "2022-11-15T10:42:49.000000Z",
"created_at": "2022-11-15T10:42:49.000000Z",
"id": 74
},
{
"product_id": "138",
"seller_id": "2",
"sale_status": "1",
"external_id": "2795841",
"store_id": "2",
"qty": "1",
"storage_address": "",
"base_price": "1812.3",
"updated_at": "2022-11-15T10:44:50.000000Z",
"created_at": "2022-11-15T10:44:50.000000Z",
"id": 76
},
{
"product_id": "138",
"seller_id": "3",
"sale_status": "1",
"external_id": "32219",
"store_id": "1",
"qty": "32",
"storage_address": "",
"base_price": "1556.25",
"updated_at": "2022-11-15T10:50:16.000000Z",
"created_at": "2022-11-15T10:50:16.000000Z",
"id": 77
},
{
"product_id": "138",
"seller_id": "4",
"sale_status": "1",
"external_id": "967427",
"store_id": "1",
"qty": "35",
"storage_address": "",
"base_price": "137.62",
"updated_at": "2022-11-15T10:50:18.000000Z",
"created_at": "2022-11-15T10:50:18.000000Z",
"id": 78
},
{
"product_id": "138",
"seller_id": "5",
"sale_status": "2",
"external_id": "209466",
"store_id": "1",
"qty": "45",
"storage_address": "",
"base_price": "187.03",
"updated_at": "2022-11-15T10:50:19.000000Z",
"created_at": "2022-11-15T10:50:19.000000Z",
"id": 79
},
{
"product_id": "138",
"seller_id": "6",
"sale_status": "1",
"external_id": "522912",
"store_id": "1",
"qty": "61",
"storage_address": "",
"base_price": "306.39",
"updated_at": "2022-11-15T10:50:20.000000Z",
"created_at": "2022-11-15T10:50:20.000000Z",
"id": 80
}
]
},
"inner_hits": {
"offers": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "idx_nested",
"_id": "kYyYf4QBgXg8h_rctd1z",
"_nested": {
"field": "offers",
"offset": 4
},
"_score": 1.540445,
"_source": {
"store_id": "1",
"updated_at": "2022-11-15T10:50:19.000000Z",
"storage_address": "",
"product_id": "138",
"qty": "45",
"base_price": "187.03",
"sale_status": "2",
"created_at": "2022-11-15T10:50:19.000000Z",
"external_id": "209466",
"id": 79,
"seller_id": "5"
}
}
]
}
}
}
}
]
}
}

Removing an object in an array of objects in elasticsearch document using an unique parameter in that object

I've got a document in elasticsearch which is like this:
{
"_index": "user",
"_type": "_doc",
"_id": "20",
"_score": 1,
"_source": {
"id": "20",
"gender": null,
"uuid": "68de0b74-cdf6-4e21-a046-6876e569e4e3",
"first_name": null,
"last_name": null,
"nick_name": null,
"email": null,
"country_code": "98",
"mobile": "9xxx7",
"password": null,
"old_password": null,
"birthdate": null,
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"old_data": null,
"devices": [
{
"updated_at": "2021-12-14T12:11:26.000000Z",
"serial": "sr_31",
"vendor": "folan",
"created_at": "2021-12-14T12:11:26.000000Z",
"model": "device_model",
"id": 4,
"device_app": {
"device_id": 4,
"updated_at": "2021-12-14T12:11:26.000000Z",
"created_at": "2021-12-14T12:11:26.000000Z",
"id": 4,
"uuid": "f60f7df0-5cd6-11ec-b71d-bdfd039f50b3"
},
"uuid": "f60a2ae0-5cd6-11ec-81cb-0128d6059ef1"
},
{
"updated_at": "2021-12-14T12:13:07.000000Z",
"serial": "sr_32",
"vendor": "folan",
"created_at": "2021-12-14T12:13:07.000000Z",
"model": "device_model",
"id": 5,
"device_app": {
"device_id": 5,
"updated_at": "2021-12-14T12:13:07.000000Z",
"created_at": "2021-12-14T12:13:07.000000Z",
"id": 5,
"uuid": "32481b80-5cd7-11ec-ba02-c33a09f165eb"
},
"uuid": "3242f860-5cd7-11ec-9d54-c3339177d9bb"
}
]
}
}
As you can see, I've got a field named devices which is an array of objects. I want to delete its first element not by selecting the index number of the array but with the content of the array, meaning that **I want to give the script the serial field (which is unique for each device) and delete the whole object of the corresponding object.
Let's say, I want some input to be sr_31 and the output would be something like this (only the object containgin sr_31 is omitted):
{
"_index": "user",
"_type": "_doc",
"_id": "20",
"_score": 1,
"_source": {
"id": "20",
"gender": null,
"uuid": "68de0b74-cdf6-4e21-a046-6876e569e4e3",
"first_name": null,
"last_name": null,
"nick_name": null,
"email": null,
"country_code": "98",
"mobile": "9xxx7",
"password": null,
"old_password": null,
"birthdate": null,
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"old_data": null,
"devices": [
{
"updated_at": "2021-12-14T12:13:07.000000Z",
"serial": "sr_32",
"vendor": "folan",
"created_at": "2021-12-14T12:13:07.000000Z",
"model": "device_model",
"id": 5,
"device_app": {
"device_id": 5,
"updated_at": "2021-12-14T12:13:07.000000Z",
"created_at": "2021-12-14T12:13:07.000000Z",
"id": 5,
"uuid": "32481b80-5cd7-11ec-ba02-c33a09f165eb"
},
"uuid": "3242f860-5cd7-11ec-9d54-c3339177d9bb"
}
]
}
}
I have read the document for updating a doc already, but there seems no example for this situation.
What you need is like this:
POST index/_doc/20/_update
{
"script": {
"lang": "painless",
"source": "ctx._source.devices.removeIf(device -> device.id == 'sr_31')"
}
}

Laravel apply sortBy to a related models collection

I am using the below code:
$greatDeals = $greatDealsinitial->sortBy(function ($deal, $key) {
return $deal->hotel->room[0]->price;
});
Here room is an array, I want to sort this collection by price in the room array.
But I can't use return $row->hotel->room->price;
So how can I sort this?
my collection :
[
{
"id": 2,
"hotel_id": 1,
"deal_code": "sfg",
"date": "2018-04-13 00:00:00",
"discount": 12,
"status": 1,
"created_at": "2018-04-13 11:21:18",
"updated_at": "2018-04-13 11:21:18",
"deleted_at": null,
"client_id": 1,
"hotel": {
"id": 1,
"name": "Aloft Dongguan Songshan Lake",
"address": "Dongguan, China, 511700",
"phone": "76982106666",
"country": "101",
"country_name": "India",
"state": "35",
"state_name": "Tamil Nadu",
"city": "3551",
"city_name": "Abiramam",
"area": null,
"pincode": "511700",
"nearest_airport": null,
"nearest_railway": null,
"star": null,
"check_in": null,
"check_out": null,
"lat": "22.929869",
"lng": "113.89232800000002",
"description": null,
"astatus": "1",
"status": "0",
"created_at": "2018-04-13 10:27:30",
"updated_at": "2018-04-13 10:27:48",
"client_id": 1,
"hotel_age": null,
"hotel_privilege": "asdf",
"departure": "sdaf",
"checkout_fee": "asdf",
"book_policy": "sadf",
"hotel_secured": null,
"hotel_pets": null,
"hotel_other": null,
"room": [
{
"id": 3,
"roomtype_id": 2,
"room_count": null,
"max_adult": 2,
"max_children": 1,
"capacity": 3,
"room_number": null,
"floor": null,
"number": null,
"name": null,
"city_name": "Abiramam",
"city": 3551,
"price": "200.00",
"extra_bed_price": null,
"astatus": "1",
"status": "0",
"created_at": "2018-04-13 12:37:11",
"updated_at": "2018-04-13 12:37:52",
"deleted_at": null,
"client_id": 1,
"hotel_id": 1
},
{
"id": 1,
"roomtype_id": 1,
"room_count": null,
"max_adult": 2,
"max_children": 1,
"capacity": 3,
"room_number": null,
"floor": null,
"number": null,
"name": null,
"city_name": "Abiramam",
"city": 3551,
"price": "435.00",
"extra_bed_price": null,
"astatus": "1",
"status": "0",
"created_at": "2018-04-13 10:33:24",
"updated_at": "2018-04-13 11:21:38",
"deleted_at": null,
"client_id": 1,
"hotel_id": 1
}
]
}
},
{
"id": 4,
"hotel_id": 2,
"deal_code": "ertgerf",
"date": "2018-04-13 00:00:00",
"discount": 1,
"status": 1,
"created_at": "2018-04-13 11:47:39",
"updated_at": "2018-04-13 11:53:45",
"deleted_at": null,
"client_id": 2,
"hotel": {
"id": 2,
"name": "Temperance Lane",
"address": "Temperance Ln, Sydney NSW 2000, Australia",
"phone": "9842814927",
"country": "14",
"country_name": "Austria",
"state": "281",
"state_name": "Carinthia",
"city": "6843",
"city_name": "Maria Rain",
"area": "test",
"pincode": "2000",
"nearest_airport": "demo",
"nearest_railway": "test",
"star": null,
"check_in": "09:30 AM",
"check_out": "04:30 PM",
"lat": "-33.86963590000001",
"lng": "151.20667979999996",
"description": "test description",
"astatus": "1",
"status": "0",
"created_at": "2018-04-13 10:43:16",
"updated_at": "2018-04-13 11:41:25",
"client_id": 2,
"hotel_age": null,
"hotel_privilege": "testtesttesttesttesttesttesttesttesttesttest",
"departure": "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest",
"checkout_fee": "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest",
"book_policy": "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest",
"hotel_secured": null,
"hotel_pets": null,
"hotel_other": "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest",
"room": [
{
"id": 2,
"roomtype_id": 1,
"room_count": null,
"max_adult": 2,
"max_children": 3,
"capacity": 5,
"room_number": null,
"floor": null,
"number": null,
"name": null,
"city_name": "Maria Rain",
"city": 6843,
"price": "345.00",
"extra_bed_price": null,
"astatus": "1",
"status": "0",
"created_at": "2018-04-13 10:46:04",
"updated_at": "2018-04-13 10:47:05",
"deleted_at": null,
"client_id": 2,
"hotel_id": 2
}
]
}
}
]
This sorts the rooms of each hotel per price:
$greatDealsinitial->each(function($deal) {
$deal->hotel->setRelation('room', $deal->hotel->room->sortBy('price'));
});
$greatDeals = $greatDealsinitial->hotel->room->sortBy('price')
if this array looks
[
['price' => 0],
['price' => 1],
['price' => 3]
]

How to filter laravel collection from given data

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.

Laravel 5.0 Eloquent Need query to list user, posts and related comments in facebook style

My table's fields are as below :
users : id email first_name last_name (rest are there but need only these)
activity_stream : id user_id post created_at updated_at deleted_at
activity_stream_comments : id post_id user_id comment created_at updated_at deleted_at
I need listing of all the users posts with related comments just like you see in facebook, it shows various users posts with posts comments so for now I basically need to list down all the users posts with related comments.
I am trying the below query but it gives only the post that have comments that too in different sets :
$results = DB::table('activity_stream')
->join('users', 'users.id', '=', 'activity_stream.user_id')
->join('activity_stream_comments', 'activity_stream.id', '=', 'activity_stream_comments.post_id')
->select('activity_stream.*', 'users.first_name','users.last_name', 'activity_stream_comments.comment')
/*->where('activity_stream.user_id', $user_id)*/
->whereNull('activity_stream.deleted_at')
->orderBy('activity_stream.created_at', 'desc')
->get();
return response()->json($results);
And the return is :
[
{
"id": "97",
"user_id": "30",
"post": "hi how are you?",
"created_at": "2015-11-20 04:35:11",
"updated_at": "2015-11-20 04:35:11",
"deleted_at": null,
"first_name": "Abhijan",
"last_name": "Pal",
"comment": "test"
},
{
"id": "14",
"user_id": "49",
"post": "Test Post",
"created_at": "2015-11-13 04:51:53",
"updated_at": "2015-11-13 04:51:53",
"deleted_at": null,
"first_name": "Nazir",
"last_name": "Hussain",
"comment": "This is my first comment"
},
{
"id": "14",
"user_id": "49",
"post": "Test Post",
"created_at": "2015-11-13 04:51:53",
"updated_at": "2015-11-13 04:51:53",
"deleted_at": null,
"first_name": "Nazir",
"last_name": "Hussain",
"comment": "This is my 2nd comment"
}
]
Improvement :
$results = User::with('activity_stream.activity_stream_comments')->find($user_id);
return response()->json($results);
While using relationship and using above query I get the response as below :
{
"id": "49",
"email": "nazir.2cool#gmail.com",
"permissions": null,
"last_login": "2015-11-19 09:32:36",
"first_name": "Nazir",
"last_name": "Hussain",
"created_at": "2015-11-03 12:36:56",
"updated_at": "2015-11-19 09:32:36",
"facebook_id": "1051400994880427",
"facebook_token": "CAAX3UySfMDcBAFs8glgjz9p1Qdt0M1zwRrmZB9y4WkEPOEldIM5BKVURut8pcg9ios6GiZBschXWBKCqM6HdyZCIxkih6Rwh2SSABrzAHZCzGZBqcISDIlUwnNp73zF8PEZAgXKdzhZAQtfjZAeawUlAHTNpt2RyXTPoeH0ETj8jSclIxMSXPxfbDlnmNjoJXVwZD",
"facebook_profile_url": "",
"facebook_avatar": "https://graph.facebook.com/v2.5/1051400994880427/picture?width=1920",
"google_id": "112519651577038840839",
"google_token": "ya29.MQLD4wmUQSqGyMVENv8mrko-tvsqg3aLibdy5m-KWBp-HqP5liYB8GlWvSC4c1ZXka3YRRgA2dX40Q",
"google_profile_url": "https://plus.google.com/+NazirHussain91",
"google_avatar": "https://lh3.googleusercontent.com/-nLaXbUtBV0g/AAAAAAAAAAI/AAAAAAAAAAA/QaM3CzCJxlM/photo.jpg?sz=50",
"twitter_id": "832857385",
"twitter_token": "832857385-2PqAa48RffL2Yl7ZNJinBhIawMmH56amI54OBklR",
"twitter_profile_url": "",
"twitter_avatar": "http://pbs.twimg.com/profile_images/2626032008/hzd0ynsei7qjpyobaklw.jpeg",
"role": "author",
"activity_stream": [
{
"id": "14",
"user_id": "49",
"post": "Test Post",
"created_at": "2015-11-13 04:51:53",
"updated_at": "2015-11-13 04:51:53",
"deleted_at": null,
"activity_stream_comments": [
{
"id": "1",
"post_id": "14",
"user_id": "49",
"comment": "This is my first comment",
"created_at": "2015-11-19 10:49:13",
"updated_at": "2015-11-19 10:49:13",
"deleted_at": null
},
{
"id": "2",
"post_id": "14",
"user_id": "49",
"comment": "This is my second comment that I edited as well",
"created_at": "2015-11-19 11:03:01",
"updated_at": "2015-11-20 11:35:04",
"deleted_at": null
},
{
"id": "6",
"post_id": "14",
"user_id": "30",
"comment": "This is another test comment",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null
}
]
},
{
"id": "24",
"user_id": "49",
"post": "This is my first post",
"created_at": "2015-11-13 06:23:24",
"updated_at": "2015-11-13 06:23:24",
"deleted_at": null,
"activity_stream_comments": []
}
]
}
Now there are two questions : 1. How to use orderBy and 2. In the post and comment level how to get user name along with user id at the same time because querying again is not at all a good idea.

Resources