Laravel Artisan commands

Here are the Laravel Artisan commands to create a migration, model, controller, view, and component:

1. Create a Migration

php artisan make:migration create_table_name_table
  • Replace create_table_name_table with your table name, e.g., create_products_table.

2. Create a Model

php artisan make:model Product
  • This creates a Product.php model inside app/Models/ (Laravel 8+).

๐Ÿ”น With Migration and Factory:

php artisan make:model Product -mf
  • -m โ†’ Create a migration file.
  • -f โ†’ Create a factory file for database seeding.

๐Ÿ”น With Controller:

php artisan make:model Product -mc
  • -c โ†’ Create a resource controller.

3. Create a Controller

php artisan make:controller ProductController
  • This creates a controller at app/Http/Controllers/ProductController.php.

๐Ÿ”น Create a Resource Controller:

php artisan make:controller ProductController --resource
  • Adds methods like index(), show(), store(), update(), and destroy().

4. Create a Blade View

Blade views are not created with Artisan. Instead, manually create them inside resources/views/.
Example:

touch resources/views/products/index.blade.php

5. Create a Component

php artisan make:component Alert
  • This creates:
    • app/View/Components/Alert.php (Component logic).
    • resources/views/components/alert.blade.php (Component Blade template).

Bonus: Create Everything at Once

For a Product feature (model, migration, controller, and factory):

php artisan make:model Product -mcrf

Let me know if you need more details! ๐Ÿš€

Leave a Reply

Your email address will not be published. Required fields are marked *