Laravel View

Last Updated: March 2, 2022

You can use Laravel view to put your all HTML code and you can pass data from Controller to View. The view is located at resources/views

Another name for the view file is the blade file

How to create Laravel View

Create the following file under resources/view/book and save the file as index.blade.php

<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>

How to pass data from Controller to View

class BookController extends Controller
{
    public function index(){

    	return view('book.index', ['books' => array("Book A","Book B","Book C")]);

    }
}

Passing array to View using the global view helper.

Now you can create the index.blade.php file inside the resources>view>book directory.

List of Books

@foreach($books as $k => $v)		
{{ $v }} 
@endforeach
    	

Finally you can see the following output in your browser.

list of books

Route::View()

Pass the request directly to the view (without going to Controller)


Route::view('/','welcome');
// return resources/views/welcome.blade.php

View Composer to Share Variable with Other Views

Use the share() function to share variables with every other view

view()->share("key","value");
Test