How to pass multiple variables to a View in Laravel?

0

How do I pass multiple variables to the same view? Something like:

return "view('textos.index', ['textos1' => $textos1, 'textos2' => $textos2, 'textos3' => $textos3]);"
    
asked by anonymous 02.12.2016 / 22:56

2 answers

3

It's just like this:

return view('textos.index', [
                 'texto1' => $texto1,
                 'texto2' => $texto2, 
                 'texto3' => $texto3
             ]);

And in the view you access it like this:

<div>
    <p>{{$texto1}}</p>
    <p>{{$texto2}}</p>
    <p>{{$texto3}}</p>
</div>

The only thing wrong here is that you put quotation marks around, so you were returning String

    
09.12.2016 / 19:37
2

In your case, you can also use the compact function.

This function captures the local variable names and creates an associative array with the appropriate values.

So, it could be done like this:

$texto1 = 1;
$texto2 = 2;
$text3 = 3;

return view('home', compact('texto1', 'texto2', 'texto3'));
    
09.12.2016 / 20:00