Laravel 5中的多个数组类别

Laravel 5中的多个数组类别

问题描述:

I'm using this scope for my projects:

@foreach($projects as $project)
{{$project->subcategory}}
@endforeach

Every projects has multiple subcategory, so it seems like this:

["cat1","cat2"]

But I wanna see like this:

cat1 cat2

What should I do? Thanks!

Loop through the subcategory array if you have array

@foreach($projects as $project)
    @if(isset($project->subcategory) && !empty($project->subcategory))
        <p>
        @foreach($project->subcategory as $subcategory)
            {{ $subcategory }}
        @endforeach
        </p>
    @endif
@endforeach

Updated but you have string in the subcategory, this can also be done with regex.

@foreach($projects as $project)
    @if($project->subcategory != '')
        <p>
        {{ str_replace(['"',"[","]"],'',$project->subcategory) }}
        </p>
    @endif
@endforeach

Updated this regex can also be used

preg_replace('/[^a-zA-Z0-9,]/', "", $project->subcategory)

See the code below:

@foreach($projects as $project)
    @if(isset($project->subcategory))
       @foreach($project->subcategory as $subcategory)
          {{ $subcategory }}
       @endforeach
   @endif
@endforeach