Laravel Tutorial: A Beginner’s Guide to Modern PHP Framework

Laravel is one of the most popular PHP frameworks used for building modern, scalable, and secure web applications. It has gained wide adoption among developers because of its elegant syntax, powerful features, and strong community support. If you are a beginner eager to step into the world of PHP frameworks, Laravel Tutorial is the best place to start. In this tutorial, we will explore what Laravel is, why you should use it, how to set it up, and build a simple example project.

What is Laravel?

Laravel is an open-source PHP framework designed for web application development. It was created by Taylor Otwell in 2011 with the goal of simplifying common tasks such as authentication, routing, sessions, and caching. Instead of writing everything from scratch, Laravel provides ready-made tools and libraries to make development faster and more efficient.

Why Use Laravel?

Here are a few reasons why developers prefer Laravel:

  1. Elegant Syntax – Laravel’s code structure is clean and easy to read.

  2. MVC Architecture – It follows the Model-View-Controller pattern, which separates logic, UI, and data.

  3. Built-in Tools – Authentication, database migration, validation, and more are built-in.

  4. Blade Templating Engine – Makes it simple to design dynamic layouts with minimal code.

  5. Eloquent ORM – Provides an easy way to interact with databases using PHP models.

  6. Security – Laravel includes CSRF protection, password hashing, and query sanitization.

Prerequisites

Before starting, make sure you have the following installed:

  • PHP (8.0 or later recommended)

  • Composer (PHP dependency manager)

  • MySQL or another database

  • A local server like XAMPP or Laravel Valet

Installing Laravel

Laravel is installed via Composer. Open your terminal and run:

composer create-project laravel/laravel myapp

This will create a new Laravel project in a folder named myapp.

Navigate into the project folder:

cd myapp

Start the development server:

php artisan serve

Visit http://127.0.0.1:8000 in your browser, and you will see the default Laravel welcome page.

Laravel Folder Structure

A Laravel project comes with a structured directory. Some important folders are:

  • app/ – Contains application logic (Models, Controllers, Middleware).

  • resources/views/ – Stores Blade template files.

  • routes/web.php – Defines application routes.

  • database/migrations/ – Stores migration files for database schema.

  • public/ – Contains public assets like CSS, JS, and images.

Creating Your First Route

Routes in Laravel define how your application responds to user requests. Open the routes/web.php file and add:

Route::get('/hello', function () {
    return "Hello, Laravel!";
});

Now visit http://127.0.0.1:8000/hello in your browser, and you will see the message.

Controllers

Instead of writing logic inside routes, Laravel uses controllers. Create a controller using Artisan:

php artisan make:controller PageController

This creates a new controller file in app/Http/Controllers/.

Inside PageController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PageController extends Controller
{
    public function home()
    {
        return view('home');
    }
}

Now, update your routes/web.php:

Route::get('/', [PageController::class, 'home']);

Blade Templates

Laravel uses Blade as its templating engine. Create a new file resources/views/home.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Home</title>
</head>
<body>
    <h1>Welcome to Laravel!</h1>
    <p>This is your first Blade template.</p>
</body>
</html>

When you visit http://127.0.0.1:8000/, this page will load.

Database and Eloquent ORM

Laravel makes database handling easy with Eloquent ORM. First, set your database details in .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=

Now create a migration:

php artisan make:migration create_posts_table --create=posts

In the migration file:

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('content');
    $table->timestamps();
});

Run the migration:

php artisan migrate

This will create a posts table.

Next, create a model:

php artisan make:model Post

Inside Post.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
    protected $fillable = ['title', 'content'];
}

Now you can interact with the database easily:

Post::create([
    'title' => 'My First Post',
    'content' => 'This is content from Laravel ORM!'
]);

Example: Displaying Posts

In PageController.php, update the home method:

use App\Models\Post;

public function home()
{
    $posts = Post::all();
    return view('home', compact('posts'));
}

Now update home.blade.php:

<h1>All Posts</h1>
<ul>
    @foreach($posts as $post)
        <li><strong>{{ $post->title }}</strong>: {{ $post->content }}</li>
    @endforeach
</ul>

Now, when you visit your app, you’ll see all posts fetched from the database.

Conclusion

Laravel Tutorial is a modern PHP framework that simplifies web development with its clean syntax, MVC structure, and powerful tools. In this tutorial, we covered the basics of Laravel setup, routes, controllers, Blade templates, migrations, and Eloquent ORM. Once you are comfortable with these fundamentals, you can explore advanced features like middleware, authentication, API development, and queues.

Laravel makes coding not just faster, but also enjoyable. If you are planning to build web applications, mastering Laravel is a skill worth investing in.


Comments

Popular posts from this blog

Quantitative Aptitude Questions and Answers with Solutions for Beginners

What is a PHP Developer? Roles, Skills, and Career Guide

Java Tutorial: Master Object-Oriented Programming