0

How can I correct display data from array. I guess it's a foreach loop, but I do not know how to bite it. I just need only header. It's in table calendars.

Thanks so much!

@foreach($list as $item)
    {{ $item->header }}
@endforeach

I tried the above code but throws out an error, unknown value "header"

@foreach($results as $type => $list)
    <div class="single-contact-information mb-30">
        <h3>{{ $type }} </h3>
        {{ $list }}  
    </div>
@endforeach

[{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}]

Edit: SearchController:

public function search(Request $request)
{
    $search = $request->get('search');
    $results = [];
    $results['calendars'] = DB::table('calendars')->where('header', 'like', '%'.$search.'%')->get();

    return view('pages.search', ['results' => $results]);
}

2 Answers 2

1

You should try this:

[{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}]

Above value is json then use below code

    $list = [{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}];



@foreach($list as $item)
    {{ $item['header'] }}
@endforeach

    $list = json_decode($list);

    @foreach($list as $item)
        {{ $item['header'] }}
    @endforeach

Updated Answer

@foreach(json_decode($list) as $item)
        {{ $item->header }}
    @endforeach
Sign up to request clarification or add additional context in comments.

3 Comments

but should I decode json in SearchController? Look my edit
"Cannot use object of type stdClass as array"
Thats it :* Thanks so much :)
0

try this one

used json_decode function here

@foreach(json_decode($list) as $item)
    {{ $item['header'] }}
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.