Laravel Tutorial for Beginners


Introduction to Laravel

Laravel is a popular PHP framework that simplifies web application development with elegant syntax, robust tools, and efficient features. This tutorial will walk you through the basics of Laravel and help you get started with your first Laravel project.

Prerequisites

  • Basic knowledge of PHP
  • Composer installed on your system
  • PHP installed (minimum version 8.0)
  • A local development server like XAMPP, WAMP, or Laravel Homestead

Step 1: Install Laravel

To get started, you need to install Laravel using Composer. Open your terminal or command prompt and run the following command:

composer create-project --prefer-dist laravel/laravel myproject

This will create a new Laravel project named myproject.

Step 2: Directory Structure

After installation, explore the Laravel project structure. Here are some key folders:

  • app/: Contains the core application code (Controllers, Models, etc.)
  • routes/: Defines the routes of the application.
  • resources/: Contains views (Blade templates), assets, and localization files.
  • config/: Holds configuration files.
  • database/: For migrations, factories, and seeds.

Step 3: Configure Environment

Laravel uses the .env file to manage environment-specific settings like database configuration, mail settings, etc. Open the .env file and update your database settings:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Make sure the database is created beforehand.

Step 4: Create a Route

Routes define how URLs map to specific actions or logic in the application. Open routes/web.php and add the following:


Route::get('/', function () {
    return view('welcome');
});

This route will display the welcome view when you visit the home page (/).

Step 5: Create a Controller

Laravel follows the MVC (Model-View-Controller) architecture. Let's create a simple controller to handle logic.

Run the following command to generate a new controller:

php artisan make:controller PageController

This creates PageController.php in the app/Http/Controllers directory. Open the controller and define a method:

php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller
{
public function about()
{
return view('about');
}
}

Now, create a new route for this controller method in routes/web.php:

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

Step 6: Create a View

Views are stored in the resources/views directory. Let's create an about.blade.php file.

html
<!-- resources/views/about.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About</title>
</head>
<body>
<h1>About Us</h1>
<p>This is the about page.</p>
</body>
</html>

Now, when you visit /about, you’ll see the content from about.blade.php.

Step 7: Migrations and Database Setup

Laravel uses migrations to manage database schema changes. Let's create a migration for a posts table.

Run the following command:

bash
php artisan make:migration create_posts_table
php
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
bash

This creates a migration file in the database/migrations directory. Open it and define the table structure:

Run the migration to create the table:

php artisan migrate 

Step 8: Create a Model

Models in Laravel represent the data in your application. Let's create a model for Post:

bash
php artisan make:model Post

This will create the Post.php model in the app/Models directory.

You can now use this model to interact with the posts table in the database.

Step 9: Fetch and Display Data

Let’s fetch posts from the database and display them on the front-end. First,

add some dummy data using Tinker:

bash

php artisan tinker
php
Post::create(['title' => 'First Post', 'body' => 'This is the body of the first post.']);
php
use App\Models\Post;
public function about() {
$posts = Post::all();
return view('about', ['posts' => $posts]);
}
php
@foreach ($posts as $post)
<h2>{{ $post->title }}</h2>
<p>{{ $post->body }}</p>
@endforeach

In the Tinker console, run:

Now, fetch and display the posts in the view. In your PageController, modify the about method:

In the about.blade.php view, display the posts:

Now, when you visit /about, it will display all posts from the database.

Step 10: Run the Application

To run your Laravel application, use the built-in development server:

bash 

php artisan serve

Now, visit http://localhost:8000 to see your Laravel app in action.


Comments