From 23dd7953970cc911932820a242b4b4ba6c4b7947 Mon Sep 17 00:00:00 2001 From: TimmensOne Date: Sat, 1 Apr 2023 20:34:39 +0200 Subject: [PATCH] simple api export --- ChatGPT/JSON Response with Device IDs | 55 +++++++++++++++++++++++++++ device-app/routes/api.php | 34 ++++++++++++----- 2 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 ChatGPT/JSON Response with Device IDs diff --git a/ChatGPT/JSON Response with Device IDs b/ChatGPT/JSON Response with Device IDs new file mode 100644 index 0000000..c3a11b4 --- /dev/null +++ b/ChatGPT/JSON Response with Device IDs @@ -0,0 +1,55 @@ +I have this methode: +Route::get('/export', function () { + return response()->json([ + 'Devices' => Device::all(), + 'purchasingInformations' => PurchasingInformation::all(), + 'OwnerTransactions' => OwnerTransaction::all(), + 'LocationTransactions' => LocationTransaction::all() + ]); +}); + +but i want to use the id of for example the devices as key for my json + +To use the id of the Device model as a key in your JSON response, you can modify your code to map each device to an array with the device id as the key. Here's an example: + +php + +Route::get('/export', function () { + $devices = Device::all()->mapWithKeys(function ($device) { + return [$device->id => $device]; + }); + + return response()->json([ + 'Devices' => $devices, + 'purchasingInformations' => PurchasingInformation::all(), + 'OwnerTransactions' => OwnerTransaction::all(), + 'LocationTransactions' => LocationTransaction::all() + ]); +}); + +In this modified code, we first retrieve all Device models using the all() method. We then call the mapWithKeys method on the resulting collection to transform it into an associative array where the device id is the key and the device model is the value. + +We then pass this modified $devices variable to the JSON response, so that the devices will be outputted with their ids as keys. The other collections are passed as is without modification. + +Note that if you have multiple models with the same id, the later models will overwrite the earlier ones in the JSON response. + +------------- + +can i also do it without an key, just the objects? + +Yes, you can also return an array of objects without using keys. Here's an example: + +scss + +Route::get('/export', function () { + return response()->json([ + 'Devices' => Device::all()->toArray(), + 'purchasingInformations' => PurchasingInformation::all()->toArray(), + 'OwnerTransactions' => OwnerTransaction::all()->toArray(), + 'LocationTransactions' => LocationTransaction::all()->toArray() + ]); +}); + +In this code, we're using the toArray() method to convert each collection of models to an array of objects. These arrays are then included in the JSON response with their corresponding names as keys. + +This will return an array of objects without keys. However, keep in mind that if you have multiple models with the same primary key value, their data will be merged into a single object in the JSON response, which may not be what you intended. diff --git a/device-app/routes/api.php b/device-app/routes/api.php index fa1c366..c6f4572 100644 --- a/device-app/routes/api.php +++ b/device-app/routes/api.php @@ -1,5 +1,9 @@ json([ - 'devices' => [ - [ - 'title' => 'Device One' - ], - [ - 'title' => 'Device Two' - ] - ] - ]); + 'Devices' => Device::all(), + 'PurchasingInformations' => PurchasingInformation::all(), + 'OwnerTransactions' => OwnerTransaction::all(), + 'LocationTransactions' => LocationTransaction::all() + ]); +}); + +Route::post('/import', function (Request $request) { + $data = $request->json()->all(); + $devices = $data['Devices']; + foreach ($devices as $device) { + Device::create([ + 'device_id' => $device->device_id, + ]); + } + $purchasingInformations = $data['PurchasingInformations']; + $ownerTransactions = $data['OwnerTransactions']; + $locationTransactions = $data['LocationTransactions']; }); Route::middleware('auth:sanctum')->get('/user', function (Request $request) {