Laravel Basic Setup Project

LaravelPHPDec 28, 2025

Laravel is one of the most popular PHP frameworks for building modern web applications. In this guide, we'll walk through setting up a Laravel project from scratch, covering installation, configuration, routing, controllers, models, migrations, and building your first CRUD application.

Prerequisites

  • PHP 8.2 or higher installed
  • Composer (PHP dependency manager)
  • Node.js & npm (for frontend assets)
  • A database (MySQL, PostgreSQL, or SQLite)

1. Installation

First, install the Laravel installer globally via Composer, then create a new project:

Terminal
# Install Laravel installer globally
composer global require laravel/installer

# Create a new Laravel project
laravel new my-project

# Or using Composer directly
composer create-project laravel/laravel my-project

# Navigate into the project
cd my-project

# Start the development server
php artisan serve

Your application will be available at http://localhost:8000.

2. Environment Configuration

Laravel uses a .env file for environment-specific configuration. Update your database settings:

.env
APP_NAME=MyProject
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_project
DB_USERNAME=root
DB_PASSWORD=
Tip: For quick local development, you can use SQLite. Set DB_CONNECTION=sqlite and remove the other DB_ lines. Laravel will create the database file automatically.

3. Project Structure

Here are the key directories you'll work with:

Project Structure
my-project/
├── app/
│   ├── Http/
│   │   ├── Controllers/    # Your controllers
│   │   └── Middleware/      # HTTP middleware
│   └── Models/              # Eloquent models
├── database/
│   ├── migrations/          # Database migrations
│   └── seeders/             # Database seeders
├── resources/
│   └── views/               # Blade templates
├── routes/
│   ├── web.php              # Web routes
│   └── api.php              # API routes
├── .env                     # Environment config
└── artisan                  # CLI tool

4. Routing

Define your application routes in routes/web.php:

routes/web.php
use App\Http\Controllers\PostController;

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

// Resource routes (creates all CRUD routes)
Route::resource('posts', PostController::class);

Run php artisan route:list to see all registered routes.

5. Database Migration

Create a migration for a posts table:

Terminal
php artisan make:migration create_posts_table

Edit the generated migration file:

database/migrations/xxxx_create_posts_table.php
public function up(): void
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('body');
        $table->string('status')->default('draft');
        $table->timestamps();
    });
}
Terminal
# Run the migration
php artisan migrate

6. Eloquent Model

Create a model for the posts table:

Terminal
# Create model with migration, factory, and seeder
php artisan make:model Post -mfs
app/Models/Post.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'title',
        'body',
        'status',
    ];
}

7. Controller (CRUD)

Generate a resource controller:

Terminal
php artisan make:controller PostController --resource --model=Post
app/Http/Controllers/PostController.php
namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->get();
        return view('posts.index', compact('posts'));
    }

    public function create()
    {
        return view('posts.create');
    }

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

        Post::create($validated);

        return redirect()->route('posts.index')
            ->with('success', 'Post created!');
    }

    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }

    public function edit(Post $post)
    {
        return view('posts.edit', compact('post'));
    }

    public function update(Request $request, Post $post)
    {
        $validated = $request->validate([
            'title' => 'required|max:255',
            'body'  => 'required',
        ]);

        $post->update($validated);

        return redirect()->route('posts.index')
            ->with('success', 'Post updated!');
    }

    public function destroy(Post $post)
    {
        $post->delete();

        return redirect()->route('posts.index')
            ->with('success', 'Post deleted!');
    }
}

8. Useful Artisan Commands

Terminal
# Clear all caches
php artisan optimize:clear

# List all routes
php artisan route:list

# Create model + migration + controller + seeder + factory
php artisan make:model Product -mcsf --resource

# Run database seeders
php artisan db:seed

# Rollback last migration
php artisan migrate:rollback

# Fresh migrate (drop all tables & re-migrate)
php artisan migrate:fresh --seed

Summary

You now have a working Laravel project with:

  • Project installation and environment setup
  • Database configuration and migrations
  • Eloquent models with fillable attributes
  • Resource controller with full CRUD operations
  • RESTful routing with validation