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

Angular How to use moment js in angular

How to use moment js in angular ?

October 13, 2022March 16, 2024

This post covers how to use Moment.js in Angular. We’ll go over why Moment.js is useful, how to install it, and how to use it in Angular to manipulate dates and times. Moment.js is a powerful and flexible library for manipulating dates and times in JavaScript. It has been around…

Read More
Uncategorized Laravel database transactions

How to use database transaction in laravel eloquent with example ?

January 1, 2022February 22, 2024

Database Laravel Transaction in laravel eloquent are used to execute multiple or single database operations which maintainer ACID(atomicity, consistency, isolation, and durability) property of transactions. In laravel we can archive the database transaction by DB facade. Laravel DB facade provides two methods to handle the transactions one through DB::transaction() method…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version