Support & Downloads

Quisque actraqum nunc no dolor sit ametaugue dolor. Lorem ipsum dolor sit amet, consyect etur adipiscing elit.

s f

Contact Info
198 West 21th Street, Suite 721
New York, NY 10010
[email protected]
+88 (0) 101 0000 000

How to delete a user in custom Artisan Command line in Laravel?

Laravel is a full-stack framework that offers a lot of artisan commands to automate various actions, like creating a controller, seeding the database, and starting the server. However, when you build custom solutions, you have your own special needs, which could include a new command. Laravel doesn’t limit you to only its commands; you can create your own in a few steps.

Here are the steps for how to create a new artisan command.

Step 1: Create a new Laravel application

Step 2: Create a command

Use the make:command command to create a new command. Simply pass in the command name, like so:

The command creates a file named RemoveUser.php, named after the command name, in a newly created Commands directory in the Console folder.

The generated file contains the configurations of the newly created command that are easy to understand and edit.

Step 3: Customize command

First, set the command signature. This is what would be put after php artisan to run the command. In this example, we will use remove:user, so the command will be accessible by running:

To do this, update the signature property of the command, like this:

Next, set up a suitable description that would show up when php artisan list displays the command with other commands.

To do this, update the $description property to match this:

Finally, in the handle() method, perform whatever action you intend it to perform. In this example, the number of users on the platform is echoed.

Step 4: Test command

In the terminal, run the command to see the number of users in your database.

Now, when php artisan remove:user 1 is run, you should see something like this:

Sagar Doshi

CEO / Managing Partner

[email protected]

Post a Comment