LARAVEL Tutorial


Laravel 7 Views Tutorial

Author:- w3alert.com
Updated Date:-11-08-2020

You have learned in the previous tutorial, laravel controller, request, response, etc. In the previous tutorial, we also learned laravel is MVC pattern based PHP frameworks. Here V stands for the Views.

Basic views are used to display presentation logic or web pages. It has held the HTML, which will be served by the web application. In laravel web applications views are placed in the resources/views directory.

A  basic view looks like this:

<html>
 <head>
  <title>Laravel Simple View</title>
 </head>
    <body>
        <h1>Hello, W3alert.com</h1>
    </body>
</html>

Laravel Return View From Route

If you want to return views file from laravel routes, Go to app/Http/routes.php and update the below code into your file:

you could do like:

Route::get('/home', function() {

    return view('home');
});

Laravel Return View From Route with Data

If you want to return views file from laravel routes with data, Go to app/Http/routes.php and open your routes file. Then pass the data separated by a comma. 

You could do like:

Route::get('/home', function() {
    
    $data = ['name' => 'W3alert.com'];

    return view('home', $data);
});

After that, open your home.blade.php file and display the passed data by routes.

You could do like

<html>
<title>Laravel Return View From Route with Data</title>
   <body>
      <h1><?php echo $name; ?></h1>
   </body>
</html>

Laravel Sharing Data with All Views

Sometimes, you need to share some data with all views. The laravel has provided the features to share data with all views.

So go to app/Providers/AppServiceProvider.php and open this file. After that, update the given boot method with your data.

You could do like:

<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {
   
   /**
      * Bootstrap any application services.
      *
      * @return void
   */

   public function boot() {
      view()->share('appName', 'Laravel View');
   }

   /**
      * Register any application services.
      *
      * @return void
   */

   public function register() {
      //
   }
}

Now you can access the variable $appName in your web application all blade views file.

you could do like:

<html>
<title>Laravel Sharing Data with All Views</title>
   <body>
      <h1><?php echo $appName; ?></h1>
   </body>
</html>

Laravel Return View From Controller

If you want to return views file from laravel controller with or without data. You can do. So Go to app/Http/controllers and open your controller file. 

You could do like:

<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }
}

If want to return views file from laravel controller with data. You can do. So Go to app/Http/controllers and open your controller file. 

<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $data = ['name' => 'w3alert.com'];
        return view('home', $data);
    }
}


After that, open your home.blade.php file and display the passed data from the controller.

You could do like:

<html>
<title>Laravel Return View From Controller with Data</title>
   <body>
      <h1><?php echo $name; ?></h1>
   </body>
</html>

Reference

This tutorial idea has taken from this https://laravel.com/docs/7.x/views