Hi Developer,
We will discover how to build a new custom class in Laravel 11 framework in this post.
With Laravel 11, the application skeleton is more compact. A simplified application structure, rate limitation by the second, health routing, etc. are all introduced in Laravel 11.
A new artisan command for creating custom classes was added in Laravel 11. In Laravel 11, we can use the following command to create a new class.
php artisan make:class {className}
It is possible to make and utilize a new class. I’ll walk you through the process of making a class and using it in a Laravel application. The basic example is visible to you:
Example of a Laravel Create Class:
In order to build the “Helper” class, you must first execute the following command. Let’s duplicate it:
php artisan make:class Helper
The Helper.php file’s ymdTomdY() and mdYToymd() functions will then be created. After that, we’ll make use of such features as support. Now let’s modify the code that is now in the Helper.php file.
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');
}
}
We can now use those functions as follows in your controller file:
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-07-03');
$newDate2 = Helper::mdYToymd('07/03/2024');
dd($newDate, $newDate2);
}
}
Output:
You will see the following output:
"03/07/2024"
"2024-07-03"
You can write more functions and use them.
I hope it can help you…