New Artisan Commands in Laravel 11

 




Laravel 11 introduces new Artisan commands to enhance developer productivity and streamline various tasks. Here’s an overview of some of the notable new Artisan commands and how to use them:

Notable New Artisan Commands in Laravel 11

1. artisan make:job-sync

The make:job-sync command creates a new synchronous job class. Synchronous jobs are processed immediately, without being queued.


php artisan make:job-sync ProcessOrder

This command generates a new job class in the app/Jobs directory:

namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class ProcessOrder { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct() { // } public function handle() { // Job logic here } }

2. artisan make:trait

The make:trait command creates a new trait class. This can help in organizing reusable pieces of code.


php artisan make:trait ExampleTrait

This command generates a new trait in the app/Traits directory:


namespace App\Traits; trait ExampleTrait { public function exampleMethod() { // Trait method logic here } }

3. artisan make:observer

The make:observer command creates a new observer class. Observers allow you to listen for Eloquent model events.

php artisan make:observer UserObserver

This command generates a new observer class in the app/Observers directory:

namespace App\Observers; use App\Models\User; class UserObserver { public function created(User $user) { // Logic to execute when a user is created } public function updated(User $user) { // Logic to execute when a user is updated } // Other model events... }

To register the observer, add it to your AppServiceProvider or a dedicated service provider:

namespace App\Providers; use App\Models\User; use App\Observers\UserObserver; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function boot() { User::observe(UserObserver::class); } }

4. artisan model:show

The model:show command provides detailed information about a model, including its relationships, attributes, and other metadata.

php artisan model:show User

Output example:

yaml

Model: App\Models\User Table: users Primary Key: id Timestamps: true Attributes: - id - name - email - created_at - updated_at Relationships: - posts: hasMany(App\Models\Post)

5. artisan route:cache-clear

The route:cache-clear command clears the route cache without needing to clear the entire application cache.

php artisan route:cache-clear

Usage Examples

  1. Creating a Synchronous Job:

    php artisan make:job-sync ProcessOrder
  2. Creating a Trait:

    php artisan make:trait ExampleTrait
  3. Creating an Observer:

    php artisan make:observer UserObserver
  4. Showing Model Information:

    php artisan model:show User
  5. Clearing Route Cache:

    php artisan route:cache-clear

Conclusion

The new Artisan commands in Laravel 11 are designed to enhance developer efficiency and provide more granular control over various aspects of the application. These commands simplify common tasks such as creating synchronous jobs, traits, and observers, as well as providing detailed model information and clearing route caches. For more detailed usage and additional commands, refer to the official Laravel documentation.

Post a Comment

0 Comments