Skip to main content
Tutorial Free

Laravel 11: New Artisan "make:class" Command

March 15, 2024
1 min read

Laravel 11 introduced new Artisan commands. Let's look at the make:class command in this post.

As with every Artisan command, you can pass the file name to create or leave empty, and Laravel will ask.

make class command

Here is how the generated class looks like:

app/Actions/CreateUserAction.php:

namespace App\Actions;
 
class CreateUserAction
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
}

By passing --invokable, the generated class will have the __invoke() method.

make class command invokable

app/Actions/DeleteUserAction.php:

namespace App\Actions;
 
class DeleteUserAction
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
 
/**
* Invoke the class instance.
*/
public function __invoke(): void
{
 
}
}

As with every generated file, the class also has stubs so that you can modify them to your needs. After publishing the subs, you may change these defaults:

stubs/class.stub:

<?php
 
namespace {{ namespace }};
 
class {{ class }}
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
}

stubs/class.invokable.stub:

<?php
 
namespace {{ namespace }};
 
class {{ class }}
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
 
/**
* Invoke the class instance.
*/
public function __invoke(): void
{
 
}
}

The make:class Artisan command has additional options, which you can check by adding the --help option.

make class command help

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.