When using the Laravel view helper, what kind of data is being returned?

Asked

Viewed 92 times

2

I am doubtful when commenting on the method below:

public function index()
{
    $posts = Post::orderBy('created_at', 'desc')->paginate(10);

    return view('painel.post.listar', compact('posts'));
}

It would be correct to comment in this way:

/**
 * Retorna uma instância de View
 * 
 * @return object
 */
public function index()
{
    $posts = Post::orderBy('created_at', 'desc')->paginate(10);

    return view('painel.post.listar', compact('posts'));
}

Or would Return be View and in the description instead of "Returns a View instance" would be "Returns an object"?

2 answers

2

The initial description is perfect it returns a View instance so Return is a View.

Note that the view is nothing more than an object responsible for rendering htmls.

Note also that directly on documentation We have some examples of the use and creation of the view, such as the section below:

$view = View::make('greetings', $data);

Note that the view has other methods of its own as well as for example the share that is responsible for sharing snippets from within a view as below:

 View::share('name', 'Steve');

If you repair the creation of the view itself in the first shown excerpt clearly demonstrates that we are creating a type of View object. see in $view = View::make ....

1


A pattern used by that’s how it is:

/**
 * Return Instance of View.
 * 
 * @return Illuminate\View\View
 */
public function index()
{
    $posts = Post::orderBy('created_at', 'desc')->paginate(10);

    return view('painel.post.listar', compact('posts'));
}

The helper view returns the class instance Illuminate\View\View.

  • I can output the class path?

  • @Sorry I didn’t understand your question as asssim output?

  • Let’s assume that I used the helper view(), I can know which was the class or the namespace called in this process, the intention is if possible to identify where it is.

  • 1

    Opa, I got "dd(get_class(view('painel.post.listar', Compact('posts'))));"

  • 1

    @Fabius a var_dump would already solve.

Browser other questions tagged

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