Laravel 7 Blade Components

Blade is a king of templating engine in laravel that permits you to use plan php in your view. Developers were introduced with new class based blade syntax for creating components in laravel 7. If you're conversant in VueJs components you'll find the thought is same but in PHP way


Laravel 7 Blade Components

By creating a blade component you are following DRY ( don't repeat yourself) principle . It means you'll reuse it in your project. So lets begin :

So lets begin :
First create a component by this command :



 php artisan make:component Alert


This command will generate two files
app\View\Components\Alert.php

this file handle variables and functions of the blade component.

resources\views\components\alert.blade.php

Now you can call this component in your project by "<x-alert>" , so you can see "x" is used to access component ,
now we would like to pass a variable name "title" within the component



<x-alert title="This is title"> </x-alert>


now open "app\View\Components\Alert.php" and add title variable in the class




 <?php

 namespace App\View\Components;
 use Illuminate\View\Component;

 class Alert extends Component
 {
    /**
     * The alert title.
     *
     * @var string
     */
    public $title;

    /**
     * Create the component instance.
     *
     * @param  string  $title
     * @return void
     */
    public function __construct($type)
    {
        $this->title= $title;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.alert');
    }
 }


Now $title property is accessible in our "alert" blade element. You be able to outline additional variables here and might access it within the blade element such as you can pass message or style of the alert.


more examples for the alert messages


Here we've passed 2 variables, "type" is comparable like title we tend to passed in higher than example and second is "message" however within the message variable we tend to area unit passing a PHP variable value.

now update your alert.blade.php with

 
<!-- /resources/views/components/alert.blade.php -->
 <div class="alert alert-{{ $type }}">
    {{ $message }}
 </div>


Real life example:

 
<!-- /resources/views/components/alert.blade.php -->
 <div {{ $attributes->merge(['class' => 'p-6 rounded-lg shadow-lg']) }}>
   <div class="text-xl text-orange-500">{{ $title }}</div>
    <div class="mt-4">
        {{ $slot }}
    </div>
 </div>


call it in your project:


 
<x-panel title="Update Your Info!" class="max-w-2xl">
    <h1>I AM IN THE SLOT</h1>
 </x-panel>


Thank you,🤗

Type Casting in Laravel

Casting a value means changing it to (or ensuring it is already) a particular type. Some types you might be familiar with are Integer or BooleanSimply put, type casting is a method of changing an entity from one data type to another.


Type Casting in Laravel

Why should I care about type casting :

So, When you store numbers in database - they are returned as string by default. But type casting allows you to cast them as integerrealfloat or Boolean you can convert 1 and 0 in your database to true and false

Let's do it ...

So in your model you can write :

 
 protected $casts =  [
    'status' => 'boolena',
 ]


It'll convert status(1,0) to true or false. so when you fetch any data from table it'll convert status column to true or false. you don't need to write any logic like this ...

 
if($post->status == "1"){
   // show status active 
 }else{
   // show status inactive
 }



Array & JSON Casting:


Additionally, if your table consist a column which stores serialized JSON string and you want that particular column to be automatically deserialize the attribute to a PHP array when you access it on your Eloquent model, you should use array cast. You can do it like this.

Alt Text

So, you can store JSON tags data into post table , but while fetching the posts you can convert it automatically into PHP array so now you can avoid creating tags table.


Conclusion:

As you can see, Eloquent attribute casting has a ton of potential to free us up from unnecessary repetitive logic, and also sneakily makes it a lot easier to store data in JSON in our database. Good Stuff!

OS Module in Python Programming

Introduction to Python OS Module: The OS module in Python provides functions for interacting with the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about operating system. The OS comes under Python's standard utility modules. Lets Start:-


OS Module in Python Programming

To work with the OS module, we need to import the OS module:


    import os

1. os.name:  os.name: This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’

Code:


    import os
print(os.name)


2. os.mkdir() : The os.mkdir() function is used to create new directory.

Code:


    import os
print(os.mkdir(path))


3. getcwd(): It returns the current working directory(CWD) of the file.

Code:


    import os
print(os.getcwd())



4. os.chdir(): The os module provides the chdir() function to change the current working directory.

Code:


    import os
print(os.chdir(path))


5. os.getpid(): Returns the real process ID of the current process.

Code:


    import os
print(os.getpid())
    

6. listdir(): This method in Python is used to get the list of all files and directories in the specified directory.

Code:


    import os
print(os.listdir(path))


7. os.environ: The os.environ value is known as a mapping object that returns a dictionary of the user’s environmental variables.

Code:


    import os
print(os.environ)


8. os.rmdir(): This method in Python is used to remove or delete a empty directory. OSError will be raised if the specified path is not an empty directory.

Code:


    import os
print(os.rmdir(path))


9.  os.remove(): This method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.

Code:


    import os
print(os.remove(path))


10. os.makedirs(): os.makedirs() method in Python is used to create a directory recursively.

Code:


    import os
print(os.makedirs(path))


11. os.system(): It is used to executing a shell command.

Code:


    import os
print(os.system)


12. os.walk(): The os.walk() method gives us a way to iterate over a root level path. It means that we can pass a path to this function and get access to all its sub-directories and files. 

Code:


    import os
print(os.walk(path))


13. os.error(): The os.error() function defines the OS level errors. It raises OSError in case of invalid or inaccessible file names and path etc.


14. os.popen(): This method opens a pipe to or from command. The return value can be read or written depending on whether mode is ‘r’ or ‘w’.

Code:


    import os
print(os.popen(fd,'w'))


15. os.access(): This function uses real uid/gid to test if the invoking user has access to the path.

Code:


    import os
print(os.name)


16. os.rename(old.txt, new.txt): A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and user has sufficient privilege permission to change the file.

Code:


    import os
print(os.rename("old.txt","new.txt"))


17. os.startfile(): The os.startfile() method allows us to “start” a file with its associated program. In other words, we can open a file with it’s associated program, just like when you double-click a PDF and it opens in Adobe Reader.

Code:


    import os
print(os.startfile(path))


18. os.close(): This function closes the associated file with descriptor fr.

Code:


    import os
print(os.close(file))


19. os.path: The os.path sub-module of the os module has lots of great functionality built into it.
  1. basename
  2. dirname
  3. exists
  4. isdir and isfile
  5. join
  6. split

1. os.path.basename
The basename function will return just the filename of a path. 

Code:


    import os
print(os.path.basename(path))


2. os.path.dirname
The dirname function will return just the directory portion of the path.

Code:


    import os
print(os.path.dirname(path))


3. os.path.exists
The exists function will tell you if a path exists or not. All you have to do is pass it a path.

Code:


    import os
print(os.path.exists(path))


4. os.path.join
The join method give you the ability to join one or more path components together using the appropriate separator.

Code:


    import os
print(os.join(path,path))


5. os.path.split
The split method will split a path into a tuple that contains the directory and the file.

Code:


    import os
print(os.path.split(path))


6. os.path.isdir / os.path.isfile
isdir only checks if the path is a directory and isfile only checks if the path is a file.

Code:


    import os
print(os.path.isdir(path))
    
    print(os.path.isfile(path))




Do Subscribe My Youtube Channel