How to pass a Static value with search results in a Laravel Controller

How to pass a Static value with search results in a Laravel Controller

Problem Description:

I have the following method defined in my controller. I want to pass the value of $title along with the search results so that it can be displayed at the top of the blade page, but I am unsure how to do it.

    public function index_sog(Request $request)
    {
        $title = 'Standard Operating Guideline';

            return view('knowledgebase.index', [
                'kbase' => Knowledgebase::orderBy('category', 'asc')
                ->filter(request(['tags', 'search']))
                ->where('type','SOG')
                ->paginate(20),
                'search' => $request->input('search')
            ]);
    }

My output…

<h4>{{//TITLE SHOULD GO HERE//}}</h4>
        <div class="panel-container show">
            <div class="panel-content">
                
                @foreach ($kbase->groupBy('category') as $category => $group)
                <table class="table">
                        <tr>
                            <th colspan="3" class="text-center bg-fusion-50"><strong>{{ $category }} <strong></th>
                        </tr>
                        @foreach ($group as $kb)
                            <tr>
                                <td>{{ $kb->title }}</td>
                                <td></td>
                                <td></td>
                            </tr>
                        @endforeach
                </table>
                @endforeach
            </div>
        </div>

Solution – 1

For example you can do this way:

return view('knowledgebase.index', [
                'kbase' => Knowledgebase::orderBy('category', 'asc')
                ->filter(request(['tags', 'search']))
                ->where('type','SOG')
                ->paginate(20),
                'search' => $request->input('search')
            ])->with('title', $title);

By adding the ->with() method to the return.
You can also put it inside the array of variables that you already have in return.

And then in your view:

<h4>{{ $title }}</h4>

Solution – 2

ADD on return variables. And you can use on blade like {{ $title }}

> return view('knowledgebase.index', [
>                 'kbase' => Knowledgebase::orderBy('category', 'asc')
>                 ->filter(request(['tags', 'search']))
>                 ->where('type','SOG')
>                 ->paginate(20),
>                 'search' => $request->input('search'),
>                 'title' => $title
>             ]);
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject