logo
GitHub

Complete Guide to Laravel for Beginners

What is Laravel?

Laravel is currently the most popular PHP full-stack development framework, with the following core features:

  1. Elegant Syntax: Clean and intuitive code structure
  2. MVC Architecture: Clear separation of business logic and presentation layer
  3. Built-in Tools: Includes authentication, queues, caching, and other common functionalities
  4. Active Community: Comprehensive ecosystem and documentation support

Environment Setup

1. System Requirements

  • PHP ≥ 8.1
  • Composer package manager
  • Database (MySQL/PostgreSQL/SQLite)
  • Web server (Nginx/Apache)

2. Installing Laravel

Create a new project using Composer:

composer create-project laravel/laravel my-project
cd my-project
php artisan serve

Visit http://localhost:8000 to see the welcome page

Core Concepts

1. Routing System

Defined in routes/web.php:

Route::get('/hello', function () {
    return view('welcome', ['name' => 'New User']);
});

2. Controllers

Create a controller:

php artisan make:controller UserController

Example controller method:

public function show($id)
{
    return User::findOrFail($id);
}

3. Models and Database

Create model and migration file:

php artisan make:model Post -m

Migration file example:

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

Quick Example Application Build

1. Create Todo Functionality

Generate scaffold:

php artisan make:model Todo -mcr

2. Define Routes

Route::resource('todos', TodoController::class);

3. Controller Logic

class TodoController extends Controller
{
    public function index()
    {
        return view('todos.index', [
            'todos' => Todo::all()
        ]);
    }

    public function store(Request $request)
    {
        Todo::create($request->validate([
            'title' => 'required|max:255'
        ]));

        return redirect('/todos');
    }
}

4. View Template

resources/views/todos/index.blade.php:

@extends('layouts.app') @section('content')
<h1>Todo List</h1>
<form method="POST" action="/todos">
  @csrf
  <input type="text" name="title" required />
  <button type="submit">Add</button>
</form>

<ul>
  @foreach ($todos as $todo)
  <li>{{ $todo->title }}</li>
  @endforeach
</ul>
@endsection

Special Features

1. Blade Template Engine

@if ($items->isEmpty())
<div class="alert">No data available</div>
@else @foreach ($items as $item)
<div>{{ $item->name }}</div>
@endforeach @endif

2. Artisan Command Line

Common commands:

php artisan make:model Product -a  # Create model and related files
php artisan migrate:refresh --seed # Reset database and seed test data
php artisan queue:work             # Start queue worker

3. Eloquent ORM

Advanced query example:

$users = User::where('active', true)
            ->with('posts')
            ->orderBy('name')
            ->paginate(10);

Learning Path Suggestions

  1. Basic Stage (1-2 weeks)

    • Routes and Controllers
    • Blade Template Basics
    • Database Migrations and Eloquent
  2. Intermediate Stage (3-4 weeks)

    • User Authentication System
    • Queues and Task Scheduling
    • API Development
  3. Advanced Stage (5-6 weeks)

    • Build Complete CRUD Applications
    • Third-party Service Integration
    • Performance Optimization Techniques

Common Issues

Q1: How to Debug Applications?

Use the dd() function:

dd($variable); // Print and terminate execution

Q2: How to Configure Environment Variables?

Modify the .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
  1. Official Documentation
  2. Laracasts Video Tutorials
  3. GitHub Open Source Projects
  4. Community: Laravel Forums

Conclusion

Laravel significantly reduces the barrier to PHP web development through its elegant syntax and rich features. Beginners are recommended to start with building simple CRUD applications, gradually master core concepts, and make good use of Artisan tools to improve development efficiency. Keeping up with official documentation updates and participating in community discussions are key to rapid growth.