Kritim Yantra
Apr 06, 2025
Laravel is a powerful PHP framework that simplifies web development with its elegant syntax and rich features. If you're just starting with Laravel, understanding its core concepts will help you build applications efficiently.
In this blog, we’ll cover:
Let’s dive in!
Laravel follows the MVC pattern, which separates an application into three parts:
User.php
). welcome.blade.php
). UserController.php
).✅ Why is MVC important?
Routes define URL endpoints and what happens when a user visits them.
// routes/web.php
Route::get('/hello', function () {
return "Hello, Laravel!";
});
/hello
will display "Hello, Laravel!".Route::get('/user/{id}', function ($id) {
return "User ID: " . $id;
});
/user/5
→ Output: "User ID: 5"✅ Common Route Types:
Route::get()
→ For reading data. Route::post()
→ For submitting forms. Route::put()
→ For updates. Route::delete()
→ For deletions.Blade is Laravel’s simple yet powerful templating engine.
<!-- resources/views/welcome.blade.php -->
<h1>Hello, {{ $name }}!</h1>
{{ $name }}
→ Outputs a PHP variable (escaped for security).<!-- resources/views/layouts/app.blade.php -->
<html>
<head><title>@yield('title')</title></head>
<body>
@yield('content')
</body>
</html>
<!-- resources/views/home.blade.php -->
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<h1>Welcome to Laravel!</h1>
@endsection
✅ Why use Blade?
Controllers handle application logic (instead of putting everything in routes).
php artisan make:controller UserController
// app/Http/Controllers/UserController.php
public function show($id) {
return "User ID: " . $id;
}
// routes/web.php
Route::get('/user/{id}', [UserController::class, 'show']);
✅ Best Practice:
Laravel uses Eloquent ORM to interact with databases.
php artisan make:model Post
// Get all posts
$posts = Post::all();
// Find a post by ID
$post = Post::find(1);
// Create a new post
Post::create(['title' => 'First Post']);
✅ Why Eloquent?
php artisan make:migration create_posts_table
// database/migrations/xxxx_create_posts_table.php
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
Run migrations:
php artisan migrate
php artisan make:seeder PostsTableSeeder
// database/seeders/PostsTableSeeder.php
Post::create(['title' => 'Sample Post']);
Run seeders:
php artisan db:seed
Middleware filters HTTP requests (e.g., authentication checks).
// routes/web.php
Route::get('/dashboard', function () {
return "Welcome to Dashboard!";
})->middleware('auth');
✅ Common Uses:
auth
middleware). admin
middleware).<form method="POST" action="/post">
@csrf
<input type="text" name="title">
<button type="submit">Submit</button>
</form>
public function store(Request $request) {
$validated = $request->validate([
'title' => 'required|max:255',
]);
// Store the post...
}
✅ Why Laravel Validation?
Laravel provides built-in auth scaffolding:
php artisan make:auth
Or use Laravel Breeze/Jetstream:
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
✅ Key Features:
Artisan is Laravel’s command-line tool for automating tasks.
Command | Description |
---|---|
php artisan make:model Post |
Creates a new model |
php artisan make:controller PostController |
Creates a controller |
php artisan migrate |
Runs database migrations |
php artisan serve |
Starts development server |
php artisan tinker |
Interact with Laravel in REPL |
✅ Why Artisan?
These 10 fundamental Laravel concepts will help you get started with confidence:
hasMany
, belongsTo
). Happy coding! 🚀
📌 Need help? Drop your questions below! 👇
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google