How to Create Custom Class in Laravel 11

  Mar 2024
  ITSolutionsGuides
  Category: Laravel
How to Create Custom Class in Laravel 11

Welcome To ITSolutionsGuides,


In this It solutions guides we will see How to Create Custom Class in Laravel 11. Laravel provided various set of the class. Lets see how to create our custom class in laravel 11. To create a custom class in Laravel 11, begin by defining your class within the app directory. Use the artisan command to generate the class skeleton. Implement desired methods and properties to tailor it to your application's requirements. Make sure to adhere to Laravel's naming conventions and namespace structure for seamless integration and readability. Lets use the Laravel features like facades, service providers, and dependency injection to enhance the functionality and reusability of your custom class.

Let's Start Coding

Lets create the class using the following artisan command,

php artisan make:class Helper

Let's Start Coding

the above artisan command will create the Helper.php file in the app directory, Lets edit the file we will edit this file to parse the date to the desired format.


app/Helper.php

<?php
  
namespace App;
  
use Illuminate\Support\Carbon;
  
class Helper
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public static function ymdTomdY($date)
    {
        return Carbon::parse($date)->format('m/d/Y'); 
    } 
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public static function mdYToymd($date)
    {
        return Carbon::parse($date)->format('Y-m-d'); 
    }
}

Let's Start Coding

Lets create the controller and then use the class we created, creating controller

we should add the use App\Helper; in the controller in order to use that,


app/Http/Controllers/TestController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Helper;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $newDate = Helper::ymdTomdY('2024-03-01');
        $newDate2 = Helper::mdYToymd('03/01/2024');

        dd($newDate, $newDate2);
    }
}

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides