Yajra datatables filter on sub relation - php

I have a DataTable where i return my model. Inside this datatable i would like to get a column that shows a sub relationship and i can filter on that.
Example: Funds Have Member and Member has Cards
I add a column that i made myselh:
FundDataTable.php
->addColumn('cards.date', function(Fund $funds) {
if(count($funds->member->cards()->get())){
return $funds->member->cards()->first()->date;
}else{
return "None";
}
})
public function query(Fund $model)
{
return $model->newQuery()->with('member')->with('fr_status')->with('fr_payment_term')->with('member.cards');
}
protected function getColumns()
{
return [
Column::make('id'),
Column::make('member_id'),
Column::make('lastname'),
Column::make('begin_date'),
Column::make('fr_payment_term.payment_term'),
Column::make('destination'),
Column::make('cards.date'),
Column::make('fr_status.status'),
Column::computed('action')
->exportable(false)
->printable(false)
->addClass('text-center'),
];
}
index.blade.php
columns":[
{
"data": "id",
"name": "id",
"title": "Id",
"orderable": true,
"searchable": true
}, {
"data": "member_id",
"name": "member_id",
"title": "Member Id",
"orderable": true,
"searchable": true
}, {
"data": "lastname",
"name": "member.lastname",
"title": "Last name",
"orderable": true,
"searchable": true
}, {
"data": "begin_date",
"name": "begin_date",
"title": "Begin Date",
"orderable": true,
"searchable": true
}, {
"data": "fr_payment_term.payment_term",
"name": "payment_term",
"title": "Payment Term",
"orderable": true,
"searchable": true
}, {
"data": "destination",
"name": "member.destination",
"title": "Destination",
"orderable": true,
"searchable": true
},{
"data": "cards.date",
"name": "date",
"title": "Cards",
"orderable": true,
"searchable": true
}, {
"data": "fr_status.status",
"name": "status",
"title": "Status",
"orderable": true,
"searchable": true
}, {
"data": "action",
"name": "action",
"title": "Action",
"orderable": false,
"searchable": false,
"className": "text-center"
}
FundController.php
public function index(FundDataTable $dataTable)
{
session(['title' => 'Overview Funds']);
$statusses = ModelOption::where(['model' => 'Fund', 'name' => 'status'])->get();
$paymentTerms = ModelOption::where('model', 'PaymentTerm')->where( 'name','<>', 'status')->get();
$packages = Package::all();
return $dataTable->render('funds.index',[ 'statusses' => $statusses, 'packages' => $packages, 'paymentTerms' => $paymentTerms]);
}
Problem is when I filter on the searchbox for None. it does not work and shows everything.

Related

Compare 2 Multidimensional Arrays in PHP and show all the difference in an array

