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:
# 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:
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=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:
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 tool4. Routing
Define your application routes in 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:
php artisan make:migration create_posts_tableEdit the generated migration file:
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();
});
}# Run the migration
php artisan migrate6. Eloquent Model
Create a model for the posts table:
# Create model with migration, factory, and seeder
php artisan make:model Post -mfsnamespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'title',
'body',
'status',
];
}7. Controller (CRUD)
Generate a resource controller:
php artisan make:controller PostController --resource --model=Postnamespace 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
# 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 --seedSummary
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