Skip to content
Advertisement

What is the difference between “return view” and “return make:view”?

I am putting a project together by following this tutorial: Laravel 8: Basic CRUD Blog Tutorial with Bootstrap https://www.parthpatel.net/laravel-8-crud-blog-tutorial/

When the PostController index has

public function index()
    {   $posts = Post::all();
        return View('posts.index', compact('posts'));
    }

The exception is View [postsindex] not found but when the return is

return view::make('posts.index', compact('posts')); 

  

The exception is

Class 'AppHttpControllersView' not found

Can someone explain the difference? What is the correct syntax for the return

Advertisement

Answer

view is a helper function for dealing with the same View Factory as the facade uses:

return view('posts.index', compact('posts'));

Using View::make is using the View facade as a static proxy to the view factory:

return View::make('posts.index', ...);

Since you have not aliased the View class PHP is assuming when you reference View that you mean View in the current declared namespace of the file, which is AppHttpControllers, so it is looking for AppHttpControllersView. You would need to alias this reference for View or use its Fully Qualified Class Name:

use IlluminateSupportFacadesView;
...
return View::make(...);

Or without the alias:

return IlluminateSupportFacadesView::make(...);

view(...) and View::make(...) are both causing make to be called on the View Factory to create a new View instance.

Laravel 8.x Docs – Views – Creating and Rendering Views view() View::make()

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement