Archive

Posts Tagged ‘programing’

Laravel Blade Templates: An Introduction to Laravel’s Templating Engine

January 22, 2023 Leave a comment

Laravel is a powerful PHP web application framework that provides developers with a wide range of tools and features to help them build robust and high-performing web applications. One of the most important features in Laravel is its templating engine, known as Blade.

Blade is a simple yet powerful templating engine that allows developers to create and reuse template files for their views. It provides a clean and elegant syntax for creating templates and eliminates the need for complex PHP code in views. This makes it easy to separate the concerns of the application and keep the view files clean and organized.

One of the key features of Blade is its use of template inheritance. With template inheritance, you can create a layout template that contains the common elements of your site, such as the header and footer, and then extend this template in your other views. This allows you to change the layout of your entire site by modifying just one file.

Blade also supports template sections, which allow you to define reusable sections of a template that can be overridden in child templates. This is useful for creating reusable components, such as sidebars and modals, that can be easily customized for different pages.

In addition to these features, Blade also supports template directives, which are special tags that can be used to control the flow of a template. These directives include @if, @else, @foreach, @while, and many more. These directives make it easy to add logic to your views and keep the code clean and organized.

Laravel’s Blade templating engine is a powerful tool that makes it easy to create and maintain complex views. With its clean syntax, template inheritance, sections, and directives, Blade makes it easy to separate the concerns of the application and keep the code organized and maintainable.

In summary, Blade is a templating engine that allows you to create views in a clean and elegant way by providing you with a simple and easy to use syntax. With Blade, you can use template inheritance, sections, and directives, making it easy to create reusable, maintainable and robust views.

Here’s an example of how you can use Blade templates in a Laravel 9 application:

Let’s say you have a home controller with an index method that returns a view.

class HomeController extends Controller
{
    public function index()
    {
        return view('home');
    }
}

In your resources/views folder, you would create a file called home.blade.php which would contain the HTML for your home page.

<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to our website!</h1>
    <p>Here's some content on the home page.</p>
</body>
</html>

Now, when a user visits the home page of your application, they will see the content of the home.blade.php file.

You can also use template inheritance in Blade. For example, you can create a layouts folder in your resources/views folder, and create a file called app.blade.php. This file can contain the common elements of your site, such as the header and footer.

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <header>
        <nav>
            <a href="#">Home</a>
            <a href="#">About</a>
            <a href="#">Contact</a>
        </nav>
    </header>
    <main>
        @yield('content')
    </main>
    <footer>
        <p>Copyright © {{ date('Y') }}</p>
    </footer>
</body>
</html>

In your home.blade.php you can use @extends(‘layouts.app’) to extend the common layout and @section to define the unique content.

@extends('layouts.app')

@section('title', 'Home Page')

@section('content')
    <h1>Welcome to our website!</h1>
    <p>Here's some content on the home page.</p>
@endsection

Now, when a user visits the home page of your application, they will see the common elements from the app.blade.php and the unique content from home.blade.php.

These are just a few examples of how you can use Blade templates in a Laravel application.

Laravel 9 Create Custom Artisan Command

January 7, 2023 Leave a comment

Artisan is the command-line interface that is included with Laravel. It is driven by the Symfony Support component.

Today we will learn how to create a custom artisan command in the Laravel application and how to use the custom laravel artisan command.

Following command will generate a file under ‘app\Console\Commands\’ with name CreateUsers.php

php artisan make:command CreateUsers

Here is the file code

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CreateUsers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $numberOfUsers = $this->argument('count');
  
        for ($i = 0; $i < $numberOfUsers; $i++) { 
            User::factory()->create();
        }  
        return Command::SUCCESS;
    }
}
?>

The handle() function holds the logic to insert the records in the database.

Let’s run our custom command to create 3 users

php artisan create:users 3

You can also view list of commands with following command.

php artisan list