Recent Versions:
- 12 – 2025 (PHP: 8.2 – 8.4)
- 11 – 2024 (PHP: 8.2 – 8.4)
- 10 – 2023 (PHP: 8.1 – 8.2)
- 9 – 2022 (PHP: 8.0 – 8.1)
- 8 – 2020 (PHP: 7.3 – 8.1)
- 7 – 2020 (PHP: 7.2 – 8.0)
- 6 – 2019 (PHP: >7.2 )
Version Change Logs:
- Laravel-12 coming with a new starter kit using all the latest and trendy set of tools and packages like Vue, react, livewire, tailwind, intertia. Async Caching is a new feature.
Required PHP Extensions:
- JSON
- Mbstring
- OpenSSL
- PDO
- BCMath
- Ctype
- Tokenizer
- XML
Database supported:
- MySQL
- PostgreSQL
- SQL Server
- SQLite
Cache supported:
- Memcached
- Redis
- Database (Define a table which will store the cache)
- File (Default)
Supported Queue drivers in the Laravel
- Beanstalkd
- SQS
- Redis
Session Supported:
- File – sessions are stored in storage/framework/sessions.
- Cookie – sessions are stored in secure, encrypted cookies.
- Database – (Define a table which will store the session item)
- Memcached / redis – sessions are stored in one of these fast, cache based stores.
- Array – sessions are stored in a PHP array and will not be persisted.
$request->session()->get(‘key’);
Advantages:
- It includes namespaces and interfaces, thus helping to organize and manage resources.
- It reuses the components from other frameworks in developing web applications.
- It utilizes Composer to manage its dependencies
- We can use multiple database and multiple cache mechanism
- Great documentation
- Its own command line interface
- Big community, Good documentation, easy to implement, secure.
Disadvantages:
- Customisation and upgrading versions are the common problems of all the frameworks. Genuinely it’s not a theoretical answer but practically I faced issues when I had to upgrade the laravel version in one of my projects. It was not docker based so at local, all went well but on production, we found issues.
Configuration:
- Configure public as web root directory of the project
- Configuration files are stored into the config folder
- Write permissions to the storage and the bootstrap/cache
- php artisan key:generate to generate the key
- rename the .env.example to .env and set the configuration parameters into it like database details, application key
- enable the mod_rewrite module in php modules
Laravel Request Lifecycle
- Request comes to public/index.php file. It loads the composer generated autoloaders and creates its own instance using bootstrap/app.php
- Next, a request sent to the kernel. Kernel configures the
- error handling,
- logging,
- middleware,
- loading service providers and other configurations based on env variables, which must be handled before the request is processed. It’s Laravel’s internal mechanism.
- Note: Here service providers play a big role. It loads all the important components/packages/libraries.
- Now the request is sent to the router, which will decide the controller/method.
- Once the controller finishes its job, response is returned to the router.
- Route will redirect it to the kernel (send method) which will give a response to the user.
Laravel Design Patterns
| Pattern Name | Description |
| Builder pattern | It builds complex objects step by step and returns them. It can decide whether to return something or not. Uses: Good for creating complex products. |
| MVC Pattern | MVC is a well-known design pattern used for separating an application into three main components: Model, View, and Controller. |
| Repository Pattern | Repository pattern is used to separate out the data layer from the rest of the business logic. |
| Service Layer Pattern | The Service Layer pattern involves creating classes that encapsulate specific business logic or application services. This helps in keeping your controllers lean and focused on handling HTTP requests and responses, while the actual business logic resides in service classes. |
| Dependency Injection (DI) | It involves injecting dependencies (such as database connections, services, or repositories) into classes rather than creating them within the class. |
| Observer Pattern | Laravel provides an implementation of the Observer pattern through its “event” system. This pattern allows you to define events and listeners that react to those events. It’s useful for decoupling different parts of your application and handling various actions or notifications triggered by events. |
| Strategy Pattern | Storage drivers for caching are made using a strategy pattern, which can be switched easilly. The Strategy pattern is useful when you want to define a family of interchangeable algorithms and make them easily switchable. In Laravel, this pattern can be seen in the way you can define different storage drivers for file uploads or caching. |
| Factory Pattern | This helps in generating test data for your application’s database models. |
| Provider Pattern | The provider pattern is the core of the Laravel framework and the packages we use. It’s a set of patterns for some essential services. It’s like a plug-in or packages into our service. It provides classes that we can use in our namespace. |
| Facade Pattern | Laravel uses facades to provide a simple interface to complex subsystems. Facades act as “static” proxies to classes in the service container, providing a convenient way to interact with various components. |
Middlewares
Details
Middleware acts as a bridge between the request and application business logic. It is a type of filtering mechanism. Like laravel default middleware verifies whether the user of the application is authenticated or not. Middleware Types: Global Middleware (Kernel.php), Route Middleware ()
Default Middlewares:
- VerifyCsrfToken
- AuthenticateWithBasicAuth
- ThrottleRequests (Rate-limiting requests from a particular IP. It prevents DDOS attacks.)
Laravel
Details
Service provider is a configuration class that is used to bind the services to the service containers. It contains register and boot methods.
It registers the middleware, event listener, subscribers, repository and other classes to the service containers, so that service containers can inject them where they are required.
- php artisan make:provider PaymentServiceProvider
- Use the singleton or bind method to register the service.
- Add the service provider to the providers array in the config/app.php file
Service Containers (Dependency Injection System)
Details
The service container is a powerful dependency injection system. It manages class dependencies and resolves them when needed. We don’t need to inject each dependency manually when needed, it is done by the service container automatically.
Service Provider Vs Container:
Details
Service providers are the configurable classes that contain register and booth methods, which are used to bind the services and dependencies to the application. While, a service container is the engine that enables to bind and use the injected packages in the whole application.
Events: An event is an occurrence or action that helps you to subscribe and listen for events that occur in Laravel applications. Some of the events are fired automatically by Laravel when any activity occurs.
ORM: Object-relational mapping technique for converting data between incompatible type systems using object-oriented programming languages like Eloquent ORM
Eloquent ORM: it means that the models you create in the MVC will have a corresponding table in the database. The ORM has built-relationships, so if you manipulate one table in the database, you manage the related data as well. The following relationships are possible => one-to- one, one-to- many, many-to- many, has- many- through, polymorphic relationships, and many-to- many polymorphic relationships.
Facade:
Facade is a class that provides access to an object from the container. Facades are a way to register your class and its methods in Laravel Container so they are available in your whole application after getting resolved by Reflection. The main benefit of using facades is we don’t have to remember long class names and also don’t need to require those classes in any other class for using them. It also gives more testability to the application.
Contract:
Details
Contracts are a set of interfaces (set of rules) that defines the core services offered by the Laravel. For example, a Queue contract defines the methods needed for queueing jobs, while the Mailer contract defines the methods needed for sending email.
- Mailer contract
- Queue contract
Factories:
Factories are a way to put values in fields of a particular model automatically.We can use factories to generate a class for each model and put data in fields accordingly.
php artisan make:factory UserFactory –class=User
| Is it possible to set DB connection in migration file. | Yes, Using following variable : protected $connection = ‘pgsql’; |
| Is it possible to set DB connection in up method | Yes, Schema::connection(‘sqlite’)->create(‘users’, function (Blueprint $table) { } |
| Is it possible to add if conditions in migration | Yes, if (Schema::hasTable(‘users’)) |
| How to Turn off CSRF protection for a particular route | app\Http\Middleware\VerifyCsrfToken.php => protected $except = [‘Url’]; |
| How to check ajax requests | $request->ajax() |
| How to check if user is logged in | Auth::user() |
| How to extend login expire time in Auth? | config\session.php => ‘lifetime’ => 180 |
| How to make a constant and use globally | config/constants.php => Config::get(‘constants.KEY’); |
| How to process jobs from a queue | php artisan queue:work –queue=high,default |
| Handle Exceptions | App\Exceptions\Handler class |
| How to prevent SQL Injection | Using Parameter binding in the DB queries |
| What is app() | Global helper that provides access to the service container. It allows you to retrieve services registered in the container. |
Packages:
- Laravel Tinker: CLI tool that allows developers to interact with their Laravel applications in real time. It provides a REPL (Read-Eval-Print Loop) environment, enabling you to run PHP code, access application models, test database queries, and perform other operations directly within the terminal.
- Sail: CLI tool to interact with Laravel default docker environment. It helps to execute the artisan commands within the application’s Docker container. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.
- Dusk: Used for browser testing and automation. Dusk uses a standalone ChromeDriver installation. php artisan dusk:make LoginTest
- Jetstream: Initial setup with basic features…login, register, etc.
- Socialite: Social Authentication
- Passport: Auth2 implementation
- Cachier: Stripe payment integration
- Nova: Admin panel
- Horizon: Queue Manager
- Octane: Provide high-powered application servers for better application performance
- Spark: Subscription + Payment System
- Homestead: A basic setup including OS for web development. We dont need to install php or anything if using homestead.
- Telescope & Debugbar: Developer tool to show logs, incoming request, jobs, notifications and many more statistics.
One Liners:
- Collection: Collection is like a php array but in a more convenient form.
- Broadcasting: Send notifications to the client side when something happens on the server side.
- Serializing: Way of converting data. ToArray, ToJson etc.
- @yield is a Blade directive [Child page ===Content====> master page]
Class auto-loading:
- Auto-Loading allows you to load class files when they are needed without explicitly loading or including them.
- Command “composer dump-autoload” regenerates the list of all the classes that need to be included in the project (autoload_classmap.php)
- Following vendor files are autoloaded:
- “autoload”: {
- “classmap”: [
- “app/commands”,
- “app/controllers”,
- “app/models”,
- “app/database/migrations”,
- “app/tests/TestCase.php”
- ]
- }
Closure Function:
- Function without name, that can be
- Stored to a variable
- Passed as an arguement
- Returned from another function
Laravel Observers Vs. Listeners:
- Observers are made to be triggered for model events like creating, updating, deleting. Observers are registered in the model class.
- Listeners are made to be triggered for Laravel custom events like event(new UserRegistered($user)). Listeners are registered in the service provider class.
Fillable Vs Guarded
- Fillable contains the fields which can be inserted into the database usgin mass assignment request()->all()
- Guarded attributes are used to specify those fields which are not mass assignable
Policy Vs. Gates:
- Both related to the Authorization, not authentication.
- Policies are classes that encapsulate authorization logic for a specific model or resource. They define methods like
view,create,update,delete, etc., to determine whether a user can perform those actions on a given model instance. - While, Gates are closures that define authorization logic for actions that aren’t tied to models. Good for non-model-related logic, like checking if the user can access the admin dashboard.
Accessor Vs Mutators:
- Accessor example getFullName. Used to calculate data fetched from the database.
- Mutators examples setFirstName, setLastName. Used to modify the data before saving to the database.
Namespace:
- Namespace allows the grouping of multiple classes that works together to perform a task
- Allows same name to be used for more than one class
- A namespace contains classes, functions, interfaces, traits etc.
Migration Squashing (Introduced in the Laravel-8):
Migration Squashing is a feature that allows you to combine (squash) many migration files into a single file to keep your codebase clean and organized — especially when a project has too many old migrations.
VS Code Extensions for Laravel:
- Laravel Intellisense : Autocomplete for everything
- Laravel Blade Snippets : Suggest Laravel related extension
- Laravel Artisan : Laravel artisan commands from VS code
- Laravel Docs : Will open the doc on browser
Method spoofing:
Normally, HTML forms do not support PUT, PATCH, or DELETE actions. So, if you want to call these actions from an HTML form, you need to define their routes by adding the hidden _method field to that form. Thus, the value you send with the _method field will be used as the HTTP request method.
Clear Cache:
- php artisan config:clear
- php artisan cache:clear
- php artisan view:clear
- php artisan route:clear
Create Migration File:
php artisan make:migration create_flights_table
Creating a table migration:
Schema::create(‘flights’, function (Blueprint $table) { $table->string(‘name’); });
Run the migration:
php artisan migrate
Run the migration forcefully (without prompt):
php artisan migrate –force
Rollback:
php artisan migrate:rollback
Rollback last 2 migration:
php artisan migrate:rollback –step=2
Rollback all migrations:
php artisan migrate:reset
Rollback and remigrate:
php artisan migrate:refresh
Check status:
php artisan migrate:status
Squash Migration (Merge all sql queries into one under database/schema folder):
php artisan schema:dump
To recreate whole database:
php artisan migrate:refresh
Create Seeder:
php artisan make:seeder [className]
Run the seeder:
php artisan db:seed
To create a new model:
php artisan make:model
To create a new factory:
php artisan make:factory UserFactory –class=User
To create a new command:
php artisan make:command SendEmails
Sail commands:
./vendor/bin/sail up
./vendor/bin/sail stop
Once alias are setup, you can run the sail command wihout fullpath.
sail artisan queue:work
sail composer require laravel/sanctum
To create a new Dusk test
php artisan dusk:make LoginTest
To run the browser dusk test:
php artisan dusk
To generate the application key:
php artisan key:generate
Create service provider:
php artisan make: provider ClientsServiceProvider
To create new project using command line:
laravel new example-app
To create new project using composer:
composer create-project laravel/laravel example-app
To Creates a special URL for the user to access the application during maintenance mode:
php artisan down –secret=”1630542a-246b-4b66-afa1-dd72a4c43515″
Note: It tells Laravel to issue a bypass cookie to the browser which will let the user view the application.