Home > Mobile >  Providing multiple variables in return view
Providing multiple variables in return view

Time:11-28

I have an overview site with a filter canton on top. The user can filer for location and department.

The code of the controller looks like this:

public function index(Request $request)
    {


        $posts = Post::orderBy('titel')
            ->get();

        $standorts = Standort::get();

        $abteilungs = Abteilung::get();

        if ($request->filled('s')) {
            $query = strtolower($request->get('s'));
            $posts = $posts->filter(function ($post) use ($query) {
                if (Str::contains(strtolower($post->Titel), $query)) {
                    return true;
                }
                return false;
            });

        }
        return view('posts.overview', ['posts' => $posts], ['standorts' => $standorts]);
    }

I need to provide the $abteilungs = Abteilung::get(); aswell, but when I return it like this:

return view('posts.overview', ['posts' => $posts], ['standorts' => $standorts], ['abteilungs' => $abteilungs]);

The last part in the brackets is greyed out and I can't access it.

In the blade file I access them like this:

<div class="text-left mb-4 h-12">
                    <select name="standort" id="standort" class="h-full w-full flex justify-center bg-white bg-opacity-95 rounded focus:ring-2 border border-gray-300 focus:border-indigo-500 text-base
                                outline-none text-gray-700 text-lg text-center leading-8">

                        <option selected="true" disabled="disabled">Standort</option>
                        @foreach($standorts as $standort)
                            <option value="{{ $standort->standort_name }}">{{ $standort->standort_name }}</option>
                        @endforeach
                    </select>
                </div>


                <button hljs-number">4 col-end-4 w-11/12 text-white text-2xl px-4 py-3 rounded text-base font-medium
                                bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">
                    Suchen
                </button>
            </form>


            @foreach($posts as $post)
                <div
                    hljs-number">10 grid-cols-3 grid-rows-3 gap-4 shadow-2xl mb-10 bg-gradient-to-r from-green-400 to-blue-500 border-solid border-2 border-black rounded-lg">


                    <!--Card 1-->
                    <a href="{{ route('select', $post->Stellenanzeigen_ID) }}">
                        <div
                            hljs-number">3 bg-gray-100 shadow-2xl border-solid border-2 border-gray-500 rounded-lg">
                            <div hljs-number">2 pl-6 mt-3"> {{ $post->Titel }}</div>
                            <div hljs-number">6 py-4 mt-2 ring-4 ring-opacity-90">
                                <button type="submit" hljs-number">4 py-3 rounded text-base font-medium
                                bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition
                                duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">Direkt
                                    bewerben!
                                </button>


                                <div hljs-number">2 pl-4 font-medium text-base font-bold font-serif">
                                    Standort: {{ $post->Standort }}</div>

                                <div hljs-number">2 pl-4 font-medium text-base font-bold font-serif">
                                    Kontakt: {{ $post->Kontakt }}</div>
                                <button type="submit" hljs-number">4 py-3 rounded text-base font-medium
                                bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">
                                    Details!
                                </button>
                                <div hljs-number">2 pl-4 font-medium text-base font-bold font-serif">
                                    Startdatum: {{ $post->startdate }}</div>
                                <div hljs-number">2 pl-4 font-medium text-base font-bold font-serif">
                                    Enddatum: {{ $post->enddate }}</div>

                                <div hljs-number">6 pt-4 pb-2"></div>
                            </div>
                        </div>
                    </a>
                </div>
            @endforeach

For the third part in the return statement I have this code:

<div class="text-left mb-4 h-12">
                    <select name="abteilung" id="abteilung" class="h-full w-full flex justify-center bg-white bg-opacity-95 rounded focus:ring-2 border border-gray-300 focus:border-indigo-500 text-base
                                outline-none text-gray-700 text-lg text-center leading-8">

                        <option selected="true" disabled="disabled">Abteilung</option>
                        @foreach($abteilungs as $abteilung)
                            <option value="{{ $abteilung->abteilung_name }}">{{ $abteilung->abteilung_name }}</option>
                        @endforeach
                    </select>
                </div>

Is there a way to make this work or is the return view limited to two parameters?

CodePudding user response:

You can also try in this way

    $data = [
            'posts' => $posts, 
            'standorts' => $standorts, 
            'abteilungs' => $abteilungs,
        ];
return view('posts.overview')->with($data);

Or,

return view('post.overview',compact('posts','standorts','abteilungs'));

CodePudding user response:

to pass more than one variable to your view you should do it like this

return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs]);
  • Related