Array To String Conversion Laravel
Hello Everyone, in this tutorial we will learn how can we do “Array to String Conversion Laravel”. So let’s get started.
In various situation we may need to convert Array to String in Laravel. When you have an Array of values but you want to store that array in database as a string. There are several methods. But easiest one is using the implode function. The implode() function return a string from a given array. Too much talking, lets jump into the coding part ( show me the code 😉 ).
implode(separator, array);
Code:[php]
Route::get('/arr-to-str', function() {
$myHeroesArray = array('Batman', 'Hulk', 'Thor');
return $myHeroesString = implode(', ', $myHeroesArray);
});
[/php]If you run this code, you will see an output like this
[php]
Batman, Hulk, Thor
[/php]
Let's say you have a Laravel Collections. You can also use that to...
[php]
Route::get('/arr-to-str', function () {
$collection = collect([
['name' => 'John'],
['name' => 'Jane'],
]);
return $collection->implode('name', ', ');
});
[/php]Result:
[php]
John, Jane
[/php]
These are some easiest or cheesiest way of Array To String Conversion Laravel. See you another day!