Laravel Views

Laravel provides two different syntaxes to handle your view. 1) you can use View class and 2) you can use view(). 

As we all know Laravel is MVC framework. Model, view, and controller it is the best technique to separate your web application into different layers such as database logic, business logic, and view.

In Laravel that is blade template engine will help you to manage your view file. The blade is template engine that converts php code to HTML code runtime.

You have to save your each and every view file into resources/views folder.

I am using Laravel 5.3 fresh installation setup. when you open views directory you can notice welcome.balde.php file. You notice that file extension is .blade.php. You have to save you all views file with .blade extension.

To show your view from the controller you can use three different syntaxes:

View::make('Your View Name Goes here');
// Or
view()->make('Your view name here')
// Or
view('View file name here')

Three different syntax but that will returns you same output. Of course, you can pass variables to your view file using with()  method or using share().

For example, I want to share my $post variable into my welcome.blade.php file. 

// app/Http/Controllers/Controllers.php
public function testFunction(){
   $post = 'Hello';

   return view()->make('welcome');

   return view()->make('welcome')->with('post',$post);

You can share multiple variables with specific key and value can be anything. 

If into your view or blade template file to print $post variable you can use special syntax something like this:

// resources/views/welcome.blade.php

<h1>{!! $post !!}</h1>

<h2>{{ $post }}</h2>

<h3>{{{ $post }}}</h3>

To print any variable you can use above syntaxes. 

of course, you can use if condition and for loop and everything that you write into normal PHP file just make sure you stick with blade template syntax:

@if( condition )
 // Test Code

@foreach( $variable as $key )
 // Test Code

Sometimes it's very important to perform some operation into view file such as to calculate variables or print calculated value after some specific operation at you can use php blocks into blade file to process on variable and then you can print it. 

For example, I want to plus counter in the loop so I can write code like this:

// resources/views/welcome.blade.php

@php $counter = 0; @endphp

@foreach($data as $key => $value)

  @php $counter++; @endphp


It's very important to end every block into view either you will get an exception for syntax. Some of best advantages of Laravel blade is you can extend Layout. You can create one single header and footer and every time you can extend it into your view file.

Laravel cache your view into storage folder. You can clear it by using a command that is:

php artisan view:clear

You can get check best blade syntaxes here. Or you can know more about Laravel views at here.

Tutorial By Gautam Patadiya