Laravel Cheatsheet

0

eutopiantech eutopiantech April 21, 2025 laravel


Creating a project

composer create-project laravel/laravel example-app This command creates a new Laravel project with the specified name (`example-app`). It installs Laravel and its dependencies into a directory with the provided name.

Then cd to your 'example-app' project

 

Authentication (Optional)

composer require laravel/jetstream This command installs the Laravel Jetstream package, which provides a scaffolding for authentication and user management in Laravel applications.

Go to .env file and change the fields below to your preferred setting

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=laravel

DB_USERNAME=root

DB_PASSWORD=    

php artisan jetstream:install livewire This command installs Laravel Jetstream with the Livewire stack, providing authentication and user management features using Livewire components. 

 

Tailwind css (Optional)

npm install tailwindcss @tailwindcss/vite Install @tailwindcss/vite and its peer dependencies via npm.

or

Create a project using Genesis

composer require devdojo/genesis dev-main

php artisan ui genesis   

or

Cloning from GitHub

composer upgrade

composer install

npm install 

mv .env.example .env This command renames the `.env.example` file to `.env`. The `.env` file typically contains environment-specific configuration options for the application.

Ensure '.env.example' file exists then php artisan migrate This command executes all pending migrations.

php artisan serv starts a local development server.  

Authenticating users through other site eg. Google, Github ... composer require laravel/socialite This command installs the Laravel Socialite package, which provides a simple way to authenticate with OAuth providers like Google and Facebook.  

Switching from Vite to Webpack (Optional) not recommended

npm install laravel-mix --save-dev This command installs Laravel Mix as a development dependency for the project. Laravel Mix is a tool for defining Webpack build steps for Laravel applications.

npm install webpack webpack-cli --save-dev This command installs Webpack and Webpack CLI as development dependencies for the project. Webpack is a module bundler for JavaScript applications.

touch webpack.mix.js This command creates a new file named `webpack.mix.js`. This file is used to define Webpack build configurations for the project.  

General

php artisan serv This command starts the Laravel development server.

INFO  Server running on [http://127.0.0.1:8000].    

php artisan optimize:clear This command clears all cache

INFO  Clearing cached bootstrap files.    

cache ..................................................................................................................... 29.15ms DONE 

compiled ................................................................................................................... 2.87ms DONE 

config ..................................................................................................................... 0.88ms DONE 

events ..................................................................................................................... 1.34ms DONE   

routes ..................................................................................................................... 0.98ms DONE   

views ...................................................................................................................... 2.28ms DONE  

php artisan make:component AppLayout This command generates a new Blade component named `AppLayout`. Blade components are reusable pieces of UI in Laravel applications. e.g ()

INFO  Component [...\example-app\app\View\Components\TestLayout.php] created successfully.  

INFO  View [...\example-app\resources\views\components/test-layout.blade.php] created successfully.   

php artisan make:controller TestController This command generates a new controller named `TestController`.   

 INFO  Controller [...\example-app\app\Http\Controllers\TestController.php] created successfully.    

php artisan make:model addTest -m This command generates a new Eloquent model named `addTest` along with a migration file to create the corresponding database table.    

INFO  Model [...\example-app\app\Models\addTest.php] created successfully.    

INFO  Migration [...\example-app\database\migrations/2025_04_21_020723_create_add_tests_table.php] created successfully.  

php artisan make:mail OrderShipped --markdown=mail.orders.shipped creates a new email template class named "OrderShipped"    

INFO  Mailable [...\example-app\app\Mail\OrderShipped.php] created successfully.    

INFO  Markdown view [...\example-app\resources\views\mail/orders/shipped.blade.php] created successfully.  

php artisan make:command TestWork     

INFO  Console command [...\example-app\app\Console\Commands\TestWork.php] created successfully.      

php artisan make:middleware TestMiddleware                                

INFO  Middleware [...\example-app\app\Http\Middleware\TestMiddleware.php] created successfully.    

 

If you want to use queues in production follow these instructions

php artisan queue:work Runs the queue worker to process jobs continiously.

php artisan queue:flush Delete all failed jobs from the failed_jobs table.

php artisan queue:failed List all failed jobs. php artisan queue:clear Clears all pending jobs from the queue.  

php artisan key:generate This command generates a new application key. The application key is used for encryption and hashing in Laravel.  

INFO  Application key set successfully.    php artisan migrate This command executes all pending migrations to your db.   php artisan migrate:fresh This command drops all tables from the database and then executes all migrations, essentially resetting the database schema.  

php artisan migrate:refresh --seed This command resets and re-runs all migrations, then seeds the database with default data.   php artisan config:cache This command caches the configuration files for faster loading. It is useful in production environments to improve performance.  

INFO  Configuration cached successfully.  

composer dump-autoload is a command used in PHP projects that use Composer to rebuild the list of all classes that need to be autoloaded.   After Publishing Website php artisan storage:link This command creates a symbolic link from the `public/storage` directory to the `storage/app/public` directory. This is used to make files stored in the `storage` directory accessible from the web.  

Comments & Answers

Please login to comment.

Be the first to add a comment or answer!