What is Laravel?
Laravel is an elegant PHP framework designed for web developers who need a simple, elegant toolkit to create full-featured web applications. It follows the MVC pattern and provides a clean, expressive syntax.
Expressive
Clean, readable code syntax
Powerful
Advanced tools and features
Secure
Built-in security features
Requirements
Before installing Laravel, make sure your environment meets these requirements:
- PHP >= 8.0
- Composer
- Web Server (Apache/Nginx)
- Database (MySQL/PostgreSQL)
- Node.js & NPM (for frontend)
Installation
Step-by-Step Guide
-
Create a new Laravel project
composer create-project laravel/laravel myproject
-
Navigate to your project directory
cd myproject
-
Start the development server
php artisan serve
-
Configure your database in .env file
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
-
Run migrations
php artisan migrate
Project Structure
-
app/
- Http/ (Controllers, Middleware)
- Models/ (Eloquent Models)
- Providers/ (Service Providers)
-
config/
- Configuration files
-
database/
- migrations/ (Database migrations)
- seeders/ (Database seeders)
-
public/
- Assets and entry point
-
resources/
- views/ (Blade templates)
- css/ (Stylesheets)
- js/ (JavaScript files)
-
routes/
- Application routes
-
storage/
- Logs, cache, uploads
Routing
Routes in Laravel are defined in the routes/web.php
file for web routes and routes/api.php
for API routes.
// Simple route with closure
Route::get('/', function () {
return view('welcome');
});
// Route with parameters
Route::get('/user/{id}', function ($id) {
return 'User ID: ' . $id;
});
use App\Http\Controllers\UserController;
// Single action
Route::get('/users', [UserController::class, 'index']);
// Resource controller (CRUD)
Route::resource('products', ProductController::class);
// Group with middleware and prefix
Route::middleware(['auth'])->prefix('admin')->group(function () {
Route::get('/dashboard', [AdminController::class, 'dashboard']);
Route::resource('products', ProductController::class);
});
php artisan route:list
to see all registered routes in your application.
Database Migrations
Migrations are like version control for your database schema. They allow you to define and share the application's database schema definition.
Creating Migrations
php artisan make:migration create_products_table
Migration Structure
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 10, 2);
$table->integer('stock');
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
Running Migrations
CRUD Operations
Here's a simplified example of implementing CRUD (Create, Read, Update, Delete) operations in Laravel:
php artisan make:model Product -m
php artisan make:controller ProductController --resource
// In routes/web.php
Route::resource('products', ProductController::class);
// Index - Show all products
public function index()
{
$products = Product::latest()->paginate(10);
return view('products.index', compact('products'));
}
// Create - Show create form
public function create()
{
return view('products.create');
}
// Store - Save new product
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'stock' => 'required|integer|min:0',
]);
Product::create($validated);
return redirect()->route('products.index')
->with('success', 'Product created successfully');
}
// Show - Display a product
public function show(Product $product)
{
return view('products.show', compact('product'));
}
// Edit - Show edit form
public function edit(Product $product)
{
return view('products.edit', compact('product'));
}
// Update - Save changes
public function update(Request $request, Product $product)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'stock' => 'required|integer|min:0',
]);
$product->update($validated);
return redirect()->route('products.index')
->with('success', 'Product updated successfully');
}
// Destroy - Delete a product
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success', 'Product deleted successfully');
}
Deployment
When deploying Laravel to production, follow these steps:
Server Setup
Set up a server with PHP 8.0+, a web server (Apache/Nginx), and database.
Upload Code
Use Git or FTP to upload your Laravel application to the server.
Environment Configuration
Set up your .env
file with production settings.
APP_ENV=production
APP_DEBUG=false
Install Dependencies
Run composer install with optimizations.
composer install --optimize-autoloader --no-dev
Optimize
Run Laravel's optimization commands.
php artisan config:cache
php artisan route:cache
php artisan view:cache
Permissions
Set correct permissions for storage and cache directories.
chmod -R 775 storage bootstrap/cache