I have a database table called "Recipes" and a column in that table called "ingredients". The ingredients come from an HTML input in the browser. I ask the user to separate each ingredient by hitting enter and jumping down to the next row. The problem I am having is that when I return this data on a show.blade.php page, it is returning as one long string. I have not been able to figure out how to return it as a list.
Example:
I get "oats milk bananas cinnamon maple syrup" in the part of my website where I am calling the ingredients data.
I want to get
I am using Laravel 5.7 to build the application and I am calling {{$recipe->ingredients}}. Been messing a little bit with the PHP explode() function but with no luck. I am not very experienced so I'm sure this is not a difficult problem to solve, I just can't seem to find anything online. Perhaps I am not framing the question right?
The code to my create.blade.php file is:
<div class="createRecipeBody">
<fieldset>
<small class="errorMessage">{{ $errors->first('title') }}</small>
<label for="title">Give your creation a title</label>
<input type="text" name="title" id="title">
</fieldset>
<fieldset>
<small class="errorMessage">{{ $errors->first('description') }}</small>
<label for="description">Describe your creation</label>
<textarea name="description" id="description"></textarea>
</fieldset>
<fieldset>
<small class="errorMessage">{{ $errors->first('ingredients') }}</small>
<label for="ingredientList">List the ingredients</label>
<textarea name="ingredients" id="ingredientList" placeholder="Please list each ingredient on a seperate line"></textarea>
</fieldset>
<fieldset>
<small class="errorMessage">{{ $errors->first('directions') }}</small>
<label for="ingredientList">Directions</label>
<textarea name="directions" id="directions" placeholder="Please list each step on a seperate line"></textarea>
</fieldset>
<div class="terms">
<label class="authenticationCheck">
<input class="termsOfService" type="checkbox">
<span>
Public Recipe
</span>
</label>
<label class="authenticationCheck">
<input class="termsOfService" type="checkbox">
<span>Private Recipe</span>
</label>
</div>
<button class="submitRecipe" type="submit">Share your creation</button>
</div>
</section>
And this is to my show.blade.php:
<h2>Ingredients</h2>
<span class="ingredients">
<span class="ingredientItem firstItem">
<span><i class="far fa-clock"></i> {{$recipe->prepTime}}</span>
</span>
<span class="ingredientItem">
<span><i class="fas fa-concierge-bell"></i> {{$recipe->servings}}</span>
</span>
<span class="ingredientItem">
<span><i class="fas fa-weight"></i> {{$recipe->calories}}</span>
</span>
</span>
<hr>
<p>Here's what you will need...</p>
<div>
<ol>
<li> {{$recipe->ingredients}}</li>
</ol>
</div>
<br>
<h2>Directions</h2>
<hr>
<p>{{$recipe->directions}}</p>
<h2>Chef's Tip</h2>
<hr>
<p>This feature is not yet enabled</p>
</div><!-- col-sm-6 -->
$string = 'apple banana cinemon pokemon';
$arr = explode(' ', $string);
array_walk($arr, function (&$item, $key) {
$item = $key+1 . '.) ' . $item;
});
$output = implode("
", $arr);
Something like this? First I explode the string to single words. Then I change the items to the format you asked for. And then I'm concatenating the elements to a string.
Edit: List is now starting with 1