Monday, 15 May 2023

Laravel class base component

 => "  What is Laravel Component? "

  

  A component is a piece of code, which we can reuse 

  in any blade template.

  

=> " when used ?"


   It’s something similar to sections, layout time


=> "example"


  we use the same header for each template, so we can 

  create a Header component, which we can reuse.


=> " There are 3 type of 

    1) class based components 

    2) inline component

    3) anonymous components. "

    

    1) class based comment 

    

    => " To create a class based component, 

    you may use the" make:component "Artisan command."

    

    => php artisan make:component Alert

    

    => "The " make:component "command will place the component 

      in the app/View/Components directory:"

      

    => "The command will also create a view template for the component. 

       The view will be placed in the" resources/views/components 

       "directory."


=> Rendering Components


 -> "To display a component, you may use a Blade component tag 

    within one of your Blade templates. Blade component tags start

    with the string "x- "followed by the kebab case name of the

    component class: "

    

 -> example

   

   <x-alert/>


<?php

 

namespace App\View\Components;

use Illuminate\View\Component;

class Alert extends Component

{

    public $type;

    public $message;

    public function __construct($type, $message)

    {

        $this->type = $type;

        $this->message = $message;

    }

 

    public function render()

    {

        return view('components.alert');

    }

}


When your component is rendered, you may display the contents of your component's public variables by echoing the variables by name:

<div class="alert alert-{{ $type }}">
{{ $message }}
</div>

No comments:

Post a Comment

Other post

1) The create() Method The  create()  method is a powerful and convenient way to store data in your Laravel application. It is typically use...