How to Create Components in laravel 9

How to Create Components in laravel 9

| |
Image Carousel

Table of Contents Contents-topics
1 What are components
2 How to setup components
3 How to use components
4 Pass data to components 

1:What are Components

Components are the one of the best feature that comes with laravel , components are define as the reusable peice of code that is being used in laravel blde fiile or we may say that laravel views. As the name defines components means to be a component of a page  , for eg in a html file there are several different components like header , sidebar , footer and sometimes  our header and footers are same but  CSS and scripts file are different according to ech page so on that time we use component.

2:How to setup Components

With an example , we inializing some scripts by using component as component are the piece of code , you may use components in the form of google analytics , some kind of loading scripts ,
Taking an HTML example from VM-Services Developer Studio , starting from the artisan command for footer  as component
Copy for creating footerCopy

php artisan make:component footer

Inside resources/views/components/footer.blade.php  you can see footer.blade.php has been created and also in app/View/Component/footer.php

3:How to use components

That blade file reffers to views in blade and php file is used for binding up the objects and send them to the component in laravel you don't have to send data every time from backend. You have to just send data for the first time in app/View/Component/footer.php and then just inialiaze this component wherever you want.

Basic blade fiile code: Copy

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Developer Corner</title>
    <!-- CSS only -->
    <link rel="stylesheet" href="" crossorigin="anonymous">
    <!-- Js -->
    <script src=""></script>
    <div class="container">
    <h4 class="mt-4">Welcome To Develpoer-Corner</h4>
        <h6 class="mt-4">Using Footer as Component</h6>
        <span id="showContent"></span>
  <!-- Footer Component -->


As you can see in the footer of this html file component is used as <x-footer></x-footer> in blade file and resources/views/components/footer.blade.php contains the basic footer code like this script code.
Footer code:

    This is footer Component by {{$name}}
    $('#showContent').html('Showing footer');

4:Pass data to components

As shown {{$name}}  variable is call from app/view/Component/footer.php  to resources/views/components/footer.blade.php  to binding up the variables
.code: Copy

namespace App\View\Components;
use Illuminate\View\Component;
class footer extends Component
     * Create a new component instance.
     * @return void
    public function __construct()
            $this->name = 'Developer-Corner';
     * Get the view / contents that represent the component.
     * @return \Illuminate\Contracts\View\View|\Closure|string
    public function render()
         $name = $this->name;
        return view('components.footer',['name'=>$name]);

Basically we use this footer.php to binding up the objects and call them separately in component because by using thhis this you don;t need to call the same variable in each controller and then in view file  So this the basic example of components in laravel ...

Tags: how to create components in laravel 9 , reusable codes laravel view , how to use peice of code in blade file in laravel 9,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More