Laravel 8 Views Tutorial with Examples

In this laravel tutorial we are going to discuss about Views, how to create laravel views, how to call, pass data to views etc.

Views in Laravel 8:

Basically, views belong to the MVC structure, which is used to show HTML content on the web browser. It contains all HTML code along with include CSS and js file path.

In laravel 8 we can call views from two way, one from laravel controller and from routing file also.

The location of laravel 8 views files is inside resources folder.

[laravel_folder]/resourse/views/

Example to call views in Laravel 8 via Routing Method

Step 1: For example we create a users.blade.php file in the resourse/views folder. blade is template engine which is provided by laravel for fast rendering.

Step 2: Now call this view via laravel routing method, inside web.php route file.

We can call users.blade.php view via two routing method.

First one is via get method:

Second way to call via view method directly:

Output:
laravel view output

Example to call view in Laravel 8 from Controller Method

Step 1: Create a controller ‘UserController.php’ and write a show() function to call users view.

Step 2: Now write route method to call this controller method:

This will show same output as above shown.

Note: Must add ‘UserController’ namespace in web.php route file before call controller method, otherwise it will gives you an error.

use App\Http\Controllers\UserController;

Pass URL Parameter to View: Example

To send URL parameter to view file, we need to use below code in ‘UserController.php’ controller file.

And add below route method to call view with parameter value.

To see this parameter value in web browser, need to echo parameter in users.blade.php file.

Now call this view like this http://localhost:8000/user/peter. It will display parameter value on web page.

Output:
call view with parameters

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish