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:
Then install Composer, the dependency manager for Laravel:
Check if Composer is installed:
1.2 Install Laravel 8 via Composer
Run the following command to create a new Laravel project:
Navigate to the Laravel project directory:
Run the Laravel development server:
Now, open your browser and go to:
🎉 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:
Start and enable MySQL:
Log in to MySQL and create a database:
Run the following SQL commands:
2.2 Update Laravel .env
File
Open the .env
file and update the database settings:
Step 3: Run Migrations
Laravel uses migrations to create database tables. Run:
If you see an error regarding foreign key constraints, run:
Step 4: Create a Model, Migration, Controller, and Route
Let’s create a Post model with a migration and controller:
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:
Run the migration to create the posts
table:
Step 5: Create Routes and Controller Methods
5.1 Define Routes
Open routes/web.php
and add:
5.2 Implement Controller Methods
Open app/Http/Controllers/PostController.php
and update the class:
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
:
6.2 Create Create Post View
Create create.blade.php
:
6.3 Create a Layout File
Create resources/views/layouts/app.blade.php
:
Step 7: Test Your Application
Start the Laravel server:
Now, visit these routes in your browser:
http://127.0.0.1:8000/posts
→ List all postshttp://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! 🚀