Reputation: 1485
I'm trying to return id's as a string and pass them via api (using for select later)
Using Laravel resouces:
public function toArray($request)
{
return [
'speciality' => $this->specialities()->pluck('speciality_id')
];
}
and it returns an array of numbers, like:
[1, 3, 5, 7]
How can I convert them within eloquent query and return like a string?
["1", "3", "5", "7"]
Upvotes: 4
Views: 2140
Reputation: 9853
You could loop
through the array, cast
it to string and add to new array
as it is only required for this specific
case.
$a = [1, 3, 5, 7];
$b = array();
foreach($a as $as)
$b[] = (string)$as;
return $b;
Or better use array_map()
-
$a = array_map(function($value){
return (string) $value;
}, $a);
Upvotes: 2
Reputation: 7489
It's a bit awful, but if have no choice then cast it to string
protected $casts=[
'speciality_id'=>'string'
];
Upvotes: 1