Summary of problem or feature request
I need to use hydrate to fill a model that is not linked to a database. But in doing so I can't use the datatable() call for laravel.
$query = "EXECUTE [dbo].[rpt_controlclient] '$XML' ";
DB::connection('sqlsrvreplicate')->reconnect();
$data = DB::connection('sqlsrvreplicate')->select($query);
$ReportData = $Model->hydrate($data);
return datatables()->collection($ReportData)->toJson();
What I need to get
"data": [
[
"Tiger Nixon",
"System Architect"
],
[
"Garrett Winters",
"Accountant"
],
What I actually get
"data": [{
"Name":"Tiger Nixon",
"Job":"System Architect"
},
{
"Name":"Garrett Winters",
"Job":"Accountant"
}],
The thing is the way you are returning the value. Check the last method of this line:
return datatables()->collection($ReportData)->toJson();
With toJson()
you are returning the data as an json
object, that is why you get:
{
"data": [
{
"Name": "Tiger Nixon",
"Job": "System Architect"
},
{
"Name": "Garrett Winters",
"Job": "Accountant"
}
]
}
Instead, you could do this:
return datatables()->collection($ReportData)->toArray();
Now, if you are using this for an API, you could parse the json
as an array
in the client side. If your client is written in php, just do:
$my_fancy_array = json_decode($received_data, true);