Updated April 11, 2023
Introduction to Laravel Artisan
Artisan is a command line interface that is to be found within the Laravel framework. One of the advantages of Artisan is that it is quite helpful towards the creation of the applications. It has got a robust list of commands which make life easier for the developer and save a lot of time. The other advantage that Artisan provides is custom command which is available to developer on top of the inclusive commands inherent to it.
What is Laravel Artisan?
- Laravel Artisan is one of the three command line interfaces found in the Laravel framework.
- It is a helpful command line interface, assisting developers into developing applications though the numerous easy to read commands. One can also create custom codes in order to increase the efficiency of the applications.
- But the efficacy of Artisan does not end here.
- Developers can generate migrations, publish assets of packages and many similar tasks. Artisan has a whole lot of built in command which are a boon to the developer.
- Though a whole lot generally work on custom commands, there are many who prefer the inbuilt ones.
To get access to the host of commands type the following line:
php artisan list
- This query will give a list of commands which certainly increases the efficiency of the entire process and saves a whole lot of time.
- With these commands one can go ahead and create a plethora of functions, namely, controller, migration, create, mail, et al.
- Laravel 3.7 was the initial offering from PHP and it was quite basic in its code structure. Though in spite of that, it had some excellent features to boot.
- With the release of Laravel 5.7, added on to the newer sets of commands, some existing packages too are available.
- Laravel 4, is primarily dependent on the Symfony framework.
How does Laravel Artisan work?
- The Laravel Artisan serve command helps run application on the PHP Development Server.
- A developer has the option to use Laravel Artisan serve for a variety of roles.
- There are other two functions that the Laravel Artisan serve support. The change of application address by using the host and port.
- The application’s port can be changed by using the port option.
- The Laravel Artisan works in two ways, through the inbuilt commands and custom commands.
- The Laravel Artisan has a robust set of inbuilt commands that can help one create a variety of functions.
- On the other hand, developers also have the option of creating their custom made commands.
Example of Laravel Artisan
Example which will exemplify how a custom command can be created:
Create the custom command:
php artisan make:command <command_name>
Execute the command on the terminal:
php artisan make:command CreateEmployeeName
A file is now created in the directory: app/console/Commands
The name of the file would be: CreateEmployeeName
The complete code would be:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CreateEmployeeName extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $user = 'command:name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}
The immediate next step is to go ahead and update the Laravel command which we have just created.
While doing that a few queries have to defined for the command structure to properly function:
- $user: Create admin
- $description: Create the account of the user which would be having admin role.
- The role field which is located in the user table also needs to be updated.
But we are still not complete with the steps. We would have to register the command in the list, without which a runtime error would appear stating that the command still does not exist. The Kernel.php file which is located within the app/Console directory needs to be updated.
protected $commands = [
Commands\CreateEmployeeName::class,
];
Once the Kernel.php has been updated, the custom command can now be run since it has become a part of the list. You can check the same by using the list command. However, if you still think this will work, you are wrong. It won’t. Commands work on logic, which has to be built.
Once the migration table is created update the model array:
protected $fillable = [
'name', 'email', 'password', 'role'
];
Now you can go ahead and update the handle() as per your wish:
$newuser = [
'name' => 'XYZ',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'role' => 'Manager'
];
Few quick examples to look at:
1. To begin a Laravel Artisan project
Code:
php artisan serve
Output:
2. To enable the caching mechanism
Code:
php artisan route:cache
3. To view help, options and arguments
Code:
php artisan help serve
4. Generating a new command class
Code:
php artisan make:console GoCommand
Conclusion
PHP Artisan has all the elements which will help the developer build a complete application. And as we have discussed, it is not simply limited to building of applications. One can do a host of other activities taking the help of the list of commands, that PHP Artisan holds. Also, with custom coding, the example of which we encountered above, this entire process of development becomes a lot more customized and personal. Needless to say, there is a rapid increase in the efficiency level too.
Recommended Articles
We hope that this EDUCBA information on “Laravel Artisan” was beneficial to you. You can view EDUCBA’s recommended articles for more information.