0

I need to get specific data from a session I send instead of getting the entire array from the session from the session() method. How do I capture just the "title" from the array?

In a controller:

return redirect('bladeFile')->with('search', $search);

In the blade file:

{{session('search')}}

2 Answers 2

1

I would do something like this:

@if(session()->has('search'))
    {{ session('search')['title'] ?? '' }}
@endif

I added ?? so it does not throw an error, but it will be empty in case there is no title in the array. If that's an array at all.

-- EDIT

Then you will need to iterate over it:

@if(session()->has('search'))
    @foreach(session('search') as $item)
        {{ $item->title ?? '' }} // or {{ $item['title'] ?? '' }}
    @endforeach
@endif

Let me know if it works :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply, I've tried this but I get an error message "Undefined index: title" back.
@noahi then it means that your search is not an array. Can you try in your controller dd($search) and share the result here?
It gives me a few arrays: Collection {#281 ▼ #items: array:3 [▼ 0 => {#283 ▼ +"id": 4 +"key": "something" +"creatorId": "1" +"creatorName": "noah" +"title": "Team 11" +"created_at": null +"updated_at": null } 1 => {#282 ▶} 2 => {#285 ▶} ] }
0
{{ session('search.title') }}

You can use "dot notation" to get a value from an array index. Assuming you actually have what you think you have in the search var.

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.