Kritim Yantra
Apr 19, 2025
Introduction
Laravel has long been celebrated as one of the most developer-friendly PHP frameworks, and Laravel 12 continues that tradition by introducing refinements that make building modern web applications even more effortless. If you’re a beginner looking to dive into Laravel 12, this guide will walk you through everything you need to know—from setting up your development environment to deploying your first app—in a clear, step-by-step manner.
Before diving in, make sure you’re comfortable with:
If any of these are new to you, take a short detour to shore up your fundamentals. There are excellent free resources (like PHP: The Right Way) and interactive tutorials to get you up to speed in a day or two.
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version
Laravel offers a global installer for convenience:
composer global require laravel/installer
# Ensure ~/.composer/vendor/bin (or ~/.config/composer/vendor/bin) is in your PATH
laravel --version
For beginners, Sail is fantastic because it mirrors production environments:
laravel new myapp --dev
cd myapp
./vendor/bin/sail up -d
laravel new blog
# or if using Composer directly:
composer create-project laravel/laravel blog "12.*"
cd blog
php artisan serve
http://127.0.0.1:8000
in your browser—you should see the Laravel welcome page.app/
: Your application code (Models, Controllers, Middleware). routes/
: Define web (web.php
), API (api.php
), console, and broadcast channels. resources/
: Views (Blade templates), raw assets (Sass, JavaScript). database/
: Migrations, seeders, factories. config/
: Framework and package configuration files. public/
: Web server’s document root (index.php, asset files).Take a tour: open each directory, skim file names, and refer to the official docs for deeper explanations.
Open routes/web.php
:
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about');
});
get
, post
, put
, delete
, etc. Route::get('/users/{id}', [UserController::class, 'show'])
->name('users.show');
Generate with Artisan:
php artisan make:controller UserController
In app/Http/Controllers/UserController.php
:
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends Controller
{
public function show($id)
{
$user = User::findOrFail($id);
return view('users.show', compact('user'));
}
}
Blade is Laravel’s powerful, minimal templating engine.
Layouts & Sections
<!-- resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
<html>
<head>
<title>@yield('title', 'My Laravel App')</title>
</head>
<body>
@include('partials.nav')
<div class="container">
@yield('content')
</div>
</body>
</html>
Views
<!-- resources/views/users/show.blade.php -->
@extends('layouts.app')
@section('title', $user->name)
@section('content')
<h1>{{ $user->name }}</h1>
<p>Email: {{ $user->email }}</p>
@endsection
Control Structures
@if($users->isEmpty())
<p>No users found.</p>
@else
@foreach($users as $user)
<li>{{ $user->name }}</li>
@endforeach
@endif
Migrations version-control your database schema:
php artisan make:migration create_users_table
In the new migration file:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
Run migrations:
php artisan migrate
Define a User
model in app/Models/User.php
(already present in new projects). Here’s how to establish relationships:
class User extends Authenticatable
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
// And in Post model:
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
Quickly generate test data:
php artisan make:factory UserFactory --model=User
In database/factories/UserFactory.php
:
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'password' => bcrypt('secret'),
];
}
Seed your database:
php artisan make:seeder DatabaseSeeder
# In DatabaseSeeder:
User::factory()->count(50)->create();
php artisan db:seed
Blade’s form helpers (optional) or plain HTML:
<form action="{{ route('posts.store') }}" method="POST">
@csrf
<input type="text" name="title" value="{{ old('title') }}">
@error('title')
<div class="text-red-500">{{ $message }}</div>
@enderror
<button type="submit">Create Post</button>
</form>
In PostController
:
public function store(Request $request)
{
$data = $request->validate([
'title' => 'required|min:5|max:255',
'body' => 'required',
]);
Post::create($data);
return redirect()->route('posts.index')
->with('success', 'Post created successfully!');
}
Laravel 12 simplifies adding auth:
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
For authorization, use Gates and Policies:
php artisan make:policy PostPolicy --model=Post
In controllers:
public function update(Request $request, Post $post)
{
$this->authorize('update', $post);
// ...
}
Putting it all together, build a simple Task Manager:
php artisan make:model Task -m
php artisan make:controller TaskController --resource
Route::resource('tasks', TaskController::class);
index.blade.php
, create.blade.php
, edit.blade.php
, show.blade.php
.php artisan test
Once you’re comfortable with the basics, explore:
cron
). The Laravel documentation and Laracasts series by Jeffrey Way are invaluable for deep dives.
Deploy your Laravel 12 app using:
public/
folder)Key steps:
.env
on server). php artisan config:cache
php artisan route:cache
php artisan view:cache
Mastering Laravel 12 is a journey that blends the fundamentals of PHP with the magic of a modern framework designed for developer happiness. By following this structured path—setting up your environment, understanding core concepts, building a CRUD app, and then exploring advanced features—you’ll gain both confidence and competence.
Remember, the best way to learn is by doing. Tackle small projects, read code, ask questions, and contribute back to the community. Before long, you’ll not only be comfortable with Laravel 12—you’ll be able to craft robust, scalable applications that stand the test of time.
Happy coding!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google