John Davidson

php - Finding differences in json in Laravel

0 comments
Message:


I am beginner in Laravel.
I make my application in Laravel 8,


I have this value in DB:


$json = {
"old": {
"id": 1,
"hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681",
"car_id": 2,
"comments": "uwagi",
"driver_id": 1,
"address_to": "adres dostawy",
"carrier_id": 22,
"company_id": 1,
"created_at": "2022-04-14T09:07:41.000000Z",
"data_start": "2022-04-01",
"deleted_at": null,
"updated_at": "2022-04-14T09:07:41.000000Z",
"carrier_nip": "728-597-9477",
"data_finish": "2022-04-04",
"address_from": "adres wyjazdu",
"carrier_city": "Lake Emersonville",
"carrier_name": "Satterfield, Lebsack and O'Conner",
"order_number": "Numer zlecenia",
"speed_number": "Numer zlecenia speed - spedycja",
"carrier_email": "[email protected]",
"carrier_phone": "347.731.2768 x1508",
"dispatcher_id": 3,
"carrier_street": "6342 Pollich Crossroad",
"selected_files": "[7,8,9]",
"transport_type": 2,
"deliver_comments": null,
"transport_status": 3,
"is_neutral_option": 2,
"selected_products": "[1,3]",
"transport_content": "kontakt",
"delivery_signature": null,
"selected_documents": "[5]",
"carrier_postal_code": "36804-7962",
"hidden_carrier_data": 0,
"delivery_name_surname": null,
"delivery_signature_binary": null,
"disable_change_status_driver": 0
},
"attributes": {
"id": 1,
"hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681",
"car_id": 2,
"comments": "uwagi",
"driver_id": 1,
"address_to":
"adres dostawy",
"carrier_id": 22,
"company_id": 1,
"created_at": "2022-04-14T09:07:41.000000Z",
"data_start": "2022-04-01",
"deleted_at": null,
"updated_at": "2022-04-14T09:09:08.000000Z",
"carrier_nip": "728-597-9477",
"data_finish": "2022-04-04",
"address_from": "adres wyjazdu",
"carrier_city": "Lake Emersonville",
"carrier_name": "Satterfield, Lebsack and O'Conner",
"order_number": "Numer zlecenia",
"speed_number": "Numer zlecenia speed - spedycja",
"carrier_email": "[email protected]",
"carrier_phone": "347.731.2768 x1508",
"dispatcher_id": 4,
"carrier_street": "6342 Pollich Crossroad",
"selected_files": "[7,8,9]",
"transport_type": 2,
"deliver_comments": null,
"transport_status": 3,
"is_neutral_option": 2,
"selected_products": "[1,3,2]",
"transport_content": "kontakt",
"delivery_signature": null,
"selected_documents": "[5,6,4]",
"carrier_postal_code": "36804-7962",
"hidden_carrier_data": 0,
"delivery_name_surname": null,
"delivery_signature_binary": null,
"disable_change_status_driver": 0
}
}

I need show differences from OLD and Attributes.
How can I make it?


Please help me.



My Answer:
You can use the array_diff_assoc function in PHP to compare the differences between the "old" and "attributes" arrays in your JSON data. Here's an example of how you can do this in Laravel:

php
$json = '{
"old": {
"id": 1,
"hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681",
"car_id": 2,
"comments": "uwagi",
"driver_id": 1,
"address_to": "adres dostawy",
"carrier_id": 22,
"company_id": 1,
"created_at": "2022-04-14T09:07:41.000000Z",
"data_start": "2022-04-01",
"deleted_at": null,
"updated_at": "2022-04-14T09:07:41.000000Z",
"carrier_nip": "728-597-9477",
"data_finish": "2022-04-04",
"address_from": "adres wyjazdu",
"carrier_city": "Lake Emersonville",
"carrier_name": "Satterfield, Lebsack and O'Conner",
"order_number": "Numer zlecenia",
"speed_number": "Numer zlecenia speed - spedycja",
"carrier_email": "[email protected]",
"carrier_phone": "347.731.2768 x1508",
"dispatcher_id": 3,
"carrier_street": "6342 Pollich Crossroad",
"selected_files": "[7,8,9]",
"transport_type": 2,
"deliver_comments": null,
"transport_status": 3,
"is_neutral_option": 2,
"selected_products": "[1,3]",
"transport_content": "kontakt",
"delivery_signature": null,
"selected_documents": "[5]",
"carrier_postal_code": "36804-7962",
"hidden_carrier_data": 0,
"delivery_name_surname": null,
"delivery_signature_binary": null,
"disable_change_status_driver": 0
},
"attributes": {
"id": 1,
"hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681",
"car_id": 2,
"comments": "uwagi",
"driver_id": 1,
"address_to": "adres dostawy",
"carrier_id": 22,
"company_id": 1,
"created_at": "2022-04-14T09:07:41.000000Z",
"data_start": "2022-04-01",
"deleted_at": null,
"updated_at": "2022-04-14T09:09:08.000000Z",
"carrier_nip": "728-597-9477",
"data_finish": "2022-04-04",
"address_from": "adres wyjazdu",
"carrier_city": "Lake Emersonville",
"carrier_name": "Satterfield, Lebsack and O'Conner",
"order_number": "Numer zlecenia",
"speed_number": "Numer zlecenia speed - spedycja",
"carrier_email": "[email protected]",
"carrier_phone": "347.731.2768 x1508",
"dispatcher_id": 4,
"carrier_street": "6342 Pollich Crossroad",
"selected_files": "[7,8,9]",
"transport_type": 2,
"deliver_comments": null,
"transport_status": 3,
"is_neutral_option": 2,
"selected_products": "[1,3,2]",
"transport_content": "kontakt",
"delivery_signature": null,
"selected_documents": "[5,6,4]",
"carrier_postal_code": "36804-7962",
"hidden_carrier_data": 0,
"delivery_name_surname": null,
"delivery_signature_binary": null,
"disable_change_status_driver": 0
}
}';

$data = json_decode($json, true);

$differences = array_diff_assoc($data['old'], $data['attributes']);

dd($differences);


This code will output an array containing the differences between the "old" and "attributes" arrays in your JSON data. You can then use this information to display or handle the differences as needed in your Laravel application.

Rate this post

5 of 5 based on 2817 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us