Most Useful Laravel Commands

In this post we will learn about most useful commands in laravel and there explanation, Whether youre a beginner or an experienced developer, this post will help you enhance your web applications and boost your productivity.

  

How to create model, controller and migration in single command in laravel?
php artisan make:model Post -mcr

The command `php artisan make:model Post -mcr` in Laravel performs three tasks:

- `make:model Post`: Creates a new model named `Post`.

- `-m`: Generates a migration file for the `Post` model.

- `-c`: Creates a new controller for the `Post` model.

- `-r`: Indicates that the generated controller should be a resource controller.

So, this command is a quick way to set up a new model with its associated controller and migration. It's a handy tool for speeding up your Laravel development process. Please note that the model name is typically singular with the first letter capitalized.



How to create a resource controller in laravel?
php artisan make:controller --resource PostController

A resource controller in Laravel is a controller that handles all HTTP requests for a specific resource. This includes methods for each of the available resource operations (index, create, store, show, edit, update, and destroy) that correspond to the CRUD operations (Create, Read, Update, Delete) in a database.


So, this command is a quick way to set up a new controller that can handle all the basic operations for a resource. It's a handy tool for speeding up your Laravel development process. Please note that the controller name is typically singular with the first letter capitalized. For example, a `PostController` would correspond to a `posts` table.


How to create a model and migration in single command in laravel?
php artisan make:model -m Post

The command `php artisan make:model -m Post` in Laravel is used to create a new Eloquent model named `Post` and a corresponding migration file. 


The `-m` option tells Laravel to generate a migration file that you can use to create the `posts` table in your database. This is a handy shortcut that can speed up your Laravel development process. Please note that the model name is typically singular with the first letter capitalized. For example, a `Post` model would correspond to a `posts` table.


How to create a model in laravel?
php artisan make:model Post

The command `php artisan make:model Post` in Laravel is used to create a new Eloquent model named `Post`. 


Eloquent models in Laravel represent the data structure and provide an interface for interacting with your database. Each database table has a corresponding model that allows you to insert, update, and delete records from the table.


Please note that the model name is typically singular with the first letter capitalized. For example, a `Post` model would correspond to a `posts` table. This command is a quick way to set up a new model in your Laravel application.


How to create a controller in laravel?
php artisan make:controller PostController

The command `php artisan make:controller PostController` in Laravel is used to create a new controller named `PostController`. 


Controllers in Laravel are used to group related request handling logic into a single class. For example, a `PostController` class might handle all incoming requests related to posts, including showing, creating, updating, and deleting posts.


Please note that the controller name is typically singular with the first letter capitalized. For example, a `PostController` would correspond to a `posts` table.


How to clear cache in laravel?
php artisan optimize:clear

The command `php artisan optimize:clear` in Laravel is used to clear all types of cache in your application. This includes:


- Configuration cache: This cache combines all of the configuration options for your application into a single file which is loaded quickly by the framework.

- Route cache: This cache decreases the amount of time it takes to register all of your application's routes.

- View cache: This cache compiles views to increase performance when a request is made.

- Events cache: If you are using events in your Laravel application, it is recommended to cache your events.


So, this command is a quick way to reset all cache in your Laravel application without having to run multiple commands. 


How to generate app key in laravel?
php artisan key:generate

The command `php artisan key:generate` in Laravel is used to generate a secure application key and set it in your application's `.env` file. This key is used for encryption and should be kept secret. 


If you're using a version control system like git, the `.env` file won't be included when you push your project. Therefore, if someone clones your project, they will have to manually enter `php artisan key:generate` for their app to function correctly.


This command is essential for the proper functioning of your Laravel application.


How to create a middleware in laravel?
php artisan make:middleware MiddlewareName

Middleware in Laravel provides a convenient mechanism for inspecting and filtering HTTP requests entering your application. They can perform a variety of tasks such as authentication, logging, and more. 


This command creates a new middleware class in the `app/Http/Middleware` directory. You can then implement your desired logic in this middleware. Please note that you should replace `MiddlewareName` with the actual name of your middleware.


How to run local laravel project on other devices?
php artisan serve --host 192.168.0.104 --port 80

The command `php artisan serve --host 192.168.0.104 --port 80` in Laravel is used to start the Laravel development server. 


Here's what each part of the command does:


- `php artisan serve`: This part of the command starts the Laravel development server.

- `--host 192.168.0.104`: This option sets the host address to `192.168.0.104`. This means the server will be accessible from this IP address.

- `--port 80`: This option sets the port number to `80`. This means the server will listen for requests on this port.


So, this command is a quick way to start your Laravel application on a specific IP address and port. It's a handy tool for testing your application in a local network. Please note that you should replace port and ip address with the your ip and port.