Explode string in laravel blade template
Just simply explode, however this logic should come from your controller/model
@if ($data->facings != "")
@foreach(explode(',', $data->facings) as $info)
<option>{{$info}}</option>
@endforeach
@endif
If $data
is some sort of model, I would suggest adding an accessor to your model
class MyModel extends Model
{
public function getFacingsAttribute()
{
return explode(',', $this->facings);
}
}
Then you can simply treat it as an array, as per your original example.
@foreach($data->facings as $info)
Use explode like this:
$new_array = array();
if($data->facing) {
$new_array = explode(',',$data->facing);
}
@if (is_array($new_array) && count($new_array) > 0)
@foreach($new_array as $info)
<option>{{$info}}</option>
@endforeach
@endif
Blades @foreach
directive is just a wrapper around PHPs native foreach
:
@foreach(explode(',', $data->facings) as $info)
<option>{{ $info }}</option>
@endforeach