After running a query, I get an array of results (articles) where each of them has their properties like id
, name
and tags
.
This is the code where I display those values:
@foreach ( $results['matches'] as $doc => $docinfo )
...
{{$docinfo['attrs']['s_tags']}}
@endforeach
But the problem is that some of articles have a long list of tags, for example:
"s_tags" => "Tech, geben, rasant, Wechsel, Pakete, Kunden, Details, Intel, Microsoft, Angebote, App, Web"
How can I assure that maximum 5 tags are displayed. So in this case it would be:
"Tech, geben, rasant, Wechsel, Pakete"
and of course, if there are articles with the number of tags less than 5, I want to display them all.
{{ collect(explode(', ', $docinfo['attrs']['s_tags']))->take(5)->implode(', ') }}
,
to turn it into an arraycollect
to get a fluent readable api,
to get your comma delimited list backUse array_slice()
like this:
I don't know about your tag array. let it be $tag
.
$five = array_slice($tag, 0, 5);
simple use implode
and explode
and array_slice()
{{ implode(', ',array_slice(explode(',',$docinfo['attrs']['s_tags']),0,5)) }}
Implode : Break the array into string
Explode : Break the string into array
Array_slice : Get the particular slice of element from array with limits
TRy this it will help from query side
DB::table('articles')->chunk(5, function($results)
{
foreach ($results['matches'] as $doc => $docinfo)
{
{{$docinfo['attrs']['s_tags']}}
}
});