Home > Back-end >  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

Time:12-06

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 >
            <div >
                
                @foreach ($kbase->groupBy('category') as $category => $group)
                <table >
                        <tr>
                            <th colspan="3" ><strong>{{ $category }} <strong></th>
                        </tr>
                        @foreach ($group as $kb)
                            <tr>
                                <td>{{ $kb->title }}</td>
                                <td></td>
                                <td></td>
                            </tr>
                        @endforeach
                </table>
                @endforeach
            </div>
        </div>

CodePudding user response:

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
>             ]);

CodePudding user response:

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>
  • Related