I have 2 Multiple-Arrays and I want to compare them. In the second Array I want then show with colors which value has changed, which key is new and which key is removed.
I have found a lot of different solutions, but no one function for me.
Here are my arrays:
{
"originFormation": {
"stationName": "Bern",
"sections": [{
"name": "B",
"cars": [{
"type": "LOK",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": false,
"nextPassage": false
}]
}, {
"name": "C",
"cars": [{
"type": "CAR",
"number": "1",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": ["AbteilRollstuhl", "AbteilBusiness"],
"closed": false,
"previousPassage": false,
"nextPassage": true
}, {
"type": "CAR",
"number": "1",
"class": "2",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": true,
"nextPassage": true
}]
}],
"legendItems": [{
"id": "1",
"text": "1st class coach",
"refersTo": "type"
}, {
"id": "2",
"text": "2st class coach",
"refersTo": "type"
}, {
"id": "Fahrtrichtung",
"text": "Direction of travel",
"refersTo": "code"
}, {
"id": "FA",
"text": "Family Coach with play area",
"refersTo": "type"
}, {
"id": "AbteilRollstuhl",
"text": "Wheelchair space",
"refersTo": "code"
}, {
"id": "AbteilVeloPl",
"text": "Bicycle loading: Without reservation",
"refersTo": "code"
}, {
"id": "LK",
"text": "Locomotive",
"refersTo": "type"
}, {
"id": "WR",
"text": "Restaurant 1st and 2nd class",
"refersTo": "type"
}, {
"id": "AbteilBusiness",
"text": "Business zone in 1st class: Reservation possible",
"refersTo": "code"
},
{
"id": "HFS_WG_NUM",
"text": "Coach",
"refersTo": "code"
}, {
"id": "KD",
"text": "No passage",
"refersTo": "type"
}
],
"info": {
"title": "Changes to the composition of the train",
"formationChanges": []
}
}
}
Array2:
{
"originFormation": {
"stationName": "Bern",
"sections": [
{
"name": "B",
"cars": [
{
"type": "LOK",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": false,
"nextPassage": false
}
]
},
{
"name": "C",
"cars": [
{
"type": "CAR",
"number": "12",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": [
"AbteilRollstuhl",
"AbteilBusiness"
],
"closed": false,
"previousPassage": false,
"nextPassage": true
},
{
"type": "CAR",
"number": "1",
"class": "2",
"occupancy": "UNKNOWN",
"attributes": [
"AbteilStefan"
],
"closed": false,
"previousPassage": true,
"nextPassage": true
},
{
"type": "CAR",
"number": "2",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": true,
"nextPassage": true
}
]
}
],
"legendItems": [
{
"id": "1",
"text": "1st class coach",
"refersTo": "type"
},
{
"id": "2",
"text": "2st class coach",
"refersTo": "type"
},
{
"id": "Fahrtrichtung",
"text": "Direction of travel",
"refersTo": "code"
},
{
"id": "FA",
"text": "Family Coach with play area",
"refersTo": "type"
},
{
"id": "AbteilRollstuhl",
"text": "Wheelchair space",
"refersTo": "code"
},
{
"id": "AbteilVeloPl",
"text": "Bicycle loading: Without reservation",
"refersTo": "code"
},
{
"id": "LK",
"text": "Locomotive",
"refersTo": "type"
},
{
"id": "WR",
"text": "Restaurant 1st and 2nd class",
"refersTo": "type"
},
{
"id": "AbteilBusiness",
"text": "Business zone in 1st class: Reservation possible",
"refersTo": "code"
},
{
"id": "AbteilStefan",
"text": "Ich bin ein neues Abteil",
"refersTo": "code"
},
{
"id": "HFS_WG_NUM",
"text": "Coach",
"refersTo": "code"
},
{
"id": "KD",
"text": "No passage",
"refersTo": "type"
}
],
"info": {
"title": "Changes to the composition of the train",
"formationChanges": [
"All Changed"
]
}
}
}
My fonction to compare:
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = "<b>".$value."</b>";
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = "<b>".$value."</b>";
}
}
return !isset($difference) ? 0 : $difference;
}
var_dump(array_diff_assoc_recursive($array2, $array1));
This difference i get:
[originformation][sections][1][cars][0][number] -> newValue 12
[originformation][sections][1][cars][1][attributes] -> newValue AbteilStefan
[originformation][sections][1][cars][2] -> all Attributes are new
[originformation][legendItems][9][id] -> newValue AbteilStefan
[originformation][legendItems][9][text] -> newValue Ich bin ein neues Abteil
[originformation][legendItems][10] -> all Attributes are new
[originformation][legendItems][11] -> all Attributes are new
[originformation][info][formationChanges][0] => newValue All Changed
The difference i expected
[originformation][sections][1][cars][0][number] -> newValue 12
[originformation][sections][1][cars][1] -> all Attributes are new
[originformation][legendItems][9] -> all Attributes are new
[originformation][info][formationChanges][0] => newValue All Changed
Problem
My function doesn't see, that car in Array2 I put in a complete new car and also a complete new legendItem and the rest is still the same.
Does someone know how I can compare the arrays, that he doesn't check the order of the attributes on the same level?

Can I update items in array with $function operator and index number?

