1

I have an overview site with a filter function on top. The user can filter 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.

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Frevelman
  • 338
  • 3
  • 11
  • Besides the point, but you should be doing a conditional query based on the request value, instead of pulling *everything* from the database and then filtering. E.g. `$posts = Post::orderBy('titel')->when($request->filled('s'), fn ($q) => $q->where('titel', 'like', '%'.$request->s.'%'))->get();` – miken32 Dec 04 '21 at 19:55
  • The `when()` method executes the callback in second parameter only if the first parameter is true. https://laravel.com/docs/8.x/queries#conditional-clauses – miken32 Dec 04 '21 at 19:56

4 Answers4

3

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'));
STA
  • 30,729
  • 8
  • 45
  • 59
2

You can pass variable to view in several ways ..

way 1:

$variables = [
       'variable1'=>'something goes here',
       'variable2'=>'something goes here',
];
return view('view-file')->with($variables );

Way 2:

return view('view-file')->with(['variable1'=>'something goes here','variable2'=>'something goes here']);

way 3:

$variable1 = 'something goes here';
$variable2 = 'something goes here';

return view('view-file')->withVariabl1($variable1);
or
return view('view-file')->withVariabl2($variable2); 
or
return view('view-file')
->withVariable2($variable2)
->withVariable1($variable1); 

way 4

$variable1 = 'something goes here';
$variable2 = 'something goes here';

return view('view-file',compact('variable1','variable2'));

Hope that, everything is clear to you how to send variable to view in several ways. If you want to pass 1 or 2 variables and in future it doesn't need to increase variable then, way 4 is recommended. But from my view and real life experience I always recommend to use way 1

Ariful Islam
  • 696
  • 6
  • 11
  • hey, thanks for the detailed answer!! :) Appreciate it, gives a good look on what's possible, since I am new to php & laravel that's very helpful. Thank you for your time & I wish you a nice day aswell :) – Frevelman Nov 28 '21 at 12:43
  • 1
    You can follow me at github and knock me any time for getting solution related to php . – Ariful Islam Nov 28 '21 at 12:46
1

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]);
Abdullah Basem
  • 424
  • 2
  • 6
  • Ah man how obvious could it be? Thanks a lot! I didn't even think about that way since I provided the second variable as seen at the top with its own brackets. Appreciate it! I will accept your answer as soon as its possible. Have a great day! – Frevelman Nov 28 '21 at 11:37
1

in the view method, the second argument is for passing data to view, you can pass as many variables as you want like below:

return view('post.review',['variableOne' => $variableOne,'variableTwo' => `$variableTwo,'variableThree' => $variableThree])`
MANSOOR KOCHY
  • 370
  • 2
  • 9