Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Create Custom Directives in Laravel

How to create custom directive in Laravel 8 ?

Aman Jain, November 11, 2021November 5, 2023

Laravel blade is powerful template engine which makes short tags directive and easy to reuse templates. Most of the templates restrict to use other language but blade gives more flexibility and we can use PHP as well in template. Directives start with prefix @ and accepts parameter. In this article i will take a simple example to demonstrate the Laravel blade directive.

To create the custom directive Laravel provides Blade::directive method which accepts two parameters, one is name of directive and second callback function when its called from view.

Let’s begin with simple steps:

Step 1: Create a laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2: Register Directive in AppServiceProvider

Next step is to register the directive in AppServiceProvider, In Laravel service providers are central place for bootstrapping any component, module, directive or itself Laravel. therefore, let’s register our directive in AppServiceProvider. In this example i will create a simple directive to show the long text in trimmed text format with dot.

<?php

namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
        Blade::directive('ellipsis', function ($expression) {
            return "<?php strlen($expression)>100?substr($expression,0,100).'...':$expression; ?>";
        });
    }
}

Step 3: Create a view

Now, create a view in resources/views folder to check our implementation


 
        <div class="row">
         Long : {{$longText}} <br><br>
         
        Trimmed Text :  @ellipsis($longText)
        
        </div>
        
    <!-- credits -->
    <div class="text-center">
      <p>
        <a href="#" target="_top">Laravel 8 custom directive </a>
      </p>
      <p>
        <a href="https://readerstacks.com" target="_top">readerstacks.com</a>
      </p>
    </div>
   

Step 4: Create route to check implementation

Create a simple route and call the view

<?php

use Illuminate\Support\Facades\Route;
Route::get('/check-directive',function(){
        return view("custom_directive",['longText'=>'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum']);
});

Screenshot:

Screenshot 2021 11 11 at 9.20.02 AM
Laravel custom directive

Related

Php Laravel directivelaravelphptrimmed text

Post navigation

Previous post
Next post

Related Posts

Php How to Get Current Url with Query Params in Laravel

How to Get Current Url with Query Parameters in Laravel ?

August 27, 2022March 16, 2024

Sometimes in our application we want to Get Current Url with Query Parameters in Laravel. It can be complete url, route name and query params too so that we can fetch the data accordingly and show to user on basis of conditions in our view or controller. We can easily…

Read More

Create custom library or custom class in Laravel 8 / 9

December 21, 2021November 6, 2023

Laravel provides various packages and library to enhance our application features and functionalities but sometime we want our custom class in laravel to handle the few features of the application. Thus I this article i will show you to create a class and use it using the alias or directly…

Read More
Php How to compress and reduce image size while uploading in laravel

How to compress and reduce image size while uploading in laravel 8 ?

May 14, 2022May 14, 2022

While uploading the images in our application user can upload big size image but storing and rendering big size of image can reduce the performance of the application therefor its an important aspect to reduce the size of image without resizing it so we can keep the original size and…

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

  • June 2025
  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version