Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

Laravel view – A complete guide for beginner

Aman Jain, December 8, 2021December 8, 2021

Laravel view is the presentation layer of laravel. Views are used to show the html and keep separate controller login from view logic. We can created nested views, dynamic views to show dynamic data, include another view in a view. It’s not practical to use a long html in controller or in a function so views are here. Views are stored in resources\views folder and can be access using view('view_name',$array) method.

Also read How to make a component in Laravel 8 ?

Here is the simple example

Create a route

Very first step is to create the route and call our view

<?php
use Illuminate\Support\Facades\Route;

Route::get('/hello', function(){
    return view('hello');
});

Here, we used a anonymous function call the view using view method and passed a parameter string hello therefore we need to create a blade html file as well.

Create view

<body class="antialiased" style="text-align: center;">
        This is a simple hello from readerstacks.com 
</body>

Passing data to Laravel view

What if we wanted to pass data like some database model or a variable to our view then we can pass second parameter to our vies as below i

<?php
use Illuminate\Support\Facades\Route;

Route::get('/hello', function(){
    return view('hello',['some_variable'=>1]);
});

and in view

<body class="antialiased" style="text-align: center;">
        This is a simple hello {{$some_variable}} from readerstacks.com 
</body>

Nested View

Nested views means nested folder for example resources/views/hello/world.blade.php then we can access it as below

   return view('hello.world',['some_variable'=>1]);
Screenshot 2021 12 08 at 8.21.17 AM
Laravel view

How to check view exist or not in Laravel view?

Sometimes you need to check the existence of a view thus for this purpose we can use Laravel View class method exists()

Suppose we want to check a view existence of article.blade.php in folder resource/views/article then we can use it as below

<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;  

Route::get('/hello', function(){
  if (View::exists('article.details')) {  
    return "View not found";  
  }  
  return view('hello',['some_variable'=>1]);
});

Sharing data between all views

Sometimes in our application we wanted to shared some specific data to all views like user details or sessions. So in this case we can use our View facade’s share method in our application service provider App\Providers\AppServiceProvider. you need to define it in boot method of it.

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    
    public function register()
    {
        //
    }
 
    public function boot()
    {
        View::share('key', 'value');
    }
}

Related

Uncategorized

Post navigation

Previous post
Next post

Related Posts

Uncategorized Request Method

Difference between $_POST $_GET and $_REQUEST in PHP

August 19, 2021November 8, 2023

PHP gives there is 3 types of request methods to by which client can send information to server. In this article we will learn difference in get vs post and also in request. So, Methods are as follow 1. $_POST Request Method $_POST is used to fetch the post request…

Read More
Uncategorized call api in angular

How to call api in angular 12?

September 10, 2021November 7, 2023

In this tutorial, we are going to call the rest API’s using the angular HttpClient. To fetch data from server we need to call api in angular so we can get the updated data from serve. Angular has vast features and library to call the web or rest API’s. Step…

Read More
Laravel Call Controller Method from Another Controller in Laravel

How to Call Controller Method from Another Controller in Laravel ?

December 2, 2023March 16, 2024

In this article we will learn Call Controller Method from Another Controller in Laravel. Sometimes we need to access the controller method from another controller to save the same code on multiple locations. Best way to achieve it is using create a separate service class or trait in PHP so…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version