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 Flutter http request to call api

Simple way to call API in flutter using http package

January 11, 2022January 15, 2022

Every client side application require connection to server to share the data and get most updated data from server. In the same way flutter also require to call the APIs’ and get data from server and also store the data in server. So in this article i will show you…

Read More

What is httpd.conf and httpd-vhost.conf file in apache?

August 28, 2021August 28, 2021

https.conf is main file of apache web server to handle the requests. Apache httpd.conf file generally located at /etc/httpd/conf/httpd, /etc/apache2/ , /etc/apache2/sites-enabled/ in ubuntu. httpd.conf contains the information of server root and port used routing. httpd-vhost contains additional information of virtual host.

Read More
Uncategorized update page data without reload

How to update page data without reload in jQuery?

October 24, 2021November 17, 2023

If you are beginner then this post is for you. For showing the new content on page without refresh we will use JQuery Reload Page without Refresh and server side PHP. In this article, I will demonstrate to show the latest content from server without refreshing the page. This is…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version