在html页面上将逗号分隔列表显示为编号列表

时间:2018-05-01 17:26:11

标签: arrays laravel html-lists

我正在Laravel中创建一个foodlog应用程序,有关如何制作食谱的说明存储在数据库中以逗号分隔的列表中,如下所示:

cook the chicken, add peppers and onions, add sauce, serve with rice

我想在我的网站上将其显示为编号列表,如:

1. cook the chicken
2. add peppers and onions
3. add sauce
4. serve with rice

我目前的字符串出现在'< p>'标签,但希望它作为编号列表。有什么想法吗?

info.blade.php

<h1>{{ $recipes->recipe_name }}<h1>
<h2>Ingredients:</h2> <p>{{ $recipes->ingredients}}</p><br>
<h2>How to:</h2> <p>{{ $recipes->description }}</p><br>

RecipesController.php

public function showinfo($id) {     
    $recipes = Recipes::find($id);
    return view('recipes.info', compact('recipes'));
}

看起来像这样:
How it is currently being displayed

1 个答案:

答案 0 :(得分:0)

希望这有帮助。

<强> info.blade.php

$descriptionList = new \Illuminate\Support\Collection(explode(",", $description));

echo "<ol>";
$descriptionList->each(function($description){
     echo "<li>" . $description . "</li>";
});
echo "</ol>";

$description = "cook the chicken, add peppers and onions, add sauce, serve with rice";

$descriptionList = explode(",", $description);
<ol>  
    @foreach ($descriptionList as $descriptionItem)
       <li>{{ $descriptionItem }}</li>
    @endforeach    
</ol>

您可以将其转换为使用Laravel刀片模板。

相关问题