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

Asked

Viewed 353 times

0

I wonder if there is any way to save the return of a view on Laravel in a string.

Usually we return to view to "print" the result of an action on the page:

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

I tried to save in a variable, but it’s 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 to string of view to a variable?

1 answer

0


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

Then change your code to:

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

var_dump($view);

It is still possible to call the method view('hello')->render().

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.