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?

2
  • 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(); Commented Dec 4, 2021 at 19:55
  • The when() method executes the callback in second parameter only if the first parameter is true. laravel.com/docs/8.x/queries#conditional-clauses Commented Dec 4, 2021 at 19:56

4 Answers 4

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'));
Sign up to request clarification or add additional context in comments.

2 Comments

Hey @Md Sohanur Rahaman :) that's a nice way, looks way tidier than my current code. I just tried it and it worked like charm, thank you for taking your time and answering :)
That's My pleasure. Have a Good Day . Happy Coding :)
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

2 Comments

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 :)
You can follow me at github and knock me any time for getting solution related to php .
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]);

1 Comment

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!
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])`

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.