I am making a voicelog conversation controller for some requests made for the front end to display i am allowing some data and some data needs to be excluded
public function index(): JsonResponse
{
$allowedMetadata = [
'queue_display_name',
'campaign_display_name',
'finished',
'transfer_destination',
'direction',
'record_expires',
'cur_call_uid',
'call_recording', //keep unexposed
'has_recording // if call recording is true make this true, otherwise false
];
request()->validate([
'from' => 'date_format:Y-m-d\TH:i:s\Z',
'until' => 'date_format:Y-m-d\TH:i:s\Z',
]);
$orderBy = request()->has('order_by') ? request('order_by') : "answered_at";
$direction = request()->has('order_by') && request('direction') === "asc" ? "asc" : "desc";
//some request ordering code...
//some request query parsing code...
$conversations = $query->paginate();
foreach ($conversations as &$conversation) {
foreach ($conversation->metadata as $key => $value) {
if (!in_array($key, $allowedMetadata)) {
$conversation->metadata->forget($key);
}
}
}
return response()->json($conversations);
}
in this case 'call_recording' is a url string which i want to keep unexposed. i want to make that value a boolean called 'has_recording' so that i can display a button in the front end if it is true, and not display if has_recording is false
how do i add an extra value to the api that has this booolean 'has_recording', while unexposing the call_recording url string?
i wanna iterate through all conversations and add a default value 'has_recording' like this in the store() function, but i want to add this value to the metadata
$reqHasCallRecording = false;
foreach ($request->metadata as $metadataData) {
if ($metadataData['key'] == 'call_recording') {
$reqHasCallRecording = true;
break;
}
}
i found out you can to this with Eloquent but not sure how to go about it? Any help is appreciated.
i solved my problem by iterating through every conversation and setting a new metadata property ' has recording' to true whenever there is a cal recording available
foreach ($conversations as &$conversation) {
foreach ($conversation->metadata as $key => $value) {
if ($key == 'call_recording') {
$conversation->metadata['has_recording'] = isset($conversation->metadata['call_recording']);
}
if (!in_array($key, $allowedMetadata)) {
$conversation->metadata->forget($key);
}
}
}