Laravel Study Material
1. Introduction to Laravel
Laravel is a modern PHP framework that simplifies web application development by
following the MVC (Model-View-Controller) pattern.
It comes with built-in features like routing, templating, ORM (Eloquent), authentication, and
an expressive syntax that makes development faster and easier.
Key Features:
- MVC architecture
- Artisan CLI (command line tool)
- Blade templating engine
- Eloquent ORM for database
- Middleware for HTTP filtering
- Built-in Authentication and Authorization
- REST API support
- Event broadcasting, queues, jobs
2. Installation & Setup
Requirements:
- PHP >= 8.1
- Composer (dependency manager)
- MySQL/PostgreSQL/SQLite
- Node.js & npm (for frontend assets)
Steps:
1. Install Laravel project:
composer create-project laravel/laravel myapp
2. Navigate into project folder:
cd myapp
3. Run the local server:
php artisan serve
This starts the application at http://127.0.0.1:8000
3. Project Structure
Important folders:
- app/ → Models, Controllers, Middleware
- bootstrap/ → Laravel bootstrapping files
- config/ → Configuration files
- database/ → Migrations, Factories, Seeders
- public/ → Entry point (index.php), CSS, JS
- resources/views → Blade templates (frontend)
- routes/ → Web and API routes
- storage/ → Cache, logs, compiled templates
- tests/ → Automated tests
- vendor/ → Composer dependencies
4. Routing
Routes define how URLs map to controllers or views.
Example: Basic Route
Route::get('/hello', function () {
return 'Hello Laravel!';
});
Route with Controller:
Route::get('/users', [UserController::class, 'index']);
Route with Parameter:
Route::get('/user/{id}', [UserController::class, 'show']);
5. Controllers
Controllers group route logic.
Create Controller:
php artisan make:controller UserController
Example:
class UserController extends Controller {
public function index() {
return User::all();
}
public function show($id) {
return User::find($id);
}
}
6. Blade Templates
Blade is Laravel’s templating engine.
Example View (resources/views/welcome.blade.php):
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
Using Blade:
Route::get('/greet', function () {
return view('welcome', ['name' => 'Vivek']);
});
7. Database & Eloquent ORM
Eloquent is Laravel’s ORM.
Migration:
php artisan make:migration create_users_table
php artisan migrate
Model:
php artisan make:model User
CRUD Example:
$user = new User;
$user->name = "John";
$user->save();
$users = User::all();
$user = User::find(1);
$user->delete();
8. Forms & Validation
Forms in Blade:
<form action="/user" method="POST">
@csrf
<input type="text" name="name">
<button type="submit">Save</button>
</form>
Validation in Controller:
$request->validate([
'name' => 'required|min:3'
]);
9. Middleware
Middleware filters HTTP requests.
Create Middleware:
php artisan make:middleware CheckUser
Example:
public function handle($request, Closure $next) {
if (!$request->user()) {
return redirect('/login');
}
return $next($request);
}
Register in app/Http/Kernel.php
10. Authentication
Authentication can be added using Laravel Breeze or Laravel UI.
Steps:
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
11. REST API Development
API Routes (routes/api.php):
Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);
API Response:
return response()->json(User::all());
12. Advanced Topics
- Queues & Jobs (php artisan queue:work)
- Events & Listeners
- Notifications (Mail, SMS, Slack)
- Task Scheduling (app/Console/Kernel.php)
- Testing (php artisan test)
- Service Container & Service Providers
13. Artisan Commands
Useful Commands:
php artisan route:list # Show routes
php artisan make:model Post # Create model
php artisan migrate # Run migrations
php artisan tinker # Interactive shell
php artisan cache:clear # Clear cache
14. Best Practices
- Follow MVC architecture strictly
- Use Resource Controllers for CRUD
- Validate input using Form Requests
- Use Eloquent relationships instead of raw SQL
- Store sensitive data in .env file
- Follow PSR-12 coding standards
- Write unit and feature tests
15. Interview Questions (Sample)
Q1. What is Laravel and why use it?
Q2. Explain Service Container in Laravel.
Q3. Difference between one-to-one, one-to-many, and many-to-many relationships in
Eloquent?
Q4. What are Laravel Facades?
Q5. Difference between “require” and “require_once” in PHP?
Q6. How to handle file uploads in Laravel?
Q7. How do you implement authentication and authorization?
Q8. What is CSRF protection and how is it handled in Laravel?
Q9. What is the difference between Laravel’s “with()” and “load()” in Eloquent?
Q10. Explain Laravel Middleware with an example.