simple api export
This commit is contained in:
parent
f43db84171
commit
23dd795397
55
ChatGPT/JSON Response with Device IDs
Normal file
55
ChatGPT/JSON Response with Device IDs
Normal file
@ -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.
|
@ -1,5 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Device;
|
||||||
|
use App\Models\LocationTransaction;
|
||||||
|
use App\Models\OwnerTransaction;
|
||||||
|
use App\Models\PurchasingInformation;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
@ -14,17 +18,27 @@
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('deviceList', function() {
|
Route::get('/export', function () {
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'devices' => [
|
'Devices' => Device::all(),
|
||||||
[
|
'PurchasingInformations' => PurchasingInformation::all(),
|
||||||
'title' => 'Device One'
|
'OwnerTransactions' => OwnerTransaction::all(),
|
||||||
],
|
'LocationTransactions' => LocationTransaction::all()
|
||||||
[
|
]);
|
||||||
'title' => 'Device Two'
|
});
|
||||||
]
|
|
||||||
]
|
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) {
|
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
||||||
|
Loading…
Reference in New Issue
Block a user