Is there any way to render a Laravel view to a string?

0

I would like to know if there is any way to save the return of a view in Laravel to a string .

Generally, we return to view in order to "print" the result of an action on the page:

 function getIndex() {
      return view('hello');
 }

I tried to save a variable, but it is returning Illuminate\View\View(Object) when I execute a var_dump .

Example of what I have so far:

 $view = view('hello');

 var_dump($view); // retorna Object

Is there any way to assign string of view to a variable?

    
asked by anonymous 24.08.2016 / 15:14

1 answer

0

In Laravel, View implements a method called __toString . With it it is possible to get a string from an object when you make a cast for string .

Then change your code to:

$view = (string) view('hello');

var_dump($view);

You can still call the view('hello')->render() method.

In versions prior to Laravel 5, you had to call View::make('hello')->render() .

    
24.08.2016 / 15:18