TrimStrings is only applied to the Request class. If you are making a request of another sites API then the http kernel is not used
TrimStrings via API?
I have a basic APi setup to return some data from a Microsoft SQL Server using a resource collection JsonResource, but I am finding that the strings returned have not been trimmed, is this correct?
The TrimStrings class is definitely in Kernel.php
Hi Snapey, I am talking about my own API calls, I can see the data has not had the strings trimmed?
How are you sending data to your api? It would be best to show it rather than explain it.
In my api.php file;
Route::get('/order/work/{ordnumbe}', function ($ordnumbe) {
return new SalesWorkResource( SalesWork::with('lines')->where('ORDNUMBE',$ordnumbe)->first() );
});
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class SalesWorkCollection extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
//return parent::toArray($request);
return [
'ordnumbe' => $this->ORDNUMBE
];
}
}
response is...
{"data":{"ordnumbe":"ORD2436075 "}}
You need to do your trimming in your response
return [
'ordnumbe' => trim($this->ORDNUMBE)
];
Again, trimstrings works only for the data you send to the api, not what you receive back.
For instance, an ajax post request sending {name: "value "} as a key/value pair would trim the value to be just "value" for the name key.
Or a form input.
<input name="name" type="text">
trimstrings will trim the value of whatever was submitted for name
trimstrings has nothing at all to do with the data you send back in the response. Only what is received from the request. You're not actually sending any data in your request at all. It's a simple get request.
Ahhh OK, that makes sense.
Is there any way I can trim all the data that comes back from the database in my responses like TrimStrings does for request without needing to manually trim each field?
Yes, but why? Wouldn't it be much better to clean up your data in the database and remove all of the unnecessary spaces wasting space in the db if they aren't wanted/needed?
I'd write a script to remove the spaces in the database that is currently there. Then, I'd also be stripping it off of any new data going into the database. Cleaning it up on output, each and every time, is not efficient at all and puts unnecessary strain on the app. Think of how much better it would be to strip it out when it gets saved and not having to worry about it anymore.
Unfortunately cleaning up the data isn't an option. The data is stored in a MS database using char data type and is entered by an ERP system.
I understand its not efficient but have no other option. Can you point me in the right direction of how I would do this on the output, would I have to use array_walk and trim?
Use a response class and process it there?
@SNAPEY - Hi Snapey, apologies but what do you mean by a response class?
https://laravel.com/docs/5.7/eloquent-resources
Sorry, should have said resource class
Using the example there, we could modify this to trim name and email;
public function toArray($request)
{
return [
'id' => $this->id,
'name' => trim($this->name),
'email' => trim($this->email),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
@SNAPEY - Ahh yes, is there an easier way to trim all fields without having to list them all individually though?
Please or to participate in this conversation.