GreatDayDan
GreatDayDan

Reputation: 169

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 [posts\index] not found but when the return is

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

  

The exception is

Class 'App\Http\Controllers\View' not found

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

Upvotes: 0

Views: 492

Answers (1)

lagbox
lagbox

Reputation: 50491

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 App\Http\Controllers, so it is looking for App\Http\Controllers\View. You would need to alias this reference for View or use its Fully Qualified Class Name:

use Illuminate\Support\Facades\View;
...
return View::make(...);

Or without the alias:

return \Illuminate\Support\Facades\View::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()

Upvotes: 2

Related Questions