I have the following code which takes a collection of CallRecords and then applies groupBy them as well as a filter:
$Groups = $CallRecords->filter( function($CallRecord)
{
return isset($CallRecord->meta->reason_not_connected) && true;
})
->groupBy('meta.reason_not_connected');
return $Groups;
Which returns
"example": {
"Reached Voicemail - No Message": [
{
"id": "44",
"phone_number_id": "51",
},
{
"id": "55",
"phone_number_id": "31",
},
],
"Reached Voicemail - Left Message": [
{
"id": "19",
"phone_number_id": "11",
},
{
"id": "20",
"phone_number_id": "21",
},
]
}
How can I morph this collection to display counts like this:
"example": {
"Reached Voicemail - No Message": 2,
"Reached Voicemail - Left Message": 2
}
Related
I am working with Rest Api using Codeigniter, Iwant to return "two arrays" (FilterType='Tag' OR FilterType='Merchant'),I am getting successfully but i want to "FilterId" also (static or dynamic) I am
getting response in json and i want to append "FilterId" in array/json but not in loop , i want to put "FilterId"
below "data" (in json response),how can i do this ?
Here is my controller
$CategoryUrl = trim($this->input->post('CategoryUrl'));
$users = $this->mymodel->SearchRecords($CategoryUrl);
$responseJSON = array(data" => $users);
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
Here is my model code
function SearchTagRecords($CategoryUrl)
{
$this->db->select("fp.id,fp.CategoryUrl,fp.FilterType,fp.FilterName,fp.paramId,fp.ParamName,fp.CategoryName")
->from("filterproducts fp")
->join("filterid fi", "fp.FilterType=fi.name")
->where("fp.CategoryUrl", $CategoryUrl);
$query = $this->db->get();
$result = $query->result_array();
$tags=array();
$merchant=array();
foreach($result as $rec)
{
if($rec['FilterType']=="Tag")
{
$tags[]=$rec;
}else if($rec['FilterType']=="Merchant")
{
$merchant[]=$rec;
}
}
$final=[];
$final['tags'] = $tags;
$final['merchant'] = $merchant;
return $final;
}
Right now here is my json output
{
"Status": "1",
"data": {
"tags": [
{
"id": "23239",
"CategoryUrl": "restaurants",
"FilterType": "Tag",
"FilterName": "collection",
"paramId": "2",
"ParamName": "Buffet",
"CategoryName": "Restaurants & Food "
},
......And so on
"merchant": [
{
"id": "23250",
"CategoryUrl": "restaurants",
"FilterType": "Merchant",
"FilterName": "brand",
"paramId": "12372",
"ParamName": "Peddlers",
"CategoryName": "Restaurants & Food "
},
....And so on
]
}
}
I want json output like following one (Add "FilterId"(coming from db or we can put static also) in json )
How can i do this ?
{
"Status": "1",
"data": {
"FilterId": "1",
"tags": [
{
"id": "23239",
"CategoryUrl": "restaurants",
"FilterType": "Tag",
"FilterName": "collection",
"paramId": "2",
"ParamName": "Buffet",
"CategoryName": "Restaurants & Food "
},
......And so on
"FilterId": "2",
"merchant": [
{
"id": "23250",
"CategoryUrl": "restaurants",
"FilterType": "Merchant",
"FilterName": "brand",
"paramId": "12372",
"ParamName": "Peddlers",
"CategoryName": "Restaurants & Food "
},
....And so on
]
}
}
i'm new in laravel and php and now i wanna create a rest api
I have this query:
$questions = Question::with(
array('picture' => function($query){
$query>select('question_id','picture_name')>pluck('picture_name')>all();
},
'user'=>function($query){
$query->select('id','image','name');
}))->get()->toArray();
my query return this json:
{
"questions:": [
{
"id": 1,
"title": "23",
"caption": "last",
"address": "lsdbabfd",
"picture": [
{
"question_id": 1,
"picture_name": "1527484678.jpg"
},
{
"question_id": 1,
"picture_name": "1527485120.jpg"
}
],
"user": {
"id": 1,
"image": "defaultPicture",
"name": "alex"
}
}
]
}
but i want this json:
{
"questions:": [
{
"id": 1,
"title": "23",
"caption": "last",
"address": "lsdbabfd",
"picture": [
"1527484678.jpg",
"1527485120.jpg"
],
"user": {
"id": 1,
"image": "defaultPicture",
"name": "alex"
}
}
]
}
how can i do that?
i already tried to do that with Laravel pluck method and Eloquent Mutators
you can add attribute method
class Question extends Model
{
...
protected $appends = ['pictures'];
public function getPicturesAttribute()
{
return $this->pictures->pluck('picture_id');
}
}
i have mongodb with database name travel and collection name sample with data as below
`{
"_id": { id": "2344" },
"places": { "destination": [ "singapore", "delhi" ] },
"dt": { "date": "1434467400" }
},
{
"_id": { id": "2345" },
"places": { "destination": [ "singapore", "delhi" ] },
"dt": { "date": "1434467445" }
},
{
"_id": { "id": "2354" },
"places": { "code": "7856", "source": [ "beijing", "singapore" ], "destination": [ "newyork", "landon", "sidney" ] },
"dt": { "date": "1434589338" }
}`
i wrote monogdb aggregate function to find count of places
`db.sample.aggregate([{$group : {_id : "$places", count : {$sum : 1}}}, {$sort: {count: 1}}])`
which shows result as
{ "_id" : { "destination" : [ "singapore", "delhi" ] }, "count" : 2 } \
{ "_id" : { "code" : "7856", "source" : [ "beijing", "singapore" ], "destination
" : [ "newyork", "landon", "sidney" ] }, "count" : 1 }
which gives count for each places by grouping places.
i have below these 2 requirements
i want to write this query in php with mongodb connection
if possible i want to get count based on code or source or destination which are inside places.
thanx in advance for help..
I have a collection from Laravel Eloquent query. Here grandfathers have many children(fathers), fathers have many children(grandchildren).
The returned result is this:
[
{
"grand_father_id": "26",
"fathers": [
{
"father_id": "101",
"children": [
{
"child_id": "77",
"children_weight": [
{
"weight": "30.00",
}
]
},
{
"child_id": "84",
"children_weight": [
{
"weight": "20.00",
}
]
}
]
},
{
"father_id": "102",
"children": [
{
"child_id": "78",
"children_weight": [
{
"weight": "50.00",
}
]
}
]
}
]
},
{
"grand_father_id": "27",
"fathers": [
{
"father_id": "100",
"children": [
{
"child_id": "83",
"children_weight": [
{
"weight": "100.00",
}
]
}
]
}
]
},
{
"grand_father_id": "28",
"fathers": [
{
"father_id": "105",
"children": [
{
"child_id": "81",
"children_weight": [
{
"weight": "80.00",
}
]
},
{
"child_id": "82",
"children_weight": [
{
"weight": "0.00",
}
]
}
]
}
]
},
{
"grand_father_id": "29",
"fathers": [
{
"father_id": "108",
"children": [
{
"child_id": "79",
"children_weight": [
{
"weight": "44.00",
}
]
},
{
"child_id": "80",
"children_weight": [
{
"weight": "56.00",
}
]
}
]
}
]
},
{
"grand_father_id": "30",
"fathers": [
{
"father_id": "107",
"children": [
]
}
]
}
]
How do I count the total number of all grandchildren of all grandfathers. I can ofcourse use nested loop to count this. But is there any other Laravel Collection method to do this? By the way, the example I gave here with grandfather, father, grandchildren is just an example with pseudo names. The real fields are objectives, actions, success_indicators, success_indicator_weight. The query to obtain the collection is:
return Objective::with([
'actions' => function($query) {
$query->select(['action_id', 'action_description', 'objective_id_fk']);
}, 'actions.successIndicators' => function($query) {
$query->select('success_indicator_id', 'success_indicator_description', 'action_id_fk');
}, 'actions.successIndicators.SuccessIndicatorYearWeight' => function($query) {
$query->select('success_indicator_weight', 'success_indicator_unit', 'success_indicator_id_fk');
},
])->get()
There are a load of array_* helper methods for doing a variety of operations on nested arrays but I don't think there is anything that would serve your cause there. You can check for yourself - http://laravel.com/docs/5.0/helpers#arrays
I don't know myself if there is anything built into Collection but you could consider an accessor method of each Grandfather class:
class Grandfather extends Eloquent
{
...
public function getTotalGrandchildrenAttribute()
{
$total_grandchildren = 0;
// loop through each father
foreach ($this->fathers as $father) {
$total_grandchildren += count($father->children);
}
return $total_grandchildren;
}
}
Then in your script:
$grandfathers = Grandfathers::all();
$total_grandchildren = 0;
foreach ($grandfathers as $grandfather) {
$total_grandchildren += $grandfather->total_grandchildren;
}
echo "Total grandchildren: $total_grandchildren";
http://laravel.com/docs/5.0/eloquent#accessors-and-mutators
In addition, instead of using all() as shown above, you may want to use with('fathers') and with('children') so you're not hitting the DB every time you want to fetch fathers/ children:
$grandfathers = Grandfathers::with(array('fathers' => function($query) {
$query->with('children');
});
http://laravel.com/docs/5.0/eloquent#eager-loading
I am trying to iterate both index of JSON(Players and Buildings), so that i can a get new result in jQuery
I have two index of JSON one having information of Players and second index having information of Building related Player.
I want to Parse it so that i can get Player and its building name.
My Actual JSON result
{
"Players": [
{
"id": "35",
"building_id": "8",
"room_num": "101",
},
{
"id": "36",
"building_id": "9",
"room_num": "102",
},
{
"id": "37",
"building_id": "10",
"room_num": "103",
},
{
"id": "38",
"building_id": "11",
"room_num": "104",
}
],
"Buildings": [
{
"id": "8",
"name": "ABC"
},
{
"id": "9",
"name": "DEF"
},
{
"id": "10",
"name": "GHI"
},
{
"id": "11",
"name": "JKL"
}
]
}
Need this
"information": [
{
"player_id": "35",
"Buildings_name": "ABC"
},
{
"player_id": "36",
"Buildings_name": "DEF"
},
{
"player_id": "37",
"Buildings_name": "GHI"
},
{
"player_id": "38",
"Buildings_name": "JKL"
}
]
}
Here you go, this go for each player and check if there are buildings and will map them to new structure. This will not filter values for buildings that do not have mapping to players, and will not include the buildings with no players.
var x = {
"Players": [
{
"id": "35",
"building_id": "8",
"room_num": "101",
},
{
"id": "36",
"building_id": "9",
"room_num": "102",
},
{
"id": "37",
"building_id": "10",
"room_num": "103",
},
{
"id": "38",
"building_id": "11",
"room_num": "104",
}
],
"Buildings": [
{
"id": "8",
"name": "ABC"
},
{
"id": "9",
"name": "DEF"
},
{
"id": "10",
"name": "GHI"
},
{
"id": "11",
"name": "JKL"
}
]
};
var res = $.map(x.Players, function(item) {
return {
player_id: item.id,
building_name: $.grep(x.Buildings, function(i) {
return i.id == item.building_id
}).length != 0 ? $.grep(x.Buildings, function(i) {
return i.id == item.building_id
})[0].name : undefined
}
})
and if you want to filter values that do not have relationships e.g INNER join
var resInnerJoin = $.grep($.map(x.Players, function(item) {
return {
player_id: item.id,
building_name: $.grep(x.Buildings, function(i) {
return i.id == item.building_id
}).length != 0 ? $.grep(x.Buildings, function(i) {
return i.id == item.building_id
})[0].name : undefined
}
}), function(it) {
return it.building_name != undefined
})
If you need it in PHP :
$json = '{...}';
// create and PHP array with you json data.
$array = json_decode($json, true);
// make an array with buildings informations and with building id as key
$buildings = array();
foreach( $array['Buildings'] as $b ) $buildings[$b['id']] = $b;
$informations = array();
for ( $i = 0 ; $i < count($array['Players']) ; $i++ )
{
$informations[$i] = array(
'player_id' => $array['Players'][$i]['id'],
'Buildings_name' => $buildings[$array['Players'][$i]['building_id']]['name']
);
}
$informations = json_encode($informations);
var_dump($informations);