Laravel 8 Tutorial for Beginner

Laravel 8 Tutorial for Beginner

Laravel 8 Tutorial for Beginners

Laravel is a powerful and user-friendly PHP framework for building modern web applications. In this tutorial, we’ll walk you through installing, setting up, and creating a basic Laravel 8 application from scratch.

What You’ll Learn:
✔️ Install Laravel 8
✔️ Configure database and migrations
✔️ Create routes, controllers, and views
✔️ Work with Blade templates
✔️ Use Eloquent ORM for database operations

Step 1: Install Laravel 8

1.1 Install PHP and Composer

Laravel requires PHP 7.3+. Install PHP and required extensions:

sudo apt update sudo apt install php-cli php-mbstring php-xml php-bcmath php-curl php-zip unzip curl -y

Then install Composer, the dependency manager for Laravel:

curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer

Check if Composer is installed:

composer -V

1.2 Install Laravel 8 via Composer

Run the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel laravel8app "8.*"

Navigate to the Laravel project directory:

cd laravel8app

Run the Laravel development server:

php artisan serve

Now, open your browser and go to:

http://127.0.0.1:8000

🎉 You should see the Laravel 8 welcome page!

Step 2: Configure Database

2.1 Set Up MySQL

If MySQL is not installed, install it using:

sudo apt install mysql-server -y

Start and enable MySQL:

sudo systemctl start mysql sudo systemctl enable mysql

Log in to MySQL and create a database:

sudo mysql -u root -p

Run the following SQL commands:

CREATE DATABASE laravel8_db; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON laravel8_db.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES; EXIT;

2.2 Update Laravel .env File

Open the .env file and update the database settings:

DB_DATABASE=laravel8_db DB_USERNAME=laravel_user DB_PASSWORD=password

Step 3: Run Migrations

Laravel uses migrations to create database tables. Run:

php artisan migrate

If you see an error regarding foreign key constraints, run:

php artisan migrate:fresh --seed

Step 4: Create a Model, Migration, Controller, and Route

Let’s create a Post model with a migration and controller:

php artisan make:model Post -mcr

This will generate:
app/Models/Post.php (Eloquent Model)
database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php (Migration)
app/Http/Controllers/PostController.php (Controller)

4.1 Define the Database Schema in Migration

Open the migration file (database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php) and modify the up() method:

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

Run the migration to create the posts table:

php artisan migrate

Step 5: Create Routes and Controller Methods

5.1 Define Routes

Open routes/web.php and add:

use App\Http\Controllers\PostController; Route::get('/posts', [PostController::class, 'index']); Route::get('/posts/create', [PostController::class, 'create']); Route::post('/posts', [PostController::class, 'store']);

5.2 Implement Controller Methods

Open app/Http/Controllers/PostController.php and update the class:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller { public function index() { $posts = Post::all(); return view('posts.index', compact('posts')); } public function create() { return view('posts.create'); } public function store(Request $request) { $request->validate([ 'title' => 'required', 'content' => 'required', ]); Post::create([ 'title' => $request->title, 'content' => $request->content, ]); return redirect('/posts')->with('success', 'Post created successfully!'); } }

Step 6: Create Blade Views

6.1 Create Posts Index View

Create a new folder resources/views/posts, then create a file index.blade.php:

@extends('layouts.app') @section('content') <h1>All Posts</h1> <a href="{{ url('/posts/create') }}">Create Post</a> <ul> @foreach ($posts as $post) <li>{{ $post->title }}</li> @endforeach </ul> @endsection

6.2 Create Create Post View

Create create.blade.php:

@extends('layouts.app') @section('content') <h1>Create a New Post</h1> <form action="{{ url('/posts') }}" method="POST"> @csrf <label>Title:</label> <input type="text" name="title"> <label>Content:</label> <textarea name="content"></textarea> <button type="submit">Submit</button> </form> @endsection

6.3 Create a Layout File

Create resources/views/layouts/app.blade.php:

<!DOCTYPE html> <html> <head> <title>Laravel 8 Blog</title> </head> <body> @yield('content') </body> </html>

Step 7: Test Your Application

Start the Laravel server:

php artisan serve

Now, visit these routes in your browser:

  • http://127.0.0.1:8000/posts → List all posts
  • http://127.0.0.1:8000/posts/create → Create a new post

Conclusion

🎉 Congratulations! You have successfully created a basic Laravel 8 application. From here, you can:
✅ Add authentication (php artisan make:auth in Laravel 7 and older, jetstream in Laravel 8)
✅ Use Eloquent relationships (one-to-many, many-to-many)
✅ Integrate APIs with Laravel Sanctum or Passport

💬 Need more features? Let us know in the comments! 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close