web-dev2 min read

Laravel Tutorial: Learn PHP Framework from Scratch (2026)

Laravel Tutorial: Learn PHP Framework from Scratch (2026)

Published:  |  Category: Web Dev  |  Reading time: ~15 min
Laravel Tutorial: Learn PHP Framework from Scratch (2026)

Laravel is a PHP web framework known for its elegant syntax, expressive ORM, and comprehensive ecosystem. Created by Taylor Otwell in 2011, Laravel is the most popular PHP framework, powering everything from small blogs to enterprise SaaS platforms. Its philosophy emphasizes developer happiness, convention over configuration, and clean MVC architecture.

Laravel ships with Eloquent ORM, Blade templating, Artisan CLI, queue system, task scheduling, and built-in authentication. The ecosystem includes Laravel Vapor (serverless), Nova (admin panels), and Horizon (queue monitoring).

Routing and Controllers

Laravel routes are defined in routes/web.php and routes/api.php. The router supports closures, controller methods, resourceful routing, and route model binding where Eloquent models are automatically injected. Route groups apply middleware and prefixes to multiple routes.

Controllers group related request logic. php artisan make:controller PostController --resource generates CRUD methods. Route::resource('posts', PostController::class) maps all standard CRUD routes automatically.

use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => view('welcome'));

Route::middleware(['auth'])->group(function () {
    Route::resource('posts', PostController::class);
});

// Controller with route model binding
class PostController extends Controller
{
    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }

    public function update(Request $request, Post $post)
    {
        $post->update($request->validated());
        return redirect()->route('posts.show', $post);
    }
}

Eloquent ORM and Relationships

Eloquent is Laravel's ActiveRecord ORM. Each database table has a Model class for querying and relationships. Eloquent supports hasOne, hasMany, belongsTo, belongsToMany, hasManyThrough, and polymorphic relationships.

Accessors and mutators transform attribute values. Global scopes add constraints to every query. The withCount() method efficiently counts related records. Soft deletes keep records with a deleted_at timestamp.

class User extends Model
{
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}

class Post extends Model
{
    protected $fillable = ['title', 'body'];

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Tag::class);
    }

    public function getExcerptAttribute(): string
    {
        return Str::limit($this->body, 100);
    }
}

$posts = Post::with('author')->where('published', true)->latest()->paginate(20);

Blade Templating Engine

Blade compiles templates into cached PHP for performance. Templates use .blade.php extensions with @extends, @section, @yield for inheritance. Components and slots provide reusable UI elements.

Blade control structures include @if, @unless, @for, @foreach, @forelse. The @csrf directive generates CSRF token fields. Stacks (push/endpush) handle scripts and styles from child views.





    @yield('title', 'My App')
    @stack('styles')


    
    
@yield('content')
@stack('scripts')