Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string in Laravel Framework

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.

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:

<ol>
   @foreach($test as $row)
     <li>{{ $row->data }}</li>
   @endforeach
</ol>
like image 859
Aidel Gerrardo Avatar asked Nov 02 '17 05:11

Aidel Gerrardo


1 Answers

First explode the $test variable to get 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.

like image 188
Ritesh Khatri Avatar answered Sep 21 '22 09:09

Ritesh Khatri