How to manage Laravel with PHP Artisan and the Web Terminal?

Procédure

php artisan is the command line interface included with Laravel. It provides many useful commands for developing and managing a Laravel application. Here is a guide to some of the most common artisan commands and how to use them.

Before reading this documentation, we invite you to access your hosting's Web Terminal.

View the list of available commands

To see all the commands available in Laravel :

⌨ Command
php artisan list

Common commands

Start the development server

To start the integrated development server :

⌨ Command
php artisan serve

Starts a local development server to run your Laravel application.

By default, the server starts on http://localhost:8000. You can specify a different port:

⌨ Command
php artisan serve --port=8080

Migration management

Migrations are used to manage the database structure.

  • Creating a new migration:

    ⌨ Command
    php artisan make:migration create_users_table
    Creates a new migration file for the database.

    Run migrations:

    ⌨ Command
    php artisan migrate

    Executes the database migrations and updates the database schema.

  • Undo the last migration:

    ⌨ Command
    php artisan migrate:rollback

Managing templates

Templates are used to interact with the database tables.

  • Create a new model:

    ⌨ Command
    php artisan make:model User
  • Create a model with a migration, a controller and a factory:

    ⌨ Command
    php artisan make:model User -mcr

Managing controllers

Controllers manage the logic of the application.

  • Create a new controller:

    ⌨ Command
    php artisan make:controller UserController
  • Create a resource controller:

    ⌨ Command
    php artisan make:controller UserController --resource

Managing views

Views manage the presentation of the application.

  • Creating a new view (using Blade): Laravel does not have a dedicated artisan command for creating views. You simply create a new file in the resources/views directory.
Seeder management

The seeder is used to populate the database with test data.

  • Create a seeder:

    ⌨ Command
    php artisan make:seeder UsersTableSeeder
  • Run the seeder:

    ⌨ Command
    php artisan db:seed
  • Run a specific seeder:

    ⌨ Command
    php artisan db:seed --class=UsersTableSeeder

Cleaning the cache

Laravel uses different types of cache to improve performance.

  • Empty the application cache:

    ⌨ Command
    php artisan cache:clear
  • Empty the configuration cache:

    ⌨ Command
    php artisan config:clear
  • Clear the route cache:

    ⌨ Command
    php artisan route:clear
  • Generating a cache file for routes and improving performance

    ⌨ Command
    php artisan route:cache

  • Clearingthe cache for compiled views:

    ⌨ Command
    php artisan view:clear

Other commands

⌨ Command
php artisan tinker

Launches Laravel's interactive console for testing code and interacting with your application.

⌨ Command
php artisan storage:link

This command creates a symbolic link named storage in your project's public directory.

Complete example

Here is an example of a typical workflow using php artisan:

  1. Create a new model with a migration and a controller:

    ⌨ Command
    php artisan make:model Product -mcr
  2. Write the migration in database/migrations/YYYY_MM_DD_create_products_table.php and add the necessary fields.

  3. Run the migration:

    ⌨ Command
    php artisan migrate
  4. Create a new route in routes/web.php:

    ⌨ Command
    Route::resource('products', ProductController::class);
  5. Start the development server:

    ⌨ Command
    php artisan serve
  6. Access the application via the browser and interact with the products via the routes generated automatically by the resource controller.

php artisan is a powerful and versatile tool that simplifies many common tasks in Laravel development. It allows you to manage the database, generate code, start a development server and much more, making the development of Laravel applications more efficient and organised.

Composer documentation: https: //laravel.com/docs/11.x/artisan

Conclusion

You now know how to :

  • Use the php artisan list command to display all the commands available in Laravel.
  • Start the development server with php artisan serve and even specify a different port.
  • Manage your migrations, from creation to execution and cancellation using the various artisan commands.
  • Create and manipulate templates to interact with the database.
  • Setting up controllers to manage the logic of your application.
  • Create views using the Blade template system, even if this is done manually.
  • Use seeders to populate your database with test data.
  • Clean up the cache of different parts of the application to ensure that it works properly.

By following these steps, you'll become a true php artisan maestro, able to orchestrate the development of your Laravel applications with ease and efficiency 🎼👨‍💻.

Thank you for following us so far! If you have any questions or feedback on using these commands, don't hesitate to leave a comment below. Your experience enriches our community. See you soon for more exciting developments with Laravel! 😊🚀

help.lws.net/a/1921
Was this article helpful?

Yes

No

Read 21 231 times

Thank you! Feel free to ask questions about our documentation if you would like more information, and help us improve it.

Similar articles

A question about this article?

Ask the LWS team and the community. Answers are published after moderation.

Ask the LWS team and its community a question

GDPR: Controller: LWS-Ligne Web Services. Purpose: moderation and public publication of your question, possible notification of an answer. Legal basis: consent (Art. 6(1)(a) GDPR). Email retention: 90 days after notification, 12 months maximum without an answer. To exercise your rights, see the GDPR section of our Terms and Conditions.