Laravel Commands and Custom Commands
Laravel provides many built-in commands that help manage the application. These commands are run using Artisan, Laravel’s command-line tool. Some common commands include:
php artisan config:cache
– Caches configuration files for better performance.
php artisan config:clear
– Clears the cached configuration.
php artisan migrate
– Runs database migrations.
php artisan route:list
– Shows all registered routes.
Creating Your Own Commands
These commands are stored under the app/Console/Commands
directory. Laravel also allows developers to create their own custom Artisan commands. You can create a command using:
Custom commands are useful when you want to update or modify anything inside the database.
For example, Suppose yourprojects
table has a type
column with five types:
- software_development
- marketing
- personal
- portfolio_websites
- blogging
You want to treat portfolio_websites and blogging as personal, but keep the original values in the database.
For new entries, when saving data, if the type is portfolio_websites or blogging, set it as personal.
For existing records, you need a Laravel command to update the database.
You first create a new idea_type column. Then, ensure:
– If type is software_development or marketing, store the same value in idea_type.
– If type is portfolio_websites or blogging, store personal in idea_type.
This can be done with a custom Artisan command to update old records automatically.
You can create a Laravel command to update the existing records in the database accordingly.
first you run the make:command Artisan command:
php artisan make:command UpdateProjectIdeaTypeColumn
Command Structure
After generating your command, go inside the UpdateProjectIdeaTypeColumn file
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; class UpdateProjectIdeaTypeColumn extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'update:project_idea_type'; /** * The console command description. * * @var string */ protected $description = 'Update the idea_type column in the projects table based on the type column.'; /** * Execute the console command. */ public function handle() { DB::transaction(function () { // Update idea_type column based on the existing type column DB::table('projects')->update([ 'idea_type' => DB::raw(" CASE WHEN type = 'software_development' THEN 'software_development' WHEN type = 'marketing' THEN 'marketing' WHEN type IN ('portfolio_websites', 'blogging') THEN 'personal' ELSE null END ") ]); }); $this->info('Project idea_type column updated successfully!'); } }
To run this command, you need to use the same name defined in the $signature property.
For example, if your signature is:
protected $signature = 'update:project_idea_type';
You can run the command using:
php artisan update:project_idea_type
Whatever name you set in $signature, you must use the same when running the command with php artisan.
Don’t forget to write a $description, as it will help you understand the purpose of the command in the future.