I have an object like this, and I'm working with "todos" array, only want update active status with specific indexs are given:
{
"_id": "61e7e78372d3221d2c5fb242",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"todos": [
{
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 0
{
"name": "sub2",
"desc": "desc2",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 1
...
]
}
The first I try update with: ['$set' => ['todos.0.active' => false]]
{
"0": {
"active": false
},
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
},
active can be update to false, key 0 is generated. I continue try other:
array_push($pipeline, ['$set' => ['todos' => [
'$function' => [
'body' => 'function(todos) {
return todos; // or do something
}',
'args' => ['todos'],
'lang' => 'js',
]
]]]);
But todos field turn into string is "todos", not array. What wrong with $set and $function? And other operators can do the same easier like $map or $filter?
My full pipeline update:
[
{
"$set": {
"name": "First Task 1",
"desc": "This is description",
"status": "completed",
"done": 100,
"level": "medium",
"company": "BBB",
"project": "AAA",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"updated_at": "$$NOW"
}
},
{
"$set": {
"todos.1.active": false
}
},
{
"$set": {
"todos": {
"$concatArrays": [
"$todos",
[
{
"name": "sub3",
"desc": "desc3",
"active": true,
"owner": "61e6125db0f102060951aa53"
}
]
]
}
}
}
]
db.collection.updateOne(['_id' => ObjectId('')], $pipeline, ['multi' => true]})
I want to update with pipeline, not each single query.
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.$[elem].active": false
}
},
{
"multi": true,
"arrayFilters": [
{
"elem.name": {
$eq: "sub1"
}
}
]
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.0.active": false,
"todos.2.active": false
}
},
{
"multi": true
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
[
{
$set: {
todos: {
$map: {
input: {
$range: [ 0, { $size: "$todos" } ]
},
as: "i",
in: {
$let: {
vars: {
"item": { $arrayElemAt: [ "$todos", "$$i" ] }
},
in: {
$cond: {
if: {
$in: [ "$$i", [ 0, 2 ] ]
},
then: {
"name": "$$item.name",
"desc": "$$item.desc",
"active": false,
"owner": "$$item.owner"
},
else: "$$item"
}
}
}
}
}
}
}
}
],
{
"multi": true
})
mongoplayground

php json array of arrays and get some key values from deep nested [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I have a JSON data get from an API and I want to get/access for each AttributeValues the code and values to store into the tables, code as tables columns and values are dynamic for each record. I don't need to store the Attachements and Total from the json values.
i want to store to tables as an insert into (ID, TrackingNumber, CustomerFull) values (json values). any codeigniter, or php soluton or hint will be much appricate.
{
"Values": [{
"AttributeValues": [{
"ID": 0,
"Name": "ID",
"Values": [
"720"
],
"DataType": "Number",
"Code": "ID",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Tracking Number",
"Values": [
"xxxx/1094/180730/1"
],
"DataType": "String",
"Code": "TrackingNumber",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Customer Full",
"Values": [
"Ali Abdi"
],
"DataType": "String",
"Code": "CustomerFull",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
}
],
"Attachements": null
},
{
"AttributeValues": [{
"ID": 0,
"Name": "ID",
"Values": [
"757"
],
"DataType": "Number",
"Code": "ID",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Tracking Number",
"Values": [
"xxx/1094/180731/1"
],
"DataType": "String",
"Code": "TrackingNumber",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Customer Full",
"Values": [
"Aberash Haile"
],
"DataType": "String",
"Code": "CustomerFull",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Service Code",
"Values": [
"SO-1096"
],
"DataType": "String",
"Code": "ServiceCode",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Request Date",
"Values": [
"7/31/2018 11:04:06 AM"
],
"DataType": "Datetime",
"Code": "RequestDate",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
}
],
"Attachements": null
}
],
"Total": 335
}
First of all, you should convert your JSON to array then use loop(foreach, for, while...) and get value accordingly like this:
<?php
$json = '{
"Values":[
{
"AttributeValues":[
{
"ID":0,
"Name":"ID",
"Values":[
"720"
],
"DataType":"Number",
"Code":"ID",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Tracking Number",
"Values":[
"xxxx/1094/180730/1"
],
"DataType":"String",
"Code":"TrackingNumber",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Customer Full",
"Values":[
"Ali Abdi"
],
"DataType":"String",
"Code":"CustomerFull",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
}
],
"Attachements":null
},
{
"AttributeValues":[
{
"ID":0,
"Name":"ID",
"Values":[
"757"
],
"DataType":"Number",
"Code":"ID",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Tracking Number",
"Values":[
"xxx/1094/180731/1"
],
"DataType":"String",
"Code":"TrackingNumber",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Customer Full",
"Values":[
"Aberash Haile"
],
"DataType":"String",
"Code":"CustomerFull",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Service Code",
"Values":[
"SO-1096"
],
"DataType":"String",
"Code":"ServiceCode",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Request Date",
"Values":[
"7/31/2018 11:04:06 AM"
],
"DataType":"Datetime",
"Code":"RequestDate",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
}
],
"Attachements":null
}
],
"Total":335
}';
$json_data = json_decode($json, true);
//echo "<pre>"; print_r($json_data['Values']); exit;
if(isset($json_data['Values'])){
foreach($json_data as $data){
foreach($data as $attr){
if(isset($attr['AttributeValues'])){
foreach($attr['AttributeValues'] as $value){
if(isset($value['Values'])){
foreach($value['Values'] as $val){
$attribute_values = $val;
echo "<pre>"; print_r($attribute_values); echo "</pre>";
}
}
}
}
}
}
}
?>

