Laravel Fundamentals & Eloquent
Overview
Laravel is the most popular PHP framework, known for its elegant syntax and powerful features. This tutorial covers the essentials: project setup, routing, Eloquent ORM, migrations, and Blade templating. You'll build a complete CRUD application.
1. Laravel Setup
Install Laravel via Composer:
Laravel's directory structure:
app/– core application code (Models, Controllers)database/– migrations, seeders, factoriesroutes/– web.php (web routes), api.php (API routes)resources/views/– Blade templates
2. Routing & Controllers
Define routes in routes/web.php.
Controller
3. Eloquent ORM
Eloquent is Laravel's ORM. Models represent database tables.
Basic CRUD operations:
4. Migrations & Relationships
Create a migration
Relationships
5. Blade Templating
Blade is Laravel's templating engine. Use .blade.php files.
Products
-
@foreach ($products as $product)
- {{ $product->name }} - ${{ $product->price }} @endforeach
Quiz
Question 1
Which command creates a new Laravel project?
- laravel new my-app
- composer create-project laravel/laravel my-app
- php artisan new my-app
- npm create laravel
Show answer
Question 2
Which method retrieves all records from an Eloquent model?
- Model::get()
- Model::all()
- Model::find()
- Model::fetch()
Show answer
Question 3
Which Laravel component is used for templating?
- Twig
- Blade
- Mustache
- EJS
Show answer
Exercises
Exercise 1
Create a Category model and migration with fields: id, name,
description. Run the migration.
Sample answer
Exercise 2
Add a route GET /categories that returns a view listing all
categories.
Sample answer
Homework
Homework 1
Build a complete CRUD for Post with fields: title, content,
author. Use Eloquent and Blade.
Sample outline
- Model: Post (fillable: title, content, author)
- Controller: PostController with index, create, store, show, edit, update, destroy
- Routes: Route::resource('posts', PostController::class)
- Views: posts/index.blade.php, create.blade.php, edit.blade.php, show.blade.php
Mini‑Project
Blog with Categories
Build a blog application with:
- Posts: title, content, category_id, author
- Categories: name, description
- Relationship: Post belongs to Category
- CRUD for both resources
- Use Blade for the UI
Sample outline
- Models: Post, Category
- Migrations: posts (category_id foreign key), categories
- Controllers: PostController, CategoryController
- Routes: Route::resource for both
- Views: Layout with navigation, post listing with category filter
Tutorial Summary
You learned the fundamentals of Laravel: project setup, routing, Eloquent ORM, migrations, relationships, and Blade templating. You built a CRUD application with a beautiful, expressive syntax.
Key takeaway: Laravel's convention‑over‑configuration approach makes development fast and enjoyable. Its ecosystem is unmatched in the PHP world.