Laravel 框架中的拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47068362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string in Laravel Framework
提问by Aidel Gerrardo
How do I split string and display it in a table in Laravel Framework? I fetch data from my database that consists of one column but in a string such as { 1234, normal, r4r3r2 }
. I want to split it into three different parts/values by commas and display it in a table of three columns.
如何在 Laravel 框架中拆分字符串并将其显示在表格中?我从我的数据库中获取数据,该数据包含一列但在一个字符串中,例如{ 1234, normal, r4r3r2 }
. 我想用逗号将它分成三个不同的部分/值,并将其显示在一个三列的表格中。
For now, I only can display the data without splitting the them.
目前,我只能显示数据而不拆分它们。
My HomeController:
我的家庭控制器:
public function index()
{
$test = Test::all();
return view('home')->with('test', $test);
}
My home.blade.php:
我的 home.blade.php:
<ol>
@foreach($test as $row)
<li>{{ $row->data }}</li>
@endforeach
</ol>
回答by Rits
First explode the $test variable to get Array,
先爆破$test变量得到Array,
<ol>
@foreach(explode(',',$test) as $row)
<li>{{ $row }}</li>
@endforeach
</ol>
After explode using foreach single key we can access from the array. I hope it helps.
使用 foreach 单个键爆炸后,我们可以从数组中访问。我希望它有帮助。
回答by bipin patel
You can explode you string in blade file like this
你可以像这样在刀片文件中分解你的字符串
@foreach(explode(',', $row->data) as $fields)
<li>{{$fields}}</li>
@endforeach
And using of your model you can also done like this
使用你的模型你也可以这样做
class Test extends Model
{
public function getDataAttribute()
{
return explode(',', $this->data);
}
}
it's return your data explode with comma separated array.
它返回您的数据以逗号分隔的数组爆炸。