How to JSON with duplicate 'id' values?

I am receiving data from another site with json API.
how can I get the data of the objects with the same id number.
I try as follows but it turns blank..
there are 10 objects with the same id number
how can i get them.
The typeid number is the same but the specialvalue value is different.
{"id":4620041,"active":true,"typeid":56,"specialvalue":"0.5","fields":{"magic":{"active":true,"type":"magic","value":"1.05"},"xmagic":{"active":true,"type":"xmagic","value":"7.5"}}}
{"id":4620045,"active":true,"typeid":56,"specialvalue":"1.5","fields":{"magic":{"active":true,"type":"magic","value":"3.05"},"xmagic":{"active":true,"type":"xmagic","value":"3.5"}}}
if($test->typeid =="56")
{
if (specialvalue == "0.5") {
$alta = $test->fields->magic->value;
$uste = $test->fields->xmagic->value;
}
}
echo $alta - $uste ;
Thank you in advance for your help
https://i.hizliresim.com/Pl0LL7.jpg
Few observations :
Provided JSON is not a valid JSON.
It should be an array of objects to filter the specific data as per the requirement.
Solution :
let jsonObj = [{
"id": 5246752,
"active": true,
"typeid": 56,
"specialvalue": "0.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "1.06"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "7.0"
}
}
}, {
"id": 5246753,
"active": true,
"typeid": 56,
"specialvalue": "1.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "1.35"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "3.0"
}
}
}, {
"id": 5246754,
"active": true,
"typeid": 56,
"specialvalue": "2.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "2.0"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.7"
}
}
}, {
"id": 5246755,
"active": true,
"typeid": 56,
"specialvalue": "3.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "3.45"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.25"
}
}
}, {
"id": 5246756,
"active": true,
"typeid": 56,
"specialvalue": "4.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "6.25"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.08"
}
}
}, {
"id": 5246757,
"active": true,
"typeid": 56,
"specialvalue": "5.5",
"fields": {
"magic": {
"active": true,
"type": "magic",
"value": "9.5"
},
"xmagic": {
"active": true,
"type": "xmagic",
"value": "1.02"
}
}
}];
let filteredData = jsonObj.filter(item => item.typeid===56 && item.specialvalue==="0.5");
console.log(filteredData);
Use .filter
var filteredData = data.filter(
(el) => {
return el.typeid===56 && el.specialvalue==="0.5";
}
);
This will return only those elements with el.typeid===56 && specialvalue==="0.5"

How can i get json object results in php using php_decode and url

below is my json object result of google api now i want to get url of cover from it which is parameter 'thumbnail' in below json object,but i dont know how can i retrive it can anyone here help me out?
{
"kind": "books#volumes",
"totalItems": 1,
"items": [
{
"kind": "books#volume",
"id": "9VKCkifBlisC",
"etag": "hhhZjjV6arI",
"selfLink": "url",
"volumeInfo": {
"title": "Instructor's manual to accompany An introduction",
"authors": [
"Jean-Paul Tremblay",
"P. G. Sorenson"
],
"publishedDate": "1976",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0070651515"
},
{
"type": "ISBN_13",
"identifier": "9780070651517"
}
],
"pageCount": 278,
"printType": "BOOK",
"categories": [
"Computers"
],
"averageRating": 5.0,
"ratingsCount": 1,
"contentVersion": "0.0.1.0.preview.0",
"imageLinks": {
"smallThumbnail": "url",
/*this is what i need*/
"thumbnail": "url"
},
"language": "en",
"previewLink": "url",
"infoLink": "url",
"canonicalVolumeLink": "url"
},
"saleInfo": {
"country": "IN",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "IN",
"viewability": "NO_PAGES",
"embeddable": false,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "url",
"accessViewStatus": "NONE",
"quoteSharingAllowed": false
}
}
]
}
You could do like this..
$arr = json_decode($json,true);
echo $arr['items'][0]['volumeInfo']['imageLinks']['thumbnail'];
Demo

